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

不言
Release: 2018-11-02 10:08:08
Original
3193 people have browsed it

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;}
Copy after login

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!

    

Click on this link!

Hover over this

Enter todays date in the field below

Date:
Copy after login

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 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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!