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
- * {
- margin: 0;
- padding: 0;
- }
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
- #container * {
- border: 1px solid black;
- }
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
- #container {
- width: 960px;
- margin: auto;
- }
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;
}
- This is a `class` selector. It differs from the `id` selector in that it can target multiple elements. You can use `class` when you want to style multiple elements. When you want to modify a specific element, use `id` to locate it.
- DEMO Compatibility
- IE6
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
- Firefox
- Chrome
- Safari
- Opera
5. }
ul { margin-left: 0; }
Safari Opera
6. X:visited and X:link
Css代码
- a:link {color:red;}
- a:visited {color: purple;}
我们使用`:link`这个伪类来定位所有还没有被访问过的链接。
另外,我们也使用`:visited`来定位所有已经被访问过的链接。
DEMO
兼容性
7. X+Y
Css代码
- ul + p {
- color: red;
- }
这个叫相邻选择器。它指挥选中指定元素的直接后继元素。上面那个例子就是选中了所有`ul`标签后面的第一段,并将它们的颜色都设置为红色。
DEMO
兼容性
8. X>Y
Css代码
- div#container > ul {
- border: 1px solid black;
- }
`X Y`和`X > Y`的差别就是后面这个指挥选择它的直接子元素。看下面的例子:
Css代码
-
- List Item
- Child
- List Item
- List Item
- List Item
`#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.
}
- The sibling node combination selector is very similar to `X Y`, but it is not that different strict. The `ul p` selector only selects those elements that immediately follow the specified element. This selector will select all matching elements following the target element. DEMO
- Compatibility
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
- Firefox
- Chrome
- Safari
- Opera
11.
a[href="http://strongme.cn"] {
color: #1f6053; /* nettuts green */
Compatibility
Firefox
Chrome
- Safari
- Opera
- 12. 🎜>a[href*="strongme"] {
}
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 - IE7
- Firefox
- Chrome
- Safari
- Opera
- This time we have another The regular expression `$` is used to indicate the end of the string. The meaning of this code is to search for all image links, or other links ending with `.jpg`. But remember that this way of writing will not work on `gifs` and `pngs`.
- DEMO Compatibility
- Opera
- 15. a[data-filetype="image"] {
- color: red;
- }
- a[href$=".jpg"],
- a[href$ =".jpeg"],
- a[href$=".png"],
- [html] Image Link [/html]With this hook, we can use the standard method to select only the anchor points whose file type is `image`.
- Css code
- a[data-filetype="image"] {
- color: red;
- Firefox
- Chrome
- Safari
- a[data-info~="external"] {
- color: red;
- }
- a[data-info~="image"] {
- Css code
- Click Me, Fool
- After setting this flag to these elements, we can use `~` to locate these tags.
- /* Target data-info attr that contains the value "external" */
- a[data-info~="external"] {
- color: red;
- }
- /* And which contains the value "image" */
- a[data-info~="image"] {
- border: 1px solid black;
- }
- input[type=radio]:checked {
- border: 1px solid black;
- }
- IE9
- Firefox
- Chrome
- Safari
- Opera
- .clearfix:after {
- content: "";
- display: block;
- clear: both;
- visibility: hidden;
- font-size: 0;
- height: 0; }
- .clearfix {
- *display: inline-block;
- _height: 1%;
- }
- Firefox
- Chrome
- Safari
- Opera
- Note that older versions of IE will only work with the `:hover` pseudo-class added to the anchor `a` tag.
- Usually people use it when adding a border when the mouse hovers over an anchor link.
- IE6 (IE6 only works on anchor tags)
- Firefox
- 20. >
- p::first-line {
- font-weight: bold;
- font-size:1.2em;
- }
- p::first-letter {
- float: left;
- font-size: 2em;
- font-weight: bold;
- font-family: cursive;
- padding-right: 2px;
- }
- p::first-line {
- font-weight: bold;
- font-size: 1.2em;
- }
- IE6
- Firefox
- Chrome
- Safari
- Opera
- Remember when we faced How to get the first element of the push-pull tag? It’s a time when you have nowhere to start. With `nth-child`, those days are gone forever.
- Please note that `nth-child` accepts an integer parameter, and it does not start from 0. If you want to get the second element then the value you pass is `li:nth-child(2)`.
- We can even get the number of child tags defined by the variable name. For example, we can use `li:nth-child(4n)` to get tags for every 3 elements.
- 23. nth-last-child(2) {
- color: red;
- }
- IE9
- Firefox3.5
- Chrome
- }
- Once upon a time, we didn't want to select child nodes, but wanted to select based on the type of element.
- Imagine there are 5 `ul` tags. If you only want to modify the third one, and you don’t want to use the `id` attribute, then you can use the `nth-of-type(n)` pseudo-class to achieve it. The above code only The third `ul` tag will have a border set.
- DEMO Compatibility
- 25. ul:nth-last-of-type(3) {
- border: 1px solid black;
- IE9+
- Firefox3.5+
- Chrome
- Safari
- Opera
- ul li:first-child {
- border-top: none;
- }
- IE7+
- Firefox
- Chrome
- Safari
- Opera
- ul > li:last-child {
- color: green;
- }
- List Item
- List Item
- List Item
- ul {
- width: 200px;
- background: #292929;
- color: white;
- list-style: none;
- padding-left: 0;
- }
- li {
- padding: 10px;
- border-bottom: 1px solid black;
- border-top: 1px solid #3c3c3c;
- }
- IE9+
- Firefox
- Chrome
- Safari
- Opera
- div p:only-child {
- color: red;
- }
-
My paragraph here.
-
Two paragraphs total.
Two paragraphs total.
- IE9+
- Firefox
- Chrome
- Safari
- Opera
- li:only-of-type {
- font-weight: bold;
- }
- ul > li:only-of-type {
- font-weight: bold;
- }
- IE9+
- Firefox 3.5+
- Chrome
- Safari
- Opera
-
My paragraph here.
- List Item 1
- List Item 2
- List Item 3
- List Item 4
- ul:first-of-type > li:nth-child(2) {
- font-weight: bold;
- }
- ul:first-of-type li:nth-last-child(1) {
- font-weight: bold;
- }
- IE9
- Firefox 3.5
- Chrome
- Safari
- Opera
14. a[href$=".jpg"] {
color: red;
}
Firefox
Chrome
Safari
Css code
color: red ;
}
- But it’s very painful to write like this, and the efficiency will be very low. Another way is to use custom attributes. We can add an attribute `data-filetype` to each anchor to specify the type of image pointed to by this link.
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;.
- Compatibility
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代码
上面例子中,第二个`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代码
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.

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
