:the place() – CSS: Cascading Taste Sheets

by | Jul 29, 2024 | Etcetera | 0 comments

Managing CSS can be difficult, in particular when types wish to override every other. This incessantly makes it hard to stick the categories in the best places. To simplify problems, the :where selector was once as soon as added in CSS. This is a specific CSS selector that permits you to group selectors without increasing their specificity, making it more straightforward to override types when sought after.

Believe the following HTML:

<div id="root">
    <p>First paragraph</p>
    <div class="section">
        <p>2d paragraph</p>
    </div>
</div>
<div class="section">
    <p>third paragraph</p>
</div>

We’ve two div elements. One is within #root, and the other is outside of it. We want paragraphs within #root to be crimson and paragraphs within .section to be green. Normally, you’ll be capable of write CSS as follows:

#root p {
    color: crimson;
}
.section p {
    color: green;
}

Then again, with this CSS, the paragraph within .section can be green, alternatively the paragraphs within #root will keep crimson instead of turning green. This happens given that specificity of the #root selector is higher than that of the .section selector.

See the Pen CSS :the place selector by way of HONGKIAT (@hkdc) on CodePen.

To unravel this situation, traditionally, we could change our CSS as follows:

#root p {
    color: crimson;
}
.section p,
#root .section p {
    color: green;
}

Then again this isn’t very good as it is going to building up the specificity of the selector and makes them look additional complicated. It’s going to in the end explanation why additional problems when we wish to override the categories.

That’s the position the :where selector is to be had in. With the :where selector, you’ll be capable of group selectors without increasing their specificity. Proper right here’s how we will change our CSS instead:

:where(#root) p {
    color: crimson;
}
.section p {
    color: green;
}

The #root selector is wrapped with the :where selector, which is in a position to cut back its specificity to 0. This will likely allow the paragraph within .section to be all green, even if it’s within #root.

See the Pen CSS :the place selector (earlier than) by way of HONGKIAT (@hkdc)
on CodePen.

How is it different from the :is selector?

The :where selector is similar to the :is selector, where we will group slightly numerous selectors together. The primary difference is their behavior affecting the specificity during the group. While the :where selector will remove it (or set it to 0), the :is selector would possibly building up the specificity of the selector with the very best specificity during the group.

See also  12 Highest Graphic Design Device in 2024 (In comparison)

Let’s take a look at the following example:

.section p {
    color: green;
}

.section .highlight {
    color: orange;
}

p:first-of-type {
    color: crimson;
}

The result may well be that most straightforward the principle and third paragraphs would turn crimson. The second paragraph would keep orange since .section .highlight has higher specificity than the p:first-of-type, even if it is usually the principle paragraph.

See the Pen CSS :the place vs :is selector by way of HONGKIAT (@hkdc) on CodePen.

Traditionally, we will at all times rewrite our CSS, as follows:

p:first-of-type,
.section p:first-of-type {
    color: crimson;
}

Then again, we will moreover write it this way:

p:first-of-type,
p.highlight:first-of-type {
    color: crimson;
}

Then again, this over again leads to additional complicated selectors and complex specificity issues down the road. With the :is selector, we will have it much more efficient to resolve this issue with the following laws.

:is(div, section, .section) p:first-of-type {
    color: crimson;
}

We group together div, section, and .section. This group will have the equivalent specificity as .section so the color crimson will observe to every during the div and the section elements as smartly.

See the Pen CSS :the place vs :is selector (carried out) by way of HONGKIAT (@hkdc) on CodePen.

Browser Compatibility

Browser Desktop Style Desktop Enhance Cell Style Cell Enhance
Google Chrome 88 and later Supported 88 and later Supported
Mozilla Firefox 78 and later Supported 78 and later Supported
Safari 14.1 and later Supported 14.5 and later (iOS) Supported
Microsoft Edge 88 and later Supported 88 and later Supported
Opera 75 and later Supported 61 and later Supported
Internet Explorer Not supported Not supported Not supported Not supported
Samsung Internet N/A N/A 14.0 and later Supported
See also  How To Way Small Industry Internet Design (2024 Information)

Wrapping up

The :where selector is a great addition to CSS that permits you to group selectors without increasing their specificity. This makes it a very good selector so that you can upload base types to a group of elements without worrying regarding the specificity of the selector. It common makes it more straightforward to control types without complicating specificity and override them when sought after.

Bonus: Check out Specificity Calculator to seem how the specificity of your CSS selectors is calculated.

The post :the place() – CSS: Cascading Taste Sheets seemed first on Hongkiat.

WordPress Website Development

Supply: https://www.hongkiat.com/blog/where-css-selector/

[ continue ]

WordPress Maintenance Plans | WordPress Hosting

read more

0 Comments

Submit a Comment

DON'T LET YOUR WEBSITE GET DESTROYED BY HACKERS!

Get your FREE copy of our Cyber Security for WordPress® whitepaper.

You'll also get exclusive access to discounts that are only found at the bottom of our WP CyberSec whitepaper.

You have Successfully Subscribed!