Commonly used selectors

Source: Internet
Author: User

1. Type selector

Used to look for specific types of elements, generally referred to in CSS are commonly defined and widely used elements, such as paragraph, title, body, etc.

< is also called an element selector or a simple selector >

P{font-size:16px;color:black;}
2. Descendant selector

used to look for descendants of a particular element or group of elements, denoted by a space in the middle

Body p{font-size:14px;}
3. ID selector

Used to find the element with the specified ID, denoted by the character #

#intro {font-weight:bold;}
<p id= "Intro" >hello world!</p>
4. Class selector

Used to look for an element with the specified class name, by the point number.

. Date-posted{color: #ccc;}
<p class= "date-posted" >12/12/2012</p>
5. Pseudo-Class selector

apply a style to a particular state of the target element

Common:: link,:visited (link pseudo-class, can only be applied to anchor elements)

: Hover,:active,:focus (Dynamic Pseudo-class, theoretically applicable to any element)

A:link{color:blue;text-decoration:none;}
A:visited{color:grey;}

a:hover,:active,:focus{color:red;}
6. Universal Selector

Matches all available elements, represented by *

*{padding:0;margin:0;}

7. Advanced Selector

1) Sub-selector

Select only the immediate descendants of the element, that is, the child element, the definition symbol is >

Body>p{color:green;}
<body>
<p> This paragraph is green </p>
<div><p> This paragraph is not green </p></div>
<p> This paragraph is green </p>
</body>

2) adjacent sibling selector (adjacent selector)
Locates the element after an element under the same parent element, the definition symbol is +
h2+p{color:red;}


<p> is red </p>
<p> not red </p>

<div><p> not red </p></div>
<p> not red </p>

It's not red.
<p> Red </p>
<p> not Red </p>

3) Property Selector
Looking for an element based on whether a property exists or the value of the property, defined by writing the property and value in []
a[name]{color:red}/* Select the A element with the name attribute */
[Special]/* Select any element with the Special property */
img[alt= "So"][class= "pic"]{margin:20px;}/* Match two properties and values simultaneously */
[special~= "Wo"] {color:red;}

Common selectors

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.