Table of Contents
Verify again
::first-line vs. tag selector
::first-line vs class selector
::first-line vs ID selector
::first-line vs !important
One more question, an example of an MDN error? An interesting phenomenon
Home Web Front-end CSS Tutorial Check out these two CSS interview questions to test your foundation!

Check out these two CSS interview questions to test your foundation!

Sep 28, 2022 pm 07:45 PM
css

<p>Jianweizhizhu, this article will share with you two interesting CSS interview questions to test your foundation!

<p>Check out these two CSS interview questions to test your foundation!

<p>I saw such an interesting question in the forum today. The simple code is as follows:

1

2

3

<div>

    <p id="a">First Paragraph</p>

</div>

Copy after login
<p>The style is as follows:

1

2

3

4

5

6

p#a {

    color: green;

}

div::first-line {

    color: blue;

}

Copy after login
<p>I would like to ask, is the color of the text in the tag <p> green or blue?

<p>Interestingly, the final result here is blue, which means color: blue takes effect. [Recommended learning: css video tutorial]

<p>Check out these two CSS interview questions to test your foundation!

<p> No, normally, shouldn’t the ID selector have a higher priority than the pseudo-class selector? ? Why does the pseudo-class selector have a higher priority here?

<p>And, after turning on the debug mode, we positioned the <p> element and only saw color: green taking effect, but no div:: was found. The style definition of first-line:

<p>Check out these two CSS interview questions to test your foundation!

## Only when we go up one level and find the style rules of <p>
can we find the final You can see such a rule below:

<p>Check out these two CSS interview questions to test your foundation!

Therefore, it is obvious here that the <p><p> tag inherits the parent element
This rule applies to the first row of elements, overwriting the color: green defined in the original ID selector.

Verify again

Here, another confusing point is why the ID selector has a higher priority than <p>::first-line The selector is lower.

Let’s make some simple attempts: <p>

The following DEMO shows the priority comparison of the <p>::first-line style and various selectors when they work together. Even !important rules are included:

    Paragraph 1 is set to gray via the tag selector
  • Paragraph 2 is set to gray via the class selector
  • The 3rd paragraph is set to gray through the ID selector
  • The 4th paragraph is set to gray through !important bash
In summary, we use each paragraph at the same time Got the <p>::first-line selector.

1

2

3

4

5

6

7

8

9

10

11

<h2 id="first-line-nbsp-vs-nbsp-tag-nbsp-selector">::first-line vs. tag selector</h2>

<p>This paragraph ...</p> 

 

<h2 id="first-line-nbsp-vs-nbsp-class-nbsp-selector">::first-line vs class selector</h2>

<p class="p2">This paragraph color i...</p> 

 

<h2 id="first-line-nbsp-vs-nbsp-ID-nbsp-selector">::first-line vs ID selector</h2>

<p id="p3">This paragraph color is set ...</p> 

 

<h2 id="first-line-nbsp-vs-nbsp-important">::first-line vs !important</h2>

<p id="p4">This paragraph color is ....</p>

Copy after login
rrree

CodePen Demo -- ::first-line: demo<p>

https://codepen.io/KittyGiraudel/pen/kWobaa/569e082a67400f5fb39a96030d0e9b6c<p>

Look at the effect: <p>

<p>Check out these two CSS interview questions to test your foundation!

#You can see that no matter what selector it is, the priority is not as high as <p>::first-line.

The reason is that <p>::first-line is actually a pseudo-element rather than a pseudo-class, and the content selected by it will actually be treated as a child element of the element. The processing is similar to ::before and ::after. Therefore, the color rule of the parent element is just a cascading relationship for it, through : :first-line The rules defined by itself will have a higher priority!

This is why, in the MDN document, the double colon writing method is more recommended (of course, browsers support the single colon writing method) -- <p>MDN -- ::first-line

<p>Check out these two CSS interview questions to test your foundation!

One more question, an example of an MDN error? An interesting phenomenon

After finishing the above question. Let’s look at another question, a very similar question. <p>

On the MDN page introducing <p>:not, there is an example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

p {

  color: #444;

}

p::first-line {

  color: deepskyblue;

}

 

.p2 {

  color: #444;

}

.p2::first-line {

  color: tomato;

}

 

#p3 {

  color: #444;

}

#p3::first-line {

  color: firebrick;

}

 

#p4 {

  color: #444 !important;

}

#p4::first-line {

  color: hotpink;

}

Copy after login

means, <p>:not(p) You can choose anything that is not <p> Element of tag. However, the actual results of the above CSS selector in the following HTML structure are not quite right.

1

2

3

4

/* Selects any element that is NOT a paragraph */

:not(p) {

  color: blue;

}

Copy after login

The results are as follows:<p>

<p>Check out these two CSS interview questions to test your foundation!

CodePen Demo -- :not pesudo demo<p>

https://codepen.io/ Chokcoco/pen/KKZbWjy<p>

means that <p>:not(p) can still select <p> elements. Yes, the results are the same across multiple browsers.

When you see this, you can stop and think about it, why is the color of the <p><p> element still color: blue?

Why is this? Answer: <p><p>这是由于 :not(p) 同样能够选中 <body>,那么 <body> 的 color 即变成了 blue,由于 color 是一个可继承属性,<p> 标签继承了 <body> 的 color 属性,导致看到的 <p> 也是蓝色。

<p>我们把它改成一个不可继承的属性,试试看:

1

2

3

4

/* Selects any element that is NOT a paragraph */

:not(p) {

  border: 1px solid;

}

Copy after login
<p>Check out these two CSS interview questions to test your foundation!

<p>OK,这次 <p>

没有边框体现,没有问题!

<p>因此,实际使用的时候,需要一定要注意样式继承的问题!

<p>(学习视频分享:css视频教程web前端

The above is the detailed content of Check out these two CSS interview questions to test your foundation!. For more information, please follow other related articles on the PHP Chinese website!

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)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

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.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

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.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles