Home  >  Article  >  Web Front-end  >  What are css pseudo-classes? A brief introduction to css pseudo-classes (with examples)

What are css pseudo-classes? A brief introduction to css pseudo-classes (with examples)

不言
不言Original
2018-11-02 10:08:083210browse

A CSS pseudo-class that allows you to assign a specific state to an element. For example, a user hovers over an image on a web page and it changes to another image. Links change color after being visited and can be distinguished from unvisited links. Input boxes on the form are disabled and have no values. Pseudo classes can implement these operations and more. Ordinary CSS classes can also be used with pseudo-classes.

CSS3 adds more pseudo-classes than before in CSS2. (Recommended course: css3 video tutorial)

The pseudo-class is added to the selector, preceded by a colon, followed by an attribute with a bracketed value. The syntax of the pseudo-class is as follows

selector: pseudo-class { property:value;}

Basic pseudo-class

There are many different pseudo-classes, and the following code example demonstrates some of the most basic pseudo-classes. These are classified as dynamic pseudo-classes, which change due to user actions. In the code example, the :link pseudo-class is used to set the link's color to blue. Once the user visits the link, the :visited pseudo-class is used to change the color of the link to maroon. Also demonstrated: When the user hovers over the image, the hover is used to change a purple circle with white text to a pink square with yellow text. The:Focus pseudo-class changes the date input field to gray when the user clicks or "focuses" on it. Code sample here Try it for yourself!

<!DOCTYPE html>
<html>
<head>
<style>
a:link{
   color:blue;
}
a:visited{
  color:maroon;
}
div {
   background-color: purple;
   color: white;
   line-height:90px;
   font-size:20px;
   text-align:center;
   width:200px;
   height:200px;
   -moz-border-radius: 100px;
   -webkit-border-radius: 100px;
    border-radius: 100px;
}
div:hover {
   background-color:pink;
   color:yellow;
   width:200px;
   height:200px;
   -moz-border-radius: 0px;
   -webkit-border-radius: 0px;
   border-radius: 0px;
}
input:focus {
  background-color: lightgray;
}
</style>
</head>
<body>
<p><b><a href="https://www.php.cn/" target="_blank">Click on this link!</a></b></p>
<div>Hover over this</div>
<p><b>Enter todays date in the field below</b></p>
<form>
Date: <input type="text" name="Date"><br>
</form>
</body>
</html>

Some more pseudo-classes

Some of the more commonly used pseudo-classes are listed below. A complete list of all pseudo-classes can be found here.

: first-child - This class allows you to add specific styles to an element that is the first child of its parent.

:root- This pseudo-class is used to match the root element of the document.

: lang- Different languages ​​have different rules and conventions. Sometimes you need to create pages that are available in multiple languages. The :lang pseudo-class is useful for this situation. It mainly looks at the lang="" HTML attributes and e8e496c15ba93d81f6ea4fe5f55a2244 elements, and matches the elements accordingly.

: disabled- This allows you to indicate disabled input elements such as text fields and buttons.

: fullscreen - This pseudo-class uses an element in full-screen mode.

Pseudo-elements

Pseudo-elements have similar functionality to pseudo-classes in that they can be styled by CSS and added by user agents. But they are different from pseudo-classes in that they allow you to use elements that do not exist in the DOM. The syntax identifier for a pseudo-element is usually a double colon, rather than a single colon like pseudo-classes. Common pseudo-elements are ::before , ::after and ::first-line . It's important to note that some coders use a single colon: for pseudo-elements, this is convention in CSS2. Keep this in mind to avoid confusion between pseudo-classes and pseudo-elements.

Finally

Pseudo-classes enable special states to be applied to elements. They allow developers to create cool effects without using scripting languages ​​like JavaScript. There are many different types of pseudo-classes that allow you to make your pages more interactive and interesting while keeping your code simple.

The above is the detailed content of What are css pseudo-classes? A brief introduction to css pseudo-classes (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn