Home > Article > Web Front-end > 30 types of CSS selectors you must remember_html/css_WEB-ITnose
You may have read about the `id`, `class` and `descendant` selectors, and are using them all, then you are making the mistake of selecting with a greater level of flexibility. Most of the selectors mentioned in this article are under the CSS3 standard, so they can only take effect in the latest version of the corresponding browser. You should keep these in your smart mind.
1. *
Css code
Before we look at more advanced selectors, we should know about this well-known clearing selection device. The asterisk will select every element on the page. Many developers use it to clear `margin` and `padding`. Of course you can use this when practicing, but I don't recommend using it in a production environment. It adds a lot of unnecessary stuff to the browser.
`*` can also be used to select all child elements of an element.
Css code
It will select all elements under `#container`. Of course, I still don't recommend you to use it, if possible.
DEMO
Compatibility
2. #X
Css code
Use `# in the selector `You can use id to locate an element. Everyone usually uses it this way, and you still have to be very careful when using it.
You need to ask yourself: Do I have to assign an id to this element to locate it?
The `id` selector is very strict and you cannot reuse it. If possible, try using tag names first, new elements in HTML5, or pseudo-classes.
DEMO
Compatibility
3. . > color: red;
}
Chrome
Safari
Opera
The next commonly used one is the `descendant` selector. You can use this if you want to be more specific about locating elements. For example, what if you don't need to target all `a` elements, but only the `a` tag under the `li` tag? At this time you need to use the `descendant` selector.
Pro tip: If your selector looks like `X Y Z A B.error`, you are doing it wrong. Always remind yourself whether you really need to modify so many elements. DEMO
IE6
5. }
ul { margin-left: 0; }
Safari Opera
6. X:visited and X:link
Css代码
我们使用`:link`这个伪类来定位所有还没有被访问过的链接。
另外,我们也使用`:visited`来定位所有已经被访问过的链接。
DEMO
兼容性
7. X+Y
Css代码
这个叫相邻选择器。它指挥选中指定元素的直接后继元素。上面那个例子就是选中了所有`ul`标签后面的第一段,并将它们的颜色都设置为红色。
DEMO
兼容性
8. X>Y
Css代码
`X Y`和`X > Y`的差别就是后面这个指挥选择它的直接子元素。看下面的例子:
Css代码
`#container > ul` will only select all direct `ul` elements under the `div` with `id` as 'container'. It will not locate the `ul` element under the first `li`.
For some reasons, using child nodes to combine selectors has many performance advantages. In fact, this is strongly recommended when using `css` selectors in JavaScript.
DEMO
Compatibility
9.
}
Firefox
Chrome
Safari
This is called an attribute selector. In the above example, only The element with the title attribute. Anchor tags that do not have this attribute will not be modified by this code. Then think again, what if you want to filter more specifically? That... DEMO
IE7
11.
a[href="http://strongme.cn"] {
color: #1f6053; /* nettuts green */
Compatibility
Firefox
Chrome
}
Tada, it’s us Required, in this way, it is specified that the value of `strongme` must appear in the `href` attribute of the anchor tag, whether it is `strongme.cn`, `strongme.com` or `www.strongme.cn`, it can be Selected.
But remember this is a very broad expression. If the anchor tag points to a site other than `strongme` related, if you want more specific restrictions, use `^` and `$` to indicate the beginning and end of the string respectively.
DEMO
Compatibility
Opera
background: url(path/to/external/icon.png) no-repeat;
padding-left: 10px;
You must have been curious. Some sites have an external link icon next to their anchor tags. I believe you must have seen this happen. Such a design will clearly tell you that you will be redirected to another website.
It can be easily done using the carat symbol. It is usually used in regular expressions to identify the beginning. If we want to locate the tag starting with `http` in the anchor attribute `href`, then we can use code similar to the above.
Note that we did not search for http://, that is not necessary because it does not contain https://.
What if we want to find all anchor tags pointing to an image? Then let's use the `&` character.
DEMO
Compatibility
14. a[href$=".jpg"] {
color: red;
}
Firefox
Chrome
Safari
Css code
color: red ;
}
Compatibility
IE7
16. >
Css code
border: 1px solid black; }
I think this one will make your friends exclaim how wonderful it is. Very few people know this trick. The `~` symbol can locate tags whose attribute values are multiple values separated by spaces. Continuing with the example from item 15, we can set a `data-info` attribute, which can be used to set any space-separated value we need. For this example we will indicate them as external links and image links.
17. X:checked
Css code
The above pseudo-class writing method can locate the selected radio and multi-select boxes. It is that simple.
DEMO
Compatibility
18. X:after
The two pseudo-classes `before` and `after`. It seems like every day everyone finds creative ways to use them. They generate some content around the selected tag.
When using the `.clear-fix` technique many properties are used for the first time.
Css code
Want to see other creative uses of this pseudo-class, see [here](http://net.tutsplus.com/tutorials/html-css-techniques/quick-tip-getting-clever-with-css3-shadows /).
According to the CSS3 standard, two colons `::` can be used. Then for compatibility, the browser will also accept a double quotation mark. In fact, in this case, it is more sensible to use a colon.
Compatibility
IE8
19.
background: #e3e3e3;
Needless to say, everyone must know it. The official saying is `user action pseudo class`. It sounds a bit confusing, but it's actually okay. If you want to put some color on the place where the user's mouse hovers, then this pseudo-class writing method can do it.
a:hover {
border-bottom: 1px solid black;
Expert tip: border-bottom:1px solid black; looks much better than text-decoration:underline;.
Safari
Opera
Css code
We can use `::` to select part of a tag, such as a paragraph or the first word. But remember that it must be used on block tags to work.
The pseudo tag is composed of two colons::.
Positioning the first letter
The above code will find all paragraphs on the page and specify them as the first of each paragraph Character.
It is usually used to highlight the key points of the content of some news newspapers.
Position the first line of a paragraph
Css code
followed by `::first-line `Similarly, the first line of the paragraph will be selected.
For compatibility, older browsers were also compatible with single colon writing, such as `:first-line`, `:first-letter`, `:before`, `:after`. But this one is compatible Has no effect on newly introduced features.
DEMO
Compatibility
22. > li:nth-child(3) {
color: red;
}
IE9
Firefox3.5
Chrome
Safari
Assume you are in an `ul` tag There are N more elements, and you only want to get the last three elements, even like `li:nth-child(397)`, you can use the `nth-last-child` pseudo-class to replace it.
This technique can accurately replace the 16th TIP. The difference is that it starts from the end and goes back.
DEMO
Compatibility
Opera
24. 🎜>
ul:nth-of-type(3) {
border: 1px solid black;
IE9
Firefox3.5
Chrome
Safari
Similarly, too You can similarly use `nth-last-of-type` to get tags in reverse order.
Compatibility
26. X:first-child
Css代码
这个结构性的伪类可以选择到第一个子标签,你会经常使用它来取出第一个和最后一个的边框。
假设有个列表,没个标签都有上下边框,那么效果就是第一个和最后一个就会看起来有点奇怪。这时候就可以使用这个伪类来处理这种情况了。
DEMO
兼容性
27. X:last-child
Css代码
跟`first-child`相反,`last-child`取的是父标签的最后一个标签。
例如
标签
Css代码
这里没啥内容,就是一个了 List。
Css代码
上面的代码将设置背景色,移除浏览器默认的内边距,为每个`li`设置边框以凸显一定的深度。
DEMO
兼容性
28. X:only-child
Css代码
说实话,你会发现你几乎都不会用到这个伪类。然而,它是可用的,有会需要它的。
它允许你获取到那些只有一个子标签的父标签。就像上面那段代码,只有一个段落标签的`div`才被着色。
Css代码
My paragraph here.
Two paragraphs total.
Two paragraphs total.
上面例子中,第二个`div`不会被选中。一旦第一个`div`有了多个子段落,那这个就不再起作用了。
DEMO
兼容性
29. X:only-of-type
Css代码
结构性伪类可以用的很聪明。它会定位某标签只有一个子标签的目标。设想你想获取到只有一个子标签的`ul`标签?
使用`ul li`会选中所有`li`标签。这时候就要使用`only-of-type`了。
Css代码
DEMO
兼容性
30. X:first-of-type `first-of-type`伪类可以选择指定标签的第一个兄弟标签。
测试
Css代码
My paragraph here.
Come and take out List Item 2. If you have already taken it out or gave up, continue.
Solution 1
There are many ways, let’s look at some of the more convenient ones. The first is `first-of-type`.
Css code
Find the first `ul` tag, then find the direct child tag `li`, then find the second child node.
Solution 2
Another solution is the proximity selector.
In this case, find the immediate `ul` tag under `p`, and then find its last direct child tag.
Solution 3
We can play around with these selectors. Let’s take a look:
Css code
First get the first `ul` tag on the page, and then find the last sub-child Label.
DEMO
Compatibility
Conclusion
If you want to compromise with older browsers, such as IE6, you still have to be careful when using these new selectors . But don't let IE6 stop you from learning these new skills. Then you are being cruel to yourself. Remember to check the [compatibility list](http://www.quirksmode.org/css/contents.html), or use [Dean Edward's excellent IE9.js script](http://code.google.com/p /ie7-js/) to enable your browser to have these features.
Second, when using jQuery, try to use native CSS3 selectors. Maybe it will make your code run faster. This allows the selector engine to use the browser's native parser instead of the selector's own.