Home >Web Front-end >CSS Tutorial >What are pseudo-classes in CSS? Give examples (e.g., :hover, :active, :focus, :visited, :first-child, :last-child).
Pseudo-classes in CSS are keywords that allow you to style elements not only based on their content or attributes but also on their state or position within a document. They are used to define special states of an element and can be used to enhance user interactivity and layout customization. Here are some examples of commonly used pseudo-classes:
:hover: Applies styles when a user hovers over an element with the mouse cursor. Example usage:
<code class="css">button:hover { background-color: #ccc; }</code>
:active: Applies styles when an element is being activated (e.g., clicked on by the user). Example usage:
<code class="css">button:active { background-color: #aaa; }</code>
:focus: Applies styles when an element has received focus, typically through keyboard navigation or being clicked. Example usage:
<code class="css">input:focus { border-color: blue; }</code>
:visited: Applies styles to links that the user has visited. Example usage:
<code class="css">a:visited { color: purple; }</code>
:first-child: Applies styles to the first child of its parent. Example usage:
<code class="css">ul li:first-child { font-weight: bold; }</code>
:last-child: Applies styles to the last child of its parent. Example usage:
<code class="css">ul li:last-child { color: red; }</code>
These pseudo-classes help to create more dynamic and responsive user interfaces by allowing developers to manipulate the appearance of elements based on user interaction and document structure.
Pseudo-classes can significantly enhance user interaction on a website in several ways:
:hover
and :focus
can provide visual feedback to users, indicating interactive elements like buttons or links. For instance, changing the color or adding a border when a user hovers over a button helps indicate it's clickable, improving the user experience.:focus
pseudo-class can be used to highlight the currently focused element, which is especially important for keyboard users and those using assistive technologies. This can help users navigate the site more effectively.:first-child
, :last-child
, and others, developers can style elements based on their position in the document, which can create more organized and visually appealing layouts. This can help draw attention to key content or provide better context for the information being displayed.:active
pseudo-class can provide immediate feedback during user interactions, such as when clicking a button. This can reinforce that an action has been initiated, enhancing the interactive feel of the website.:visited
pseudo-class allows developers to style links differently based on whether the user has visited them before, which can help users remember where they've been and navigate more efficiently.Overall, pseudo-classes contribute to creating a more interactive, responsive, and user-friendly website by leveraging different states and positions of elements.
The :hover
and :active
pseudo-classes in CSS are both used to style elements based on user interaction, but they apply to different interaction states and are used for different purposes:
:hover:
Example usage:
<code class="css">a:hover { color: #ff0000; }</code>
:active:
Example usage:
<code class="css">button:active { background-color: #0000ff; }</code>
While both pseudo-classes can be used together to create a more detailed interaction experience, :hover
provides feedback on potential interaction, whereas :active
indicates that an action is currently happening. For instance, you might see a button change color when you hover over it, then darken further when you click on it before you release the mouse button.
Pseudo-classes can be used effectively to style elements based on their position within a list, providing unique styling to certain elements based on their ordinal placement. Here’s how you can use some common pseudo-classes for this purpose:
:first-child: Applies styles to the first element in a list.
<code class="css">ul li:first-child { background-color: yellow; }</code>
This will make the background of the first list item yellow.
:last-child: Applies styles to the last element in a list.
<code class="css">ul li:last-child { color: blue; }</code>
This will change the text color of the last list item to blue.
:nth-child(n): Applies styles to elements at a specific position in the list. The n
can be a number, a formula, or a keyword.
To style the third item:
<code class="css">ul li:nth-child(3) { font-weight: bold; }</code>
To style every other item starting from the second:
<code class="css">ul li:nth-child(2n) { background-color: #eee; }</code>
To style the first three items:
<code class="css">ul li:nth-child(-n 3) { font-style: italic; }</code>
:nth-last-child(n): Similar to :nth-child
, but counts from the last element in the list.
To style the second-to-last item:
<code class="css">ul li:nth-last-child(2) { border: 1px solid red; }</code>
These pseudo-classes allow you to create visually distinct and organized lists, which can be particularly useful for drawing attention to certain items, highlighting patterns, or providing a more structured layout for users.
The above is the detailed content of What are pseudo-classes in CSS? Give examples (e.g., :hover, :active, :focus, :visited, :first-child, :last-child).. For more information, please follow other related articles on the PHP Chinese website!