Detailed explanation of several common CSS selector examples

伊谢尔伦
Release: 2017-05-30 13:36:11
Original
2048 people have browsed it

Methods to import external css style sheets

Use the link tag to import external css style sheets

Copy after login

Import (import) external style sheets in the style sheet

@import url("/crazy-html5/06css/css/demo01.css");
Copy after login

Use internal style sheet

The internal style sheet can only be applied to a certain web page. The definition method is to add the style tag in the head header. You can add it in the style tag. Page style.


    

Copy after login

Selector knowledge points

Element attribute selector

1) Ordinary tag selector

table:{background:red;}

2) A tag containing a certain attribute

p[id]{background:red;} represents a p element containing an id attribute

3) A tag that contains an attribute and the attribute value is a specific value

p[id=aaa]{background:red;} represents a p element that contains the id attribute and id=aaa

4) Elements that contain an attribute and the attribute value starts with a specific value

p[id=^aaa]{background:red;} means that it contains an id attribute, and the id value starts with aaa p element

5) An element that contains an attribute and the attribute value ends with a specific value

p[id=$c]{background:red;} means that it contains the id attribute, and the id The value is a p element ending in c

ID selector

id selector, specifying the element whose id is a specific value, for example, #myp means that the id is myp For elements with a value, the symbol

#Class selector must be added in front of the id selector. The class selector is an element that matches the class value. The symbol must be added in front of the selector. , for example, .myclass represents all elements whose class value is myclass.

Class selectors can be used in conjunction with element selectors. For example, p.important{color:red;} must meet the conditions of both selectors to take effect.

Contains selectors and descendant selectors

The descendant selector can select elements that are descendants of an element, for example: h1 em{color:red}, this rule will Turn the text of the em element that is a descendant of the h1 element into red. Other em text will not be affected by this rule.

Child selector

is similar to the descendant selector, but will only act on certain direct descendants of the element. For example, h1>strong{color:red;} acts on the first-level strong element in the h1 element, and other levels are not affected

Adjacent sibling selector

If you need to select an element immediately after another element, and both have the same parent element, you can use the adjacent sibling selector, such as h1+p{margin-top:50px;} to select the element immediately after h1 In the paragraph that appears, h1 and p elements have the same parent element

Selector grouping

Selectors that act on multiple elements at the same time, such as h2, p{color :gray;} will affect both the h2 element and the p element.

* is a wildcard selector that can match any element

Pseudo element selector

1): first-line{color:red; }Set a special style for the first line of text

2):first-letter{color:red;}Set a special style for the first letter of text

:after, :before no selector

:before{} can be used to insert content in front of the element content. The content can be specified by content.

:after{} can be used to insert content after the element content. The content can be specified by content, such as

p:before{
    content:url("img.png");
}
Copy after login

after and before can be used with quotes, quotes can be used with the quotation mark type for setting nested references

Copy after login
after, before can be used with counters

You can use a counter to add multi-level numbers in front of the text. This can be a special article and will not be detailed here.

Pseudo-class selector

1 :root selector matches the document root element

2 :first-child specifies when the element is its parent The style of the first child

3 :last-child specifies the style of the last child when the element is its parent

4 :nth-child(n) specifies the style when the element is The style of the nth child of its parent

When n is odd, it matches when the element is an odd-numbered child of its parent

When n is even, it matches when the element is an even number of its parent The style of the child

When n is m*n+p, match the style of the element whose parent conforms to the m*n+pth child

5 :nth-last-child (n) Specifies the style that takes effect when the element is the n-th child from the last of its parent

6 :only-child specifies the style that takes effect when the element is the only child element of its parent

7 :empty Specify the style of empty elements

Pseudo-class selector for element status

1 :hover Style when the mouse pointer is on the element

2 :focus Style of the element that gets focus

3 :enabledStyle of the enabled element

4 :disabledStyle of the disabled element

5 :checkedStyle of the selected element( checkbox,radio)

6 ::selection The style of some elements selected by the user

7 :not(selector) selects a style that is not this selector

8 :target selection The currently active #news element, generally used with anchor points

The above is the detailed content of Detailed explanation of several common CSS selector 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 [email protected]
Popular Tutorials
More>
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!