Home > Web Front-end > Front-end Q&A > How to use selector to exclude elements in css

How to use selector to exclude elements in css

PHPz
Release: 2023-04-25 11:18:48
Original
1725 people have browsed it

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.

Introduction to selectors

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:

  • Tag selector: Select by the tag name of the HTML element, for example: p{color:red}
  • Class selector: Select through the class attribute of the HTML element, for example: .myclass{color:blue}
  • ID selector: Select through the class attribute of the HTML element ID attribute to select, for example: #myid{color:green}
  • Wildcard selector: select all HTML elements, for example: *{color:pink}
  • Attribute selector: Select through the attribute value of the HTML element, for example: [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.

Select to exclude

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:

  • :not pseudo-class
  • :first-child pseudo-class
  • :last -child pseudo-class
  • :only-child pseudo-class

Let’s introduce them one by one.

:not pseudo-class

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

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

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

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

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

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

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

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.

Actual Case

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

CSS code:

.menu li > a:not(.submenu > li > a){
  color: red;
}
Copy after login

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!

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