คลาสหลอกใช้เพื่อกำหนดสถานะพิเศษ ขององค์ประกอบ
ตัวอย่างเช่น สามารถใช้เพื่อ:
จัดรูปแบบองค์ประกอบเมื่อผู้ใช้วางเมาส์เหนือองค์ประกอบนั้น
รูปแบบของลิงก์ที่เข้าชมและไม่ได้เข้าชมแตกต่างกัน
จัดรูปแบบองค์ประกอบเมื่อได้รับการโฟกัส
Mouse Over Me
ไวยากรณ์ของคลาสหลอก:
selector:pseudo-class {
property: value;
}
ลิงค์สามารถแสดงได้หลายวิธี:
/* unvisited link */
a:link {
color: #FF0000;
}
/* visited
link */
a:visited {
color: #00FF00;
}
/* mouse over link */
a:hover {
color: #FF00FF;
}
/* selected link */
a:active {
color: #0000FF;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: hotpink;
}
/* selected link */
a:active {
color: blue;
}
</style>
</head>
<body>
<h2>Styling a link depending on state</h2>
<p><b><a href="default.asp" target="_blank">This is a link</a></b></p>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p>
</body>
</html>
หมายเหตุ: a:hover
ต้องอยู่หลัง a:link
และ a:visited
ในคำจำกัดความ CSS เพื่อให้มีประสิทธิภาพ! a:active
ต้องตามมาทีหลัง a:hover
ในคำจำกัดความ CSS เพื่อให้มีประสิทธิภาพ! ชื่อคลาสหลอกไม่คำนึงถึงขนาดตัวพิมพ์
คลาสหลอกสามารถใช้ร่วมกับคลาส HTML:
เมื่อคุณวางเมาส์เหนือลิงก์ในตัวอย่าง มันจะเปลี่ยนสี:
a.highlight:hover {
color: #ff0000;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
a.highlight:hover {
color: #ff0000;
font-size: 22px;
}
</style>
</head>
<body>
<h2>Pseudo-classes and HTML Classes</h2>
<p>When you hover over the first link below, it will change color and font size:</p>
<p><a class="highlight" href="css_syntax.asp">CSS Syntax</a></p>
<p><a href="default.asp">CSS Tutorial</a></p>
</body>
</html>
ตัวอย่างของการใช้คลาสหลอก :hover
ใน <div> องค์ประกอบ:
div:hover {
background-color: blue;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: green;
color: white;
padding: 25px;
text-align: center;
}
div:hover {
background-color: blue;
}
</style>
</head>
<body>
<p>Mouse over the div element below to change its background color:</p>
<div>Mouse Over Me</div>
</body>
</html>
วางเมาส์เหนือองค์ประกอบ <div> เพื่อแสดงองค์ประกอบ <p> (เช่นคำแนะนำเครื่องมือ):
ทาดา! ฉันอยู่นี่!
p {
display: none;
background-color: yellow;
padding: 20px;
}
div:hover p {
display: block;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
p {
display: none;
background-color: yellow;
padding: 20px;
}
div:hover p {
display: block;
}
</style>
</head>
<body>
<div>Hover over this div element to show the p element
<p>Tada! Here I am!</p>
</div>
</body>
</html>
คลาสหลอก :first-child
ตรงกับองค์ประกอบที่ระบุซึ่งเป็นลูกคนแรกขององค์ประกอบอื่น
ในตัวอย่างต่อไปนี้ ตัวเลือกจะจับคู่องค์ประกอบ <p> ใด ๆ ที่เป็นลูกแรกขององค์ประกอบใด ๆ :
p:first-child
{
color: blue;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
p:first-child {
color: blue;
}
</style>
</head>
<body>
<p>This is some text.</p>
<p>This is some text.</p>
<div>
<p>This is some text.</p>
<p>This is some text.</p>
</div>
</body>
</html>
ในตัวอย่างต่อไปนี้ ตัวเลือกจะจับคู่องค์ประกอบแรก <i> ในองค์ประกอบ <p> ทั้งหมด:
p i:first-child
{
color: blue;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
p i:first-child {
color: blue;
}
</style>
</head>
<body>
<p>I am a <i>strong</i> person. I am a <i>strong</i> person.</p>
<p>I am a <i>strong</i> person. I am a <i>strong</i> person.</p>
</body>
</html>
ในตัวอย่างต่อไปนี้ ตัวเลือกจะจับคู่องค์ประกอบ <i> ทั้งหมดในองค์ประกอบ <p> ที่เป็นลูกคนแรกขององค์ประกอบอื่น:
p:first-child i
{
color: blue;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
p:first-child i {
color: blue;
}
</style>
</head>
<body>
<p>I am a <i>strong</i> person. I am a <i>strong</i> person.</p>
<p>I am a <i>strong</i> person. I am a <i>strong</i> person.</p>
<div>
<p>I am a <i>strong</i> person. I am a <i>strong</i> person.</p>
<p>I am a <i>strong</i> person. I am a <i>strong</i> person.</p>
</div>
</body>
</html>
คลาสหลอก :lang
ช่วยให้คุณสามารถกำหนดกฎพิเศษสำหรับภาษาต่างๆ ได้
ในตัวอย่างด้านล่าง :lang
กำหนดเครื่องหมายคำพูดสำหรับ องค์ประกอบด้วย lang="no":
<html>
<head>
<style>
q:lang(no) {
quotes: "~" "~";
}
</style>
</head>
<body>
<p>Some text <q lang="no">A quote in a paragraph</q>
Some text.</p>
</body>
</html>
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
q:lang(no) {
quotes: "~" "~";
}
</style>
</head>
<body>
<p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p>
<p>In this example, :lang defines the quotation marks for q elements with lang="no":</p>
</body>
</html>
เพิ่มสไตล์ที่แตกต่างให้กับไฮเปอร์ลิงก์
<!DOCTYPE html>
<html>
<head>
<style>
a.one:link {color:#ff0000;}
a.one:visited {color:#0000ff;}
a.one:hover {color:#ffcc00;}
a.two:link {color:#ff0000;}
a.two:visited {color:#0000ff;}
a.two:hover {font-size:150%;}
a.three:link {color:#ff0000;}
a.three:visited {color:#0000ff;}
a.three:hover {background:#66ff66;}
a.four:link {color:#ff0000;}
a.four:visited {color:#0000ff;}
a.four:hover {font-family:monospace;}
a.five:link {color:#ff0000;text-decoration:none;}
a.five:visited {color:#0000ff;text-decoration:none;}
a.five:hover {text-decoration:underline;}
</style>
</head>
<body>
<h2>Styling Links</h2>
<p>Mouse over the links and watch them change layout:</p>
<p><b><a class="one" href="default.asp" target="_blank">This link changes color</a></b></p>
<p><b><a class="two" href="default.asp" target="_blank">This link changes font-size</a></b></p>
<p><b><a class="three" href="default.asp" target="_blank">This link changes background-color</a></b></p>
<p><b><a class="four" href="default.asp" target="_blank">This link changes font-family</a></b></p>
<p><b><a class="five" href="default.asp" target="_blank">This link changes text-decoration</a></b></p>
</body>
</html>
การใช้ :โฟกัส
<!DOCTYPE html>
<html>
<head>
<style>
input:focus {
background-color: yellow;
}
</style>
</head>
<body>
<form action="/action_page.php" method="get">
First name: <input type="text" name="fname"><br><br>
Last name: <input type="text" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
ตัวอย่าง:
a:active
คำอธิบายตัวอย่าง:
เลือกลิงค์ที่ใช้งานอยู่
ตัวอย่าง:
input:checked
คำอธิบายตัวอย่าง:
เลือกทุกองค์ประกอบที่เลือก <input>
ตัวอย่าง:
input:disabled
คำอธิบายตัวอย่าง:
เลือกทุกองค์ประกอบที่ปิดการใช้งาน <input>
ตัวอย่าง:
p:empty
คำอธิบายตัวอย่าง:
เลือกทุก <p> องค์ประกอบที่ไม่มีลูก
ตัวอย่าง:
input:enabled
คำอธิบายตัวอย่าง:
เลือกทุกองค์ประกอบที่เปิดใช้งาน <input>
ตัวอย่าง:
p:first-child
คำอธิบายตัวอย่าง:
เลือกทุก <p> องค์ประกอบที่เป็นลูกคนแรกของผู้ปกครอง
ตัวอย่าง:
p:first-of-type
คำอธิบายตัวอย่าง:
เลือกทุก <p> องค์ประกอบที่เป็นองค์ประกอบแรก <p> ของผู้ปกครอง
ตัวอย่าง:
input:focus
คำอธิบายตัวอย่าง:
เลือก <input> องค์ประกอบที่มีโฟกัส
ตัวอย่าง:
a:hover
คำอธิบายตัวอย่าง:
เลือกลิงก์เมื่อวางเมาส์ไว้เหนือ
ตัวอย่าง:
input:in-range
คำอธิบายตัวอย่าง:
เลือก <input> องค์ประกอบที่มีค่าภายในช่วงที่ระบุ
ตัวอย่าง:
input:invalid
คำอธิบายตัวอย่าง:
เลือกองค์ประกอบ <input> ทั้งหมดที่มีค่าไม่ถูกต้อง
ตัวอย่าง:
p:lang(it)
คำอธิบายตัวอย่าง:
เลือกทุก <p> องค์ประกอบที่มีค่าแอตทริบิวต์ lang เริ่มต้นด้วย "it"
ตัวอย่าง:
p:last-child
คำอธิบายตัวอย่าง:
เลือกทุก <p> องค์ประกอบที่เป็นลูกคนสุดท้ายของผู้ปกครอง
ตัวอย่าง:
p:last-of-type
คำอธิบายตัวอย่าง:
เลือกทุกองค์ประกอบ <p> ที่เป็นองค์ประกอบ <p> สุดท้ายของผู้ปกครอง
ตัวอย่าง:
a:link
คำอธิบายตัวอย่าง:
เลือกลิงก์ที่ยังไม่ได้เยี่ยมชมทั้งหมด
ตัวอย่าง:
:not(p)
คำอธิบายตัวอย่าง:
เลือกทุกองค์ประกอบที่ไม่ใช่องค์ประกอบ <p>
ตัวอย่าง:
p:nth-child(2)
คำอธิบายตัวอย่าง:
เลือกทุก <p> องค์ประกอบที่เป็นลูกคนที่สองของผู้ปกครอง
ตัวอย่าง:
p:nth-last-child(2)
คำอธิบายตัวอย่าง:
เลือกทุกองค์ประกอบ <p> ที่เป็นลูกคนที่สองของแม่ นับจากลูกคนสุดท้าย
ตัวอย่าง:
p:nth-last-of-type(2)
คำอธิบายตัวอย่าง:
เลือกทุกองค์ประกอบ <p> ที่เป็นองค์ประกอบ <p> ที่สองขององค์ประกอบหลัก นับจากลูกสุดท้าย
ตัวอย่าง:
p:nth-of-type(2)
คำอธิบายตัวอย่าง:
เลือกทุก <p> องค์ประกอบที่เป็นองค์ประกอบที่สอง <p> ของผู้ปกครอง
ตัวอย่าง:
p:only-of-type
คำอธิบายตัวอย่าง:
เลือกทุกองค์ประกอบ <p> ที่เป็นเพียงองค์ประกอบ <p> ของผู้ปกครอง
ตัวอย่าง:
p:only-child
คำอธิบายตัวอย่าง:
เลือกทุก <p> องค์ประกอบที่เป็นลูกเพียงคนเดียวของผู้ปกครอง
ตัวอย่าง:
input:optional
คำอธิบายตัวอย่าง:
เลือก <input> องค์ประกอบที่ไม่มีแอตทริบิวต์ "required"
ตัวอย่าง:
input:out-of-range
คำอธิบายตัวอย่าง:
เลือก <input> องค์ประกอบที่มีค่าอยู่นอกช่วงที่ระบุ
ตัวอย่าง:
input:read-only
คำอธิบายตัวอย่าง:
เลือก <input> องค์ประกอบที่มีแอตทริบิวต์ "อ่านอย่างเดียว" ที่ระบุ
ตัวอย่าง:
input:read-write
คำอธิบายตัวอย่าง:
เลือก <input> องค์ประกอบที่ไม่มีแอตทริบิวต์ "อ่านอย่างเดียว"
ตัวอย่าง:
input:required
คำอธิบายตัวอย่าง:
เลือก <input> องค์ประกอบที่มีแอตทริบิวต์ "required" ที่ระบุ
ตัวอย่าง:
root
คำอธิบายตัวอย่าง:
เลือกองค์ประกอบรากของเอกสาร
ตัวอย่าง:
#news:target
คำอธิบายตัวอย่าง:
เลือกองค์ประกอบ #news ที่ใช้งานอยู่ในปัจจุบัน (คลิกที่ URL ที่มีชื่อจุดยึดนั้น)
ตัวอย่าง:
input:valid
คำอธิบายตัวอย่าง:
เลือกทั้งหมด <input> องค์ประกอบที่มีค่าที่ถูกต้อง
ตัวอย่าง:
a:visited
คำอธิบายตัวอย่าง:
เลือกลิงก์ที่เยี่ยมชมทั้งหมด
ตัวอย่าง:
p::after
คำอธิบายตัวอย่าง:
แทรกเนื้อหาหลังจากทุกๆ <p> องค์ประกอบ
ตัวอย่าง:
p::before
คำอธิบายตัวอย่าง:
แทรกเนื้อหาก่อนทุกๆ <p> องค์ประกอบ
ตัวอย่าง:
p::first-letter
คำอธิบายตัวอย่าง:
เลือกตัวอักษรตัวแรกของทุกๆ <p> องค์ประกอบ
ตัวอย่าง:
p::first-line
คำอธิบายตัวอย่าง:
เลือกบรรทัดแรกของทุกๆ <p> องค์ประกอบ
ตัวอย่าง:
::marker
คำอธิบายตัวอย่าง:
เลือกเครื่องหมายของรายการ
ตัวอย่าง:
p::selection
คำอธิบายตัวอย่าง:
เลือกส่วนขององค์ประกอบที่ผู้ใช้เลือก