Home > Article > Web Front-end > Summary of the latest 30 commonly used selectors in CSS3 (suitable for beginners)
*: Universal element selector
* { margin: 0; padding: 0; }
*The selector selects all elements on the page. The function of the above code is to set the margin and padding of all elements to 0. It is the most basic method to clear the default CSS style
* Selectors can also be applied to sub-selectors, such as the following code:
#container * { border: 1px solid black; }
In this way, all child tag elements with the ID container are selected and the border is set.
2
#ID:ID Selector
#container { width: 960px; margin: auto; }
The ID selector is the most efficient selector in CSS. When using it, you must ensure the uniqueness of the ID.
3
.class: class selector
.error { color: red; }
Class selectors are less efficient than ID selectors. A page can have multiple classes, and classes can be used in different tags.
4
X Y: tag combination selector
li a { text-decoration: none; }
The tag combination selector is also a commonly used selector.
5
X: Tag Selector
a { color: red; } ul { margin-left: 0; }
If you just want to change the style of a certain tag on the page, you can choose to use the tag selector.
6
X:visited and X:link
a:link { color: red; } a:visted { color: purple; }
Pseudo-class selector, the most commonly used is the A tag
7
X + Y:毗邻元素选择器
ul + p { color: red; }
毗邻元素选择器,匹配的是所有紧随X元素之后的同级元素Y
8
X > Y:子元素选择器
div#container > ul { border: 1px solid black; }
匹配#container下的所有子元素。
关于X>Y和X Y的区别请看下面的html实例:
The selector #container > ul will only match the first UL, which is the child element UL of #container, not the ul in li, but div ul can match the ul in all DIVs.
9
X ~ Y:
ul ~ p { color: red; }
Matches any sibling P element after the X element. That is, all elements of the same level after UL are selected.
10
X[title]: attribute selector
a[title] { color: green; }
Matches tags with a certain attribute. For example, in the example, it matches the a tag with the title attribute.
11
X[href="foo"]
a[href="http://js8.in"] { color: #1f6053; /* nettuts green */ }
also belongs to the attribute selector, matching tags with a certain value in the attribute. For example, the a tag matching href="http://js8.in" in the example is not selected, but the a tags of other links are not selected.
12
X[href*="nettuts"]
a[href*="tuts"] { color: #1f6053; /* nettuts green */ }
belongs to the attribute selector and matches all tags containing tuts in href. Regular matching
13
X[href^="http"]
a[href^="http"] { background: url(path/to/external/icon.png) no-repeat; padding-left: 10px; }
Similar to the zodiac sign selection tag above, but matches A tags starting with http, regular matching
14
X[href$=".jpg"]
a[href$=".jpg"] { color: red; }
Match tags ending with .jpg in attributes, regular matching, and also a type of attribute selector
15
X[data-*="foo"]
If you want to match all image links, you can do it with the following CSS:
a[href$=".jpg"], a[href$=".jpeg"], a[href$=".png"], a[href$=".gif"] { color: red; }
But if we add a data-filetype attribute to the a tag, we can use the following CSS to quickly select the tag we need to match.