สร้างรูปย่อ (Thumbnail) พร้อม Border, Border-radius และ Hover Box-shadow คลิกเพื่อดูรูปเต็มในแท็บใหม่
ตัวอย่างโค้ด
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> img { border: 3px solid rgb(208, 255, 0); border-radius: 4px; width: 150px; height: 150px; } img:hover { box-shadow: 2px 2px 4px 2px rgb(60, 255, 53); } </style> </head> <body> <h1>Thumbnail Image Example</h1> <a target="_blank" href="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"> <img src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"/></a> <h1>Clicking on the above thumbnail will open image in new tab</h1> </body> </html>
CSS Properties
- width/height: 150px - ขนาดคงที่สำหรับ Thumbnail
- border: 3px solid rgb(208, 255, 0) - ขอบสีเขียวเหลืองหนา 3px
- border-radius: 4px - มุมโค้งมนเล็กน้อย
- hover: box-shadow - เงาเขียวสว่างเมื่อ Hover
- <a target="_blank"> - คลิกเปิดรูปเต็มในแท็บใหม่
Responsive Thumbnail
img {
width: 100%;
max-width: 150px;
height: auto;
aspect-ratio: 1/1; /* รูปสี่เหลี่ยมจัตุรัส */
object-fit: cover; /* ครอบพื้นที่ ไม่บีดบัง */
}
ผลลัพธ์
รูปย่อ 150x150px ขอบเขียวเหลือง มุมมน Hover มีเงาเขียว คลิกเปิดรูปใหญ่แท็บใหม่