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

วิธีเลือกอิลิเมนต์ที่มีแอตทริบิวต์ที่ระบุด้วย CSS

เพื่อเลือกอิลิเมนต์ที่มีแอตทริบิวต์ที่ระบุ ให้ใช้ CSS [attribute] Selector

ตัวอย่างเช่น แอตทริบิวต์ 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 จึงไม่ได้รับสไตล์ เหมาะสำหรับตรวจสอบ accessibility (ภาพควรมี alt text)