This time I will bring you the CSS descendant selector. What are the precautions when using the CSS descendant selector? Here is a practical case, let’s take a look.
The descendant selector is used to select all descendants of a tag, including children and grandchildren, etc.; while the descendant selector only selects the descendant tags of the specified parent (the first generation child elements of the specified tag element) .
The descendant selector uses an extra symbol (angle brackets > ) to indicate the relationship between two elements.
For example: body>h1 selects all the first-generation
tags in the tag.
Relationship diagram of HTML tag
##HTML code
<h1 id="body里面的h-标题">body里面的h1标题</h1> <p> </p><h2 id="p里面的h">p里面的h2</h2> <p> p里面的p标签,p标签里面有一个<strong><a>加粗的链接</a></strong> </p> <h2 id="body里面的h-标题">body里面的h2标题</h2>After understanding the above structure, you can try the following code: CSS code
body>h2 { color: orange; }
, and the other is inside
, so the above CSS code only works on the first
Label.
The following is a more interesting child selector
Select the first sub-tag. CSS codeh2:first-child { color: orange; }
tags in the web page, and then find them through the tag parent element, and then determine whether the tag is ranked first in its parent element.
Here because the first tag of is tag is ranked first in its parent element.
, the child element of is not affected by the style.
Because in
is the first child element of
, so the
in
becomes orange.
:last-child
This selector is similar to the :first-child selector, but it selects the last child of an element. CSS codeli:last-child { font-size: 2em; }
:only-child
Select the only child of an element. HTML code<p> </p><p>第一个p的p</p> <p> </p><p>第二个p的第一个p</p> <a>第二个p的第一个a</a>CSS code
p:only-child { color: orange; }
element. Since there are not only
elements in the second
HTML代码
-
a
- 1
- 2
- 3
- 4
- 5
- 6
- 7
这时的效果是
li:nth-child(odd) 的意思是找到
因为
- 的第一个元素是,第二个元素才是
- 。也就是说,第一个
- 的下标是2,所以第一个下标赋的样式是绿色的背景。
使用上面的方法可以让表格里的各行交替使用不同的样式特别简单。不过,:nth-child() 还有一些更妙更强大的用法。
可以给 :nth-child() 指定一个数字,精确选择某个子代。比如说要让第4个
- 的背景色改成橙色。
HTML代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
CSS代码
li:nth-child(4) { background: orange; }
同样的,这里的 li:nth-child(4) 的意思,是找到
- 标签的父元素,然后查找父元素的第4个子元素。
如果HTML代码是下面这样,CSS代码不变的情况下。
HTML代码
-
a
- 1
- 2
- 3
- 4
- 5
- 6
- 7
效果就会变成这样:
会选中第3个
- 。因为第3个
- 在其父元素里面是排第4的。
如果想每隔2个项目选中第三个项目,可以在数字后面加上字母n。
HTML代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 0
CSS代码
li:nth-child(3n) { background: orange; }
每当遇到3的整数倍的那个元素,就会应用规定的样式。
如果想从第二个子代元素开始算起,选取每隔2个元素的第三个子代元素。可以在3n后面加个2。
HTML代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 0
CSS代码
li:nth-child(3n+2) { background: orange; }
如果想从第5个开始算起,每隔2个元素的第三个子代元素更改样式。
CSS代码
li:nth-child(3n+5) { background: orange; }
如果想反向遍历,n前面的倍数就要修改成负数。
CSS代码
li:nth-child(-n+3) { background: orange; }
意思是:从列表的第三个条目算起,选取在此之前的每个条目。
如果想从第4个元素开始,往下选取所有元素。可以这样写:
CSS代码
li:nth-child(n+4) { background: orange; }
以下是子代选择符总览表
# I believe you have mastered the method after reading the case in this article. Please pay attention for more exciting things. Other related articles on php Chinese website! Recommended reading:
The above is the detailed content of CSS descendant selector. For more information, please follow other related articles on the PHP Chinese website!

Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.

Article discusses three methods to add CSS to HTML: inline, internal, and external. Each method's impact on website performance and suitability for beginners is analyzed.(159 characters)


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
