CSS pseudo-class and pseudo-elements

Source: Internet
Author: User

In addition to selecting elements based on the ID (#), Class (.), attributes ([]), the element selector for CSS has an important category, which is to select elements based on their special state. They are pseudo-classes and pseudo-elements. Unlike the ID selector, class selector, property selector, and derived selectors, and so on, these get elements from the HTML document hierarchy (DOM tree), the pseudo-class and pseudo-elements are predefined, independent of the document elements. The way they get the elements is not based on the element characteristics of the ID, class, attribute, but on the element that is in a special state (pseudo-Class), or the element's special content (pseudo-element). Of course, the representation of pseudo-and pseudo-elements is also distinguished using ":" (colon) with other selectors.

Pseudo class

The pseudo-class selection element is based on the state in which the current element is present, or the attributes that the element currently has, not the static flags of the element's ID, class, attributes, and so on. Because the state is dynamically changing, when an element reaches a certain state, it may get a pseudo-class style, and when the state changes, it loses the style. As you can see, its function is somewhat similar to class, but it is based on abstractions outside of the document, so it is called a pseudo class.

: Link

Pseudo-classes will be applied to links that have not been accessed, with the: visited mutex.

: hover

The pseudo-class will be applied to the element that has the mouse pointer hovering over it.

: Active

The pseudo-class will be applied to the element being activated, such as the clicked Link, the button being pressed, and so on.

: Visited

Pseudo-classes will be applied to links that have already been visited, with: link mutex.

The following example shows a demonstration of the above 4 pseudo-class effects:

[HTML]View PlainCopy
  1. <html>
  2. <head>
  3. <Meta content= "text/html"charset= "Utf-8" />
  4. <style type="Text/css">
  5. a:link {color: #FF0000}
  6. a:visited {color: #00FF00}
  7. a:hover {color: #FF00FF}
  8. a:active {color: #0000FF}
  9. </style>
  10. </head>
  11. <body>
  12. <ahrefahref="http://www.baidu.com"> Baidu </a>
  13. </body>
  14. </html>

It is important to note that in the definition of CSS, the same element: hover must be located in: Link,: Visited before it takes effect: active must be in: hover before it takes effect.

: Focus

The pseudo-class will be applied to the element that owns the keyboard input focus.

: First-child

The pseudo-class will be applied to the first occurrence of the element in the page.

: lang

The pseudo-class will be applied to the case with the specified lang for the element.

The following example shows the effect of the above 3 pseudo-classes:

[HTML]View PlainCopy
  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtdxhtml 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <html>
  3. <head>
  4. <Meta charset= "Utf-8"content= "text/html" />
  5. <title></title>
  6. <style type="Text/css">
  7. Div:first-child {
  8. Background: #0F0;
  9. }
  10. Textarea:focus {
  11. Background: #F00;
  12. }
  13. Div:lang (en) {
  14. Background: #00F;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div>
  20. <textarea></textarea>
  21. </div>
  22. <div>
  23. <textarea></textarea>
  24. </div>
  25. <div lang="zh">
  26. <textarea></textarea>
  27. </div>
  28. </body>
  29. </html>

Pseudo element

Unlike pseudo-class elements that are specific to a particular state, a pseudo-element operates on a particular content in an element that operates at a level deeper than a pseudo-class, and therefore is much less dynamic than a pseudo-class. In fact, the purpose of designing pseudo-elements is to select the first word (mother), the first line of the element content, and select the work that the ordinary selector cannot do before or after some content. The content that it controls is actually the same as the element, but it is simply an abstraction based on elements that does not exist in the document, so it is called a pseudo-element.

: First-letter

The style of the pseudo-element is applied to the first word (parent) of the element text.

: First-line

The style of the pseudo-element will be applied to the first line of the element text.

: Before

Adds new content to the front of the element's content.

: After

Adds new content to the last side of the element's content.

: Before and: After is usually used in conjunction with CSS content generation, the author will use in the next Article blog post.

CSS pseudo-class and pseudo-elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.