search
HomeWeb Front-endCSS TutorialCSS selectors in detail

CSS selectors in detail

Feb 17, 2017 pm 01:15 PM
css selector

CSS3 basic selector

CSS selectors in detail

To better illustrate the problem, first create a simple DOM structure, as follows:

<div> 
    <ul> 
       <li>1</li> 
       <li>2</li> 
       <li>3</li> 
       <li>4</li> 
       <li>5</li> 
       <li>6</li> 
       <li>7</li> 
       <li>8</li> 
       <li>9</li> 
       <li>10</li> 
   </ul> 
</div>

1. Wildcard selector (*)
Wildcard selector is used to select all elements , you can also select all elements under a certain element. For example:

The code is as follows:
, the other is to select all elements under a certain element:

The code is as follows:

.demo * {border:1px solid blue;}

2. Element selector (E)
Element selector is css The most common and basic selector among selectors. Element selectors are actually elements of the document, such as html, body, p, div, etc. For example, in our demo: the elements include div, ul, li, etc.


The code is as follows:

li {background-color: gray;color: orange;}

3. Class selector (.className)

The class selector specifies the style in a way that is independent of the document element. Before using the class selector, you need to define the class name on the html element. In other words, you need to ensure that the class name exists in the html tag so that the class can be selected, such as:

  • 2
    In "active, important, items", we add a class name to li so that the class selector can work normally and better associate the style of the class selector with the element.

    .important {font-weight: bold; color: yellow;}
    The above code means to add a "font-weight, color: yellow" style to the element with the important class name;
    The class selector also It is possible to have multiple class names. As we saw above, there are two or more class names in our li element at the same time, and they are separated by spaces. Then the selector can also be connected together using multiple classes, such as:



    The code is as follows:

    .important {font-weight: bold;}

    .active {color: green;background: lime;}
    .items {color: #fff;background: #000;}
    .important.items { background:#ccc;}
    .first.last {color: blue;}

    As shown in the above code, the ".important.items" selector only selects elements that contain both "important" and "items" "Two classes can work. All browsers support class selectors, but multi-class selectors (.className1.className2) are not supported by IE6.

    5. Descendant selector (EF)

    The descendant selector is also called the inclusion selector. Its function is to select the descendant elements of a certain element, for example: E F. The preceding E is the ancestor element and F is the descendant. Element means that all descendant F elements of the E element are selected. Please note that they need to be separated by a space. Here, F will be selected regardless of whether it is a child element, a grandchild element, or a deeper relationship of the E element. In other words, no matter how many levels of relationships F has in E, it will be selected:

    .demo li { color: blue;}

    The above means that all li elements in div.demo are selected

    6. Child element selector (E>F)

    The child element selector can only select the child elements of a certain element. Among them, E is the parent element, and F is the child element. E>F means that all child elements F under the E element are selected. This is different from the descendant selector (EF), in which F is a descendant element of E, and the child element selector E > F, where F is only a child element of E.

    ul > li {background: green;color: yellow;}
    The code above indicates that all sub-elements li under ul are selected. For example:
    IE6 does not support child element selectors.

    7. Adjacent sibling element selector (E + F)

    Adjacent sibling selector can select elements immediately after another element, and they have the same parent element, in other words, EF The two elements have the same parent element, and the F element is behind the E element and adjacent, so we can use the adjacent sibling element selector to select the F element.



    The code is as follows:

    li + li {background: green;color: yellow; border: 1px solid #ccc;}

    The above code means selecting the adjacent element li of li. We have a total of ten li here , then the above code selects from the 2nd li to the 10th li, a total of nine

    IE6 does not support this selector

    8. Universal sibling selector (E~F)

    The universal sibling element selector is CSS3 A new selector is added. This selector will select all sibling elements behind an element. They are also similar to adjacent sibling elements and need to be in the same parent element. In other words, the E and F elements belong to the same Within the parent element, and the F element is after the E element, then the E ~ F selector will select the F elements after all the E elements. For example, the following code:



    The code is as follows:

    .active ~ li {background: green;color: yellow; border: 1px solid #ccc;}


    What the above code means is that all sibling elements li behind the li.active element are selected

    9. Group selector (selector1, selector2,...,selectorN)

    Group selector groups elements with the same style together. Each selector is separated by a comma ",", as above Shown are selector1,selector2,...,selectorN. This comma tells the browser that the rule contains multiple different selectors. If there is no this comma, the meaning expressed is completely different. Omitting the comma becomes the descendant selector we mentioned earlier. This is what everyone knows Be careful when using it.


    For more detailed introduction to CSS selectors and related articles, please pay attention to the PHP Chinese website!

  • Statement
    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
    Weekly Platform News: Web Apps in Galaxy Store, Tappable Stories, CSS SubgridWeekly Platform News: Web Apps in Galaxy Store, Tappable Stories, CSS SubgridApr 14, 2025 am 11:20 AM

    In this week's roundup: Firefox gains locksmith-like powers, Samsung's Galaxy Store starts supporting Progressive Web Apps, CSS Subgrid is shipping in Firefox

    Weekly Platform News: Internet Explorer Mode, Speed Report in Search Console, Restricting Notification PromptsWeekly Platform News: Internet Explorer Mode, Speed Report in Search Console, Restricting Notification PromptsApr 14, 2025 am 11:15 AM

    In this week's roundup: Internet Explorer finds its way into Edge, Google Search Console touts a new speed report, and Firefox gives Facebook's notification

    The Power (and Fun) of Scope with CSS Custom PropertiesThe Power (and Fun) of Scope with CSS Custom PropertiesApr 14, 2025 am 11:11 AM

    You’re probably already at least a little familiar with CSS variables. If not, here’s a two-second overview: they are really called custom properties, you set

    We Are ProgrammersWe Are ProgrammersApr 14, 2025 am 11:04 AM

    Building websites is programming. Writing HTML and CSS is programming. I am a programmer, and if you're here, reading CSS-Tricks, chances are you're a

    How Do You Remove Unused CSS From a Site?How Do You Remove Unused CSS From a Site?Apr 14, 2025 am 10:59 AM

    Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

    An Introduction to the Picture-in-Picture Web APIAn Introduction to the Picture-in-Picture Web APIApr 14, 2025 am 10:57 AM

    Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

    Ways to Organize and Prepare Images for a Blur-Up Effect Using GatsbyWays to Organize and Prepare Images for a Blur-Up Effect Using GatsbyApr 14, 2025 am 10:56 AM

    Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

    Oh Hey, Padding Percentage is Based on the Parent Element's WidthOh Hey, Padding Percentage is Based on the Parent Element's WidthApr 14, 2025 am 10:55 AM

    I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    DVWA

    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

    mPDF

    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),