Home > headlines > body text

A brief introduction to the usage summary of 30 CSS3 selectors

小云云
Release: 2017-12-19 11:49:33
Original
3254 people have browsed it

Several selectors we are more familiar with are: #id .class and tag selectors. But these are not enough. In order to be more convenient in development, this article summarizes 30 CSS3 selectors, hoping to be helpful to everyone.

1 *: Universal selector

* {   margin:0;   padding:0;  }
Copy after login

* 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, the most basic method to clear the browser's default style.

*Selectors can also be applied to sub-selectors, such as the following code:


#container * {   border:1px solid black;  }
Copy after login

In this way, all sub-tag elements with the ID container are selected It is selected and the border is set.

2 #id: id selector


#container {
   width: 960px;
   margin: auto;
}
Copy after login

The id selector is very strict and you cannot reuse it . 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?

3 .class: class selector


.error {
  color: red;
}
Copy after login

This is a class selector. It is different 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 the id to locate it.

4 selector1 selector2: descendant selector


li a {
  text-decoration: none;
}
Copy after login

The descendant selector is a more commonly used selector. You can use this if you want to be more specific about locating elements. For example, what if you don't need to locate all a elements, but only the a tag under the li tag? At this time you need to use descendant selectors.

Tip: If your selector looks like X Y Z A B.error, then you are wrong. Always remind yourself whether you really need to modify so many elements.

5 tagName: tag selector


a { color: red; }
ul { margin-left: 0; }
Copy after login

If you want to locate all tags on the page, not by id or 'class', this is simple, use the type selector directly.

6 selector:link selector:visited selector:hover selector:active Pseudo-class selector

Generally, the selector is a tag, and the above four pseudo-class selections The meaning of the device is as follows:

link: The normal state of the connection.

visited: After the connection has been visited.

hover: When the mouse is placed on the connection.

active: When the connection is pressed.

When the a label link is not moved: link

When the a label link is moved: link, hover

When the a label link is clicked: link, hover, active

After clicking and not moving into the a label connection: link, visited

After clicking and moving into the a label connection: link, visited, hover

After clicking and clicking the a label again when connecting: link, visited, hover, active

This is the style of all combinations.

If there are multiple identical styles, the later styles will overwrite the previous styles, so the definitions of these four pseudo-classes have order requirements, and it is also for this reason that everyone calls 'lvha' .

7 selector1 + selector2: Adjacent selector


ul + p {
   color: red;
}
Copy after login

It will only select elements that are the direct successor of the specified element. The example above selects the first paragraph after all ul tags and sets their color to red.

8 selector1 > selector2 : sub-selector


p#container > ul {
  border: 1px solid black;
}
Copy after login

The difference between it and it is that the latter command selects its direct child elements . Look at the example below


<p id="container">
   <ul>
      <li> List Item
        <ul>
           <li> Child </li>
        </ul>
      </li>
      <li> List Item </li>
      <li> List Item </li>
      <li> List Item </li>
   </ul>
</p>
Copy after login

#container > ul will only select all direct ul elements under p with the id of 'container'. It will not locate the ul element under the first li. For some reasons, there are a number of performance advantages to combining selectors with child nodes. In fact, this is highly recommended when using CSS selectors in JavaScript.

9 selector1 ~ selector2: sibling selector


ul ~ p {
  color: red;
}
Copy after login

The sibling node combination selector is very similar to the adjacent selector, and then It's not that strict. The ul + p selector will only select those elements that immediately follow the specified element. This selector will select all matching elements following the target element.

10 selector[title] : Attribute selector


a[title] {
  color: green;
}
Copy after login

In the above example, only those with the title attribute will be selected. element. Anchor tags that do not have this attribute will not be modified by this code.

11 selector[href="foo"] : Attribute selector


a[href="http://strongme.cn"] {
  color: #1f6053; /* nettuts green */
}
Copy after login

The above code will change the href attribute The anchor tag with the value http://strongme.cn is set to green, while other tags are unaffected.

Note: We enclose the value in double quotes. Then use double quotes when using Javascript. If possible, use standard CSS3 selectors.

12 selector[href*=”strongme”] : Attribute selector


a[href*="strongme"] {
  color: #1f6053;
}
Copy after login

指定了strongme这个值必须出现在锚点标签的href属性中,不管是strongme.cn还是strongme.com还是www.strongme.cn都可以被选中。

但是记得这是个很宽泛的表达方式。如果锚点标签指向的不是strongme相关的站点,如果要更加具体的限制的话,那就使用^和$,分别表示字符串的开始和结束。

13 selector[href^=”href”] : 属性选择器


a[href^="http"] {
   background: url(path/to/external/icon.png) no-repeat;
   padding-left: 10px;
}
Copy after login

大家肯定好奇过,有些站点的锚点标签旁边会有一个外链图标,我也相信大家肯定见过这种情况。这样的设计会很明确的告诉你会跳转到别的网站。

用克拉符号就可以轻易做到。它通常使用在正则表达式中标识开头。如果我们想定位锚点属性href中以http开头的标签,那我们就可以用与上面相似的代码。

注意我们没有搜索http://,那是没必要的,因为它都不包含https://。

14 selector[href$=”.jpg”] : 属性选择器


a[href$=".jpg"] {
  color: red;
}
Copy after login

这次我们又使用了正则表达式$,表示字符串的结尾处。这段代码的意思就是去搜索所有的图片链接,或者其它链接是以.jpg结尾的。但是记住这种写法是不会对gifs和pngs起作用的。

15 selector[data-*=”foo”] : 属性选择器


a[data-filetype="image"] {
   color: red;
}
Copy after login
Copy after login

回到上一条,我们如何把所有的图片类型都选中呢png,jpeg,’jpg’,’gif’?我们可以使用多选择器。看下面:


a[href$=".jpg"],
a[href$=".jpeg"],
a[href$=".png"],
a[href$=".gif"] {
   color: red;
}
Copy after login

但是这样写着很蛋疼啊,而且效率会很低。另外一个办法就是使用自定义属性。我们可以给每个锚点加个属性data-filetype指定这个链接指向的图片类型。


a[data-filetype="image"] {
   color: red;
}
Copy after login
Copy after login

16 selector[foo~=”bar”] : 属性选择器


a[data-info~="external"] {
   color: red;
}
 
a[data-info~="image"] {
   border: 1px solid black;
}
Copy after login

这个我想会让你的小伙伴惊呼妙极了。很少有人知道这个技巧。这个~符号可以定位那些某属性值是空格分隔多值的标签。

继续使用第15条那个例子,我们可以设置一个data-info属性,它可以用来设置任何我们需要的空格分隔的值。这个例子我们将指示它们为外部连接和图片链接。


<a href="path/to/image.jpg" data-info="external image"> Click Me, Fool </a>
Copy after login

给这些元素设置了这个标志之后,我们就可以使用~来定位这些标签了。


/* Target data-info attr that contains the value "external" */
a[data-info~="external"] {
   color: red;
}
 
/* And which contain the value "image" */
a[data-info~="image"] {
  border: 1px solid black;
}
Copy after login

17 selector:checked : 伪类选择器


input[type=radio]:checked {
   border: 1px solid black;
}
Copy after login

上面这个伪类写法可以定位那些被选中的单选框和多选框,就是这么简单。

18 selector:after : 伪类选择器

before和after这俩伪类。好像每天大家都能找到使用它们的创造性方法。它们会在被选中的标签周围生成一些内容。

当使用.clear-fix技巧时许多属性都是第一次被使用到里面的。


.clearfix:after {
    content: "";
    display: block;
    clear: both;
    visibility: hidden;
    font-size: 0;
    height: 0;
  }
 
.clearfix { 
   *display: inline-block; 
   _height: 1%;
}
Copy after login

上面这段代码会在目标标签后面补上一段空白,然后将它清除。这个方法你一定得放你的聚宝盆里面。特别是当overflow:hidden方法不顶用的时候,这招就特别管用了。

根据CSS3标准规定,可以使用两个冒号::。然后为了兼容性,浏览器也会接受一个冒号的写法。其实在这个情况下,用一个冒号还是比较明智的。

19 selector:hover : 伪类选择器


p:hover {
  background: #e3e3e3;
}
Copy after login

不用说,大家肯定知道它。官方的说法是user action pseudo class.听起来有点儿迷糊,其实还好。如果想在用户鼠标飘过的地方涂点儿彩,那这个伪类写法可以办到。

注意:旧版本的IE只会对加在锚点a标签上的:hover伪类起作用。

通常大家在鼠标飘过锚点链接时候加下边框的时候用到它。


a:hover {
 border-bottom: 1px solid black;
}
Copy after login

专家提示:border-bottom:1px solid black;比text-decoration:underline;要好看很多。

20 selector1:not(selector2) : 伪类选择器


p:not(#container) {
   color: blue;
}
Copy after login

取反伪类是相当有用的,假设我们要把除id为container之外的所有p标签都选中。那上面那么代码就可以做到。

或者说我想选中所有出段落标签之外的所有标签


:not(p) {
  color: green;
}
Copy after login

21 selector::pseudoElement : 伪类选择器


p::first-line {
  font-weight: bold;
  font-size:1.2em;
}
Copy after login

