การใช้ CSS pseudo-class selector กล่าวคือ :active, :hover, :link และ :visited เราสามารถกำหนดรูปแบบสถานะต่างๆ ของลิงก์/จุดยึดได้ เพื่อการทำงานที่เหมาะสม ลำดับของตัวเลือกเหล่านี้ควรเป็น −
- :ลิงค์
- :เยี่ยมชมแล้ว
- :โฮเวอร์
- :ใช้งานอยู่
ไวยากรณ์ของคุณสมบัติ CSS psudo-class มีดังนี้ -
a:(pseudo-selector) {
/*declarations*/
} ตัวอย่าง
มาดูตัวอย่างการใช้ CSS Anchor Pseudo Classes -
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: flex;
float: left;
}
a {
margin: 20px;
padding: 10px;
border: 2px solid black;
text-shadow: -1px 0px black, 0px -1px black, 0px 1px black, 1px 0px black;
font-size: 1.1em;
}
a:link {
text-decoration: none;
}
a:visited {
color: blueviolet;
}
a:hover {
color: orange;
font-size: 150%;
font-weight: bold;
box-shadow: 0 0 5px 1px grey;
}
a:active {
color: red;
box-shadow: inset 0 0 15px red;
}
</style>
</head>
<body>
<div>
<h2>Your favourite sports?</h2>
<a href="#">Football</a>
<a href="">Cricket</a>
</div>
</body>
</html> ผลลัพธ์
เอาต์พุตเริ่มต้น -

เมื่อวางเมาส์เหนือลิงก์แรก เราจะได้ผลลัพธ์ดังต่อไปนี้

ในระหว่างการคลิกลิงก์แรก ผลลัพธ์ต่อไปนี้จะถูกส่งกลับ -

มาดูตัวอย่างอื่นของ CSS Anchor Pseudo Class −
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<title>CSS Anchor Pseudo Classes</title>
</head>
<style>
div {
color: #000;
padding:20px;
background-image: linear-gradient(135deg, grey 0%, white 100%);
text-align: center;
}
.anchor {
color: #FF8A00;
}
.anchor:last-child {
color: #03A9F4;
}
.anchor:visited {
color: #FEDC11;
}
.anchor:hover {
color: #C303C3;
}
.anchor:active {
color: #4CAF50;
}
</style>
<body>
<div>
<h1>Search Engines</h1>
<a class="anchor" href="https://www.google.com" target="_blank">Go To Google</a>
<a class="anchor" href="https://www.bing.com" target="_blank">Go To Bing</a>
</div>
</body>
</html> ผลลัพธ์
