Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

การใช้งาน CSS [attribute] Selector

ใช้ Selector [attribute] เพื่อเลือกอิลิเมนต์ที่มีแอตทริบิวต์ที่ระบุ เช่น alt, target เป็นต้น

ตัวอย่างโค้ด

ลองรันโค้ดด้านล่างเพื่อทดสอบ CSS [attribute] Selector:

<!DOCTYPE html>
<html>
<head>
  <style>
    img[alt] {
      border: 3px solid orange;
    }
  </style>
</head>
<body>
  <img src="https://www.tutorialspoint.com/videotutorials/images/tutor_connect_home.jpg" height="200" width="200">
  <img src="https://www.tutorialspoint.com/videotutorials/images/tutorial_library_home.jpg" height="200" width="200" alt="Tutorials Library">
</body>
</html>

ผลลัพธ์: รูปภาพที่สองจะมีขอบสีส้มเพราะมีแอตทริบิวต์ alt ส่วนรูปแรกไม่มี alt จึงไม่ได้รับสไตล์