我们可以使用::来选中某标签的部分内容,如第一段,或者是第一个字。但是记得必须使用在块式标签上才起作用。

伪标签是由两个冒号 :: 组成的

定位第一个字:


p::first-letter {
   float: left;
   font-size: 2em;
   font-weight: bold;
   font-family: cursive;
   padding-right: 2px;
}
Copy after login

上面这段代码会找到页面上所有段落,并且指定为每一段的第一个字。

它通常在一些新闻报刊内容的重点突出会使用到。

定位某段的第一行


p::first-line {
   font-weight: bold;
   font-size: 1.2em;
}
Copy after login

跟::first-line相似,会选中段落的第一行

为了兼容性,之前旧版浏览器也会兼容单冒号的写法,例如:first-line,:first-letter,:before,:after.但是这个兼容对新介绍的特性不起作用。

22 selector:nth-child(n) : 伪类选择器


li:nth-child(3) {
   color: red;
}
Copy after login

还记得我们面对如何取到堆叠式标签的第几个元素时无处下手的时光么,有了nth-child那日子就一去不复返了。

请注意nth-child接受一个整形参数,然后它不是从0开始的。如果你想获取第二个元素那么你传的值就是li:nth-child(2).

我们甚至可以获取到由变量名定义的个数个子标签。例如我们可以用li:nth-child(4n)去每隔3个元素获取一次标签。

23 selector:nth-last-child(n) : 伪类选择器


li:nth-last-child(2) {
   color: red;
}
Copy after login

假设你在一个ul标签中有N多的元素,而你只想获取最后三个元素,甚至是这样li:nth-child(397),你可以用nth-last-child伪类去代替它。

24 selectorX:nth-of-type(n) : 伪类选择器


ul:nth-of-type(3) {
   border: 1px solid black;
}
Copy after login

曾几何时,我们不想去选择子节点,而是想根据元素的类型来进行选择。

想象一下有5个ul标签。如果你只想对其中的第三个进行修饰,而且你也不想使用id属性,那你就可以使用nth-of-type(n)伪类来实现了,上面的那个代码,只有第三个ul标签会被设置边框。

25 selector:nth-last-of-type(n) : 伪类选择器


ul:nth-last-of-type(3) {
   border: 1px solid black;
}
Copy after login

同样,也可以类似的使用nth-last-of-type来倒序的获取标签。

26 selector:first-child : 伪类选择器


ul li:first-child {
   border-top: none;
}
Copy after login

这个结构性的伪类可以选择到第一个子标签,你会经常使用它来取出第一个和最后一个的边框。

假设有个列表,每个标签都有上下边框,那么效果就是第一个和最后一个就会看起来有点奇怪。这时候就可以使用这个伪类来处理这种情况了。

27 selector:last-child : 伪类选择器


ul > li:last-child {
   color: green;
}
Copy after login

跟first-child相反,last-child取的是父标签的最后一个标签。

例如

标签


<ul>
   <li> List Item </li>
   <li> List Item </li>
   <li> List Item </li>
</ul>
Copy after login

这里没啥内容,就是一个了 List。


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;
}
Copy after login

上面的代码将设置背景色,移除浏览器默认的内边距,为每个li设置边框以凸显一定的深度。

28 selector:only-child : 伪类选择器


p p:only-child {
   color: red;
}
Copy after login

说实话,你会发现你几乎都不会用到这个伪类。然而,它是相当有用的,说不准哪天你就会用到它。

它允许你获取到那些只有一个子标签的父标签下的那个子标签。就像上面那段代码,只有一个段落标签的p才被着色。


<p><p> My paragraph here. </p></p>
<p>
   <p> Two paragraphs total. </p>
   <p> Two paragraphs total. </p>
</p>
Copy after login

上面例子中,第二个p不会被选中。一旦第一个p有了多个子段落,那这个就不再起作用了。

29 selector:only-of-type: 伪类选择器


li:only-of-type {
   font-weight: bold;
}
Copy after login

结构性伪类可以用的很聪明。它会定位某标签下相同子标签的只有一个的目标。设想你想获取到只有一个子标签的ul标签下的li标签呢?

使用ul li会选中所有li标签。这时候就要使用only-of-type了。


ul > li:only-of-type {
   font-weight: bold;
Copy after login

最后记住:使用像jQuery等工具的时候,尽量使用原生的CSS3选择器。可能会让你的代码跑的很快。这样选择器引擎就可以使用浏览器原生解析器,而不是选择器自己的。

相关推荐:

详解CSS选择器 Selector

如何用CSS3属性选择器替代JS的作用

css中关于选择器的使用总结

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 admin@php.cn
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!