CSS is one of the important skills in front-end development. In addition to the basic selection of elements and modification of styles, selection and exclusion is also a very important knowledge point in CSS. This article will explain selection exclusions in CSS and provide practical cases for reference.
Before learning to select and exclude, let’s first understand the basics of CSS selectors.
CSS selector is used to select HTML elements. In CSS, there are many kinds of selectors, such as basic selectors, hierarchical selectors, pseudo-class selectors, etc. These selectors have different uses and syntax.
The following are several types of basic selectors:
p{color:red}
.myclass{color:blue}
#myid{color:green}
*{color:pink}
[href="http://www.example.com"]{color:purple}
Through the combination of the above basic selectors, we can accurately select HTML elements and modify their styles through CSS.
Sometimes we not only need to select elements, but also need to exclude some elements. At this time, selecting to exclude is very useful.
There are several ways to exclude selection in CSS:
Let’s introduce them one by one.
:not pseudo-class is used to exclude certain elements. Through simple syntax, we can exclude specified elements in the selector.
For example, we want to select all p
elements, but exclude elements with class="no"
in the p
elements, You can use the following syntax:
p:not(.no){color:blue}
This way you can accurately select all p
elements, but exclude elements with class="no"
and replace them The text font color is changed to blue.
:first-child pseudo-class indicates selecting the first child element, and can also be used to exclude certain elements.
For example, we want to select the first child element a
of all li
elements, but exclude elements with class="no"
Elements, you can use the following syntax:
li > a:first-child:not(.no){color:blue}
This way you can accurately select the first child element a
of all li
elements, but exclude the elements with class="no"
elements, change their text font color to blue.
:last-child pseudo-class indicates selecting the last child element, and can also be used to exclude certain elements.
For example, we want to select the last child element li
of all ul
elements, but exclude those with class="no"
element, you can use the following syntax:
ul > li:last-child:not(.no){color:blue}
This way you can accurately select the last child element li
of all ul
elements, but exclude elements with For elements with class="no"
, change their text font color to blue.
:only-child pseudo-class indicates selecting the only child element, and can also be used to exclude certain elements.
For example, we want to select all div
elements, but only the unique child element p
, and exclude elements with class="no"
elements, you can use the following syntax:
div > p:only-child:not(.no){color:blue}
This way you can accurately select the only child element p
of all div
elements, but exclude For elements with class="no"
, change their text font color to blue.
The following provides an actual case to show how to use selection exclusion.
Case description: There is a dynamically generated navigation bar menu, some of which have drop-down menus. Now it is necessary to change the font color of all menus to red, but exclude menus with drop-down menus and their drop-down menus. font color.
HTML code:
<ul class="menu"> <li><a href="#">首页</a></li> <li><a href="#">产品中心</a> <ul class="submenu"> <li><a href="#">产品1</a></li> <li><a href="#">产品2</a></li> <li><a href="#">产品3</a></li> </ul> </li> <li><a href="#">解决方案</a></li> <li><a href="#">关于我们</a> <ul class="submenu"> <li><a href="#">公司简介</a></li> <li><a href="#">联系我们</a></li> </ul> </li> </ul>
CSS code:
.menu li > a:not(.submenu > li > a){ color: red; }
Explanation: Through the :not()
pseudo-class, we can use The
li element of submenu
and its child elements submenu > li
are excluded, and only all li
elements and child elements a
are selected. And change its font color to red.
The above is the basic knowledge and practical application of selection exclusion. In actual development, selection exclusion is a very practical technique that can help us select HTML elements and modify their styles more efficiently.
The above is the detailed content of How to use selector to exclude elements in css. For more information, please follow other related articles on the PHP Chinese website!