Table of Contents
2.1. Label selector
2.1.1 Universal selection*
2.1.3 Multi-label
2.2. Attribute selector
2.2.1 Special 1: id selector
2.2.2 Special 2: class selector
2.2.3 Attribute selector
3.1.1 UI pseudo-classes
 
3.2. Pseudo-element
Home Web Front-end HTML Tutorial How much do you know about css (5)--selector_html/css_WEB-ITnose

How much do you know about css (5)--selector_html/css_WEB-ITnose

Jun 24, 2016 am 11:49 AM

1. Introduction

Starting from this section, we will enter the second part of this series - the combination of css and html - to put it bluntly, it is the selector.

Styles are defined in CSS. How to set these styles to the corresponding html nodes? You have to pass the selector. Let the browser know which DOM node is selected by CSS, and the browser will obediently render the corresponding style into the view.

As for how CSS can render the page, this is the third part of this series.

The first part talks about the loading and cascading of CSS styles, the second part talks about selectors and selector levels, and the third part talks about the various styles presented (background, font, positioning, floating, etc.). Such an idea, which also formalizes the process of browsers using CSS, is the most effective learning idea. (Told in Section 2, review here again)

2. Selector

When it comes to CSS selectors, everyone knows that there are many kinds, but if you really want to count them on your fingers, you It may take several minutes. In fact, so many selectors can be divided into two categories:

  1. Tag selector (* is a special case), which can be single tag or contextual multiple tags;
  2. attribute selection (id and class are attributes, special attributes);

2.1. Label selector

2.1.1 Universal selection*

Universal selector* Everyone should be familiar with it. The most commonly used one is *{margin:0; box-sizing:border-box;}. mragin:0 As we have said in the previous section, box-sizing: border-box will be described in detail later in the box model.

For example, when we detect the style of bootstrap3, we can also see that it uses the * selector:

 

2.1.2 Single tag

Single tag selector is the most basic CSS knowledge. In the browser default style in the previous section, single tag selector is used everywhere. I won’t go into details here. Friends who have a weak foundation in CSS can take supplementary courses first.

2.1.3 Multi-label

Multi-label selector is generally related to html context. It has the following centralized classification

  1. Select an ancestor All descendant nodes of div p{…}
  2. Select all direct nodes of a parent element, such as div > p{…}
  3. Select the sibling nodes next to an element , for example li li{…}
  4. Select all sibling nodes of a certain element, for example span ~ a{…}
  5. Combination application of the above situations (don’t make the combination too complicated, the coding should be careful Readability first)

Let me list a typical application, as shown below

The effect in the above picture should be It's more common to put underlines between menus. My previous implementation was: add a border-bottom to each li, and then remove the border-bottom of the last li.

In fact, there is no need to go to such trouble. The following style setting can solve the problem:



 A bit interesting, right?

2.2. Attribute selector

2.2.1 Special 1: id selector

Basic knowledge, no more details.

According to many CSS tutorials, id selectors and attribute selectors are two different categories. Why should the id selector be placed under the attribute selector? Because the css selector is set based on the characteristics of the html node, the id is also an attribute, but it is a special attribute. The id of each html node cannot be repeated.

Because it is special and commonly used, the id selector is given a separate "#", which is essentially an attribute selector. The execution effect of the following two lines of code is exactly the same:

2.2.2 Special 2: class selector

Basic knowledge, no need to repeat it.

Class is also a special attribute. The reason why it is placed under the attribute selector is the same as the id mentioned above.

2.2.3 Attribute selector

There are two cases of attribute selector:

  1. Select only by attribute name: img[title | It has been expanded on in detail. If you don’t understand it, you can check out the basic tutorial to make up for it.
  2. 3. Pseudo-classes and pseudo-elements
  3. Several selector types are mentioned above, and pseudo-classes and pseudo-elements can be used for any selector.
3.1. Pseudo-classes

Pseudo-classes are divided into UI pseudo-classes and structured pseudo-classes.

3.1.1 UI pseudo-classes

UI pseudo-classes are relatively simple and commonly used. I will simply write a few lines of code below and will not go into details.

 

3.1.2 Structured pseudo-classes (lower versions of IE will not work)

Let me ask you a question first: How to implement a table interval display background Color, as shown in the picture:

 

The simplest way is to use structured pseudo-classes, which can be achieved with just one style setting.

 

The structured pseudo-class has the following writing options. As for what it means, you can understand it literally:

 

3.2. Pseudo-element

Remember, pseudo-element This is a very important concept! Among them, :before and :after are very commonly used.

First, let’s take a look at what :before and :after are about.

 

In the picture above, we can see that we can add content before and after elements. The "content" here can also be written in unicode encoding, as shown below:

In addition, in addition to adding content, you can also customize the execution content style, as shown below:

​ The above gives a general understanding of the basic usage of the two. Here are two typical application scenarios:

​ First, everyone knows FontAwesome, the most popular icon font library on the web. The application of these small icons is realized through pseudo elements, as shown below:

 

 (It doesn’t matter if you don’t know fontAwesome, we will introduce it in detail when we talk about css fonts)

Second, everyone knows about the clear floating style, right? This is a very typical pseudo-element application scenario:

​ (when talking about css floating, clearfix will be explained specifically)

4. Selector expansion

Selector Originally a rule of CSS, used to select html nodes for CSS. But smart humans have still created great works in other fields through selectors.

  • jQuery
  • The fundamental reason why jQuery is promoted and popular is its “Query”?? “Query” based on css selector. Today's browsers support the querySelectAll() method. In fact, this is the jQuery design "plagiarized" by the W3C.

    I believe that all web front-end personnel are familiar with jquery, just click here.

  • zen-Coding
  • jquery can select the dom group that matches the expression from the existing html structure through a css selector expression, but zen-coding reverses The way to go is to create html nodes based on css selector expressions. I have to admire the creativity of these people.

       

      If you haven’t used zen-coding, whether you will use it in the future or not, I suggest you try it!

    5. Summary

    This section summarizes the knowledge of CSS selectors and knows that there are several types of selectors. But having too many types is not necessarily a good thing. The next section will tell you about a problem caused by too many types and its solution.

    ----------------------------------------------- -------------------------------------------------- ---------------

    Welcome to follow my tutorial: "From Design to Pattern" " In-depth understanding of javascript prototypes and closures Series 》《Microsoft petshop4.0 source code interpretation video》《json2.js source code interpretation video》

    You are also welcome to follow my open source project??wangEditor, a simple and easy-to-use web rich text editor Device

    ---------------------------------------- -------------------------------------------------- ------------------

    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

    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

    Video Face Swap

    Video Face Swap

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

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

    HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

    AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

    Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

    GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

    How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

    The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

    How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

    To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

    HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

    HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

    See all articles