โดยใช้ CSS selectors เราสามารถจัดรูปแบบองค์ประกอบที่ต้องการอย่างเฉพาะเจาะจงตามความชอบของเรา มีวิธีต่างๆ สำหรับการเลือกองค์ประกอบใน HTML DOM
ไวยากรณ์
ไวยากรณ์สำหรับ CSS selectors ดังนี้:
Selector { /*declarations*/ }
ตัวอย่างต่อไปนี้แสดง CSS selector สำหรับการจัดรูปแบบองค์ประกอบ:
ตัวอย่าง
<!DOCTYPE html> <html> <head> <style> #one { filter: invert(85%); } </style> </head> <body> <img id="one" src="https://www.tutorialspoint.com/hadoop/images/hadoop-mini-logo.jpg"> <img src="https://www.tutorialspoint.com/plsql/images/plsql-mini-logo.jpg"> </body> </html>
ผลลัพธ์
ได้ผลลัพธ์ดังนี้:

ตัวอย่างเพิ่มเติม
<!DOCTYPE html> <html> <head> <style> div, p { margin: 4px; float: left; height: 200px; width: 300px; box-shadow: inset 0 2px 4px olive; background-image: url("https://www.tutorialspoint.com/sas/images/sas-mini-logo.jpg"); } p { background-image: url("https://www.tutorialspoint.com/qlikview/images/qlikview-mini-logo.jpg"); background-position: 50% 50%; color: black; } </style> </head> <body> <h2>บทช่วยสอนการเรียนรู้</h2> <div></div> <p>Tutorials</p> </body> </html>
ผลลัพธ์
ได้ผลลัพธ์ดังนี้:
