Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ How to Increase CSS Class Selector Specificity to Beat the ID Selector Without Using Important

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š How to Increase CSS Class Selector Specificity to Beat the ID Selector Without Using Important


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Recently, while working on some legacy code, I had to figure out how to overwrite ID selector styles without using !important.

The CSS was similar to this

<div id="parent">
  <div class="child">Content</div>
</div>

<style>
  /* specificity 100 + 10 = 110 */
  #parent .child {
    color: red;
  }

  /* specificity 10 */
  .child {
    color: yellow;
  }
</style>

This results in a text with red color, but in my case, I needed to make the text yellow.

Normally, I would just refactor the CSS and HTML, but in this case, I couldnโ€™t change the HTML and order of CSS due to some legacy code.

The easy solution would be to use an infamous !important keyword to overwrite everything, but I didnโ€™t want to go that way.

Using attribute selector

The better way is to change the ID selector to the attribute selector. Instead of #parent it's possible to use [id="parent"]. This has the same specificity as a class selector.

Now instead of specificity 110, it is down to 20.

/* specificity 10 + 10 = 20 */
[id="parent"] .child {
  color: red;
}

/* specificity 10 */
.child {
  color: yellow;
}

Chaining the selector with itself

Now I had to increase the .child specificity above 20 to beat the [id="parent" .child" selector. One way to achieve that is to chain the class selector with itself.

The specificity of .child is 10. The specificity of .child.child.child is 30.

<div id="parent">
  <div class="child">Content</div>
</div>

<style>
  /* specificity 10 + 10 = 20 */
  [id="parent"] .child {
    color: red;
  }

  /* specificity 10 + 10 + 10 = 30 */
  .child.child.child {
    color: yellow;
  }
</style>

This is an easy way to bump up specificity without adding additional classes or knowing about the context where the item will be placed.

I find this better than using !important, but it is still a hack. If possible, try to avoid this.

...



๐Ÿ“Œ How to Increase CSS Class Selector Specificity to Beat the ID Selector Without Using Important


๐Ÿ“ˆ 132.76 Punkte

๐Ÿ“Œ INTRODUCTION TO CSS CASCADE, SELECTOR AND SPECIFICITY- A BEGINNER GUIDE 101


๐Ÿ“ˆ 55.24 Punkte

๐Ÿ“Œ CSS Cascade, Selector and Specificity.


๐Ÿ“ˆ 55.24 Punkte

๐Ÿ“Œ A BRIEF REVIEW OF CSS CASCADING, CSS SELECTORS and CSS SPECIFICITY.


๐Ÿ“ˆ 55.04 Punkte

๐Ÿ“Œ How to Override the Properties of a CSS Class Using Another CSS Class


๐Ÿ“ˆ 44.51 Punkte

๐Ÿ“Œ How to Target a CSS Class Inside Another CSS Class


๐Ÿ“ˆ 39.4 Punkte

๐Ÿ“Œ Understanding CSS Specificity: What It Is and How It Works


๐Ÿ“ˆ 35.89 Punkte

๐Ÿ“Œ CSS Cascade, Specificity, and Selectors


๐Ÿ“ˆ 35.89 Punkte

๐Ÿ“Œ CSS Specificity Explained


๐Ÿ“ˆ 35.89 Punkte

๐Ÿ“Œ CSS SPECIFICITY?


๐Ÿ“ˆ 35.89 Punkte

๐Ÿ“Œ Simplifying CSS: Understanding Specificity, Cascade, and Selectors


๐Ÿ“ˆ 35.89 Punkte

๐Ÿ“Œ Decoding CSS: Mastering Cascade, Selectors, and Specificity for Smarter Styling.


๐Ÿ“ˆ 35.89 Punkte

๐Ÿ“Œ Understanding CSS Cascade, Selectors and Specificity


๐Ÿ“ˆ 35.89 Punkte

๐Ÿ“Œ Recognizing Selectors, Specificity, and CSS Cascade


๐Ÿ“ˆ 35.89 Punkte

๐Ÿ“Œ Use the :lang pseudo-class over the lang attribute selector for language-specific styles


๐Ÿ“ˆ 29.48 Punkte

๐Ÿ“Œ How to get full CSS selector path?


๐Ÿ“ˆ 28.92 Punkte

๐Ÿ“Œ Level Up Your CSS Skills With The :has() Selector


๐Ÿ“ˆ 28.92 Punkte

๐Ÿ“Œ Complete CSS Selector Cheat Sheet: A Hands-On Guide with images๐Ÿ˜


๐Ÿ“ˆ 28.92 Punkte

๐Ÿ“Œ How to Use the :has() Selector in CSS


๐Ÿ“ˆ 28.92 Punkte

๐Ÿ“Œ Unleash the Power of CSS :has() Selector: A Game-Changer for Styling Your Web Content


๐Ÿ“ˆ 28.92 Punkte

๐Ÿ“Œ An Introduction to the :has() Selector in CSS


๐Ÿ“ˆ 28.92 Punkte

๐Ÿ“Œ Choosing a CSS selector


๐Ÿ“ˆ 28.92 Punkte

๐Ÿ“Œ CSS Grid: Moving From CSS Frameworks To CSS Grid (2018 and beyond)


๐Ÿ“ˆ 28.72 Punkte

๐Ÿ“Œ Stylify CSS: Automagic CSS bundles splitting into CSS layers in Astro.build


๐Ÿ“ˆ 28.72 Punkte

๐Ÿ“Œ How to increase allocated size for partition /system in Linux Mint OS without burnt disks & without doing reinstall?


๐Ÿ“ˆ 27.4 Punkte

๐Ÿ“Œ Facebook รผbernimmt Beat Saber-Entwickler Beat Games


๐Ÿ“ˆ 26.51 Punkte

๐Ÿ“Œ Beat Games: Facebook kauft Entwicklerstudio von Beat Saber


๐Ÿ“ˆ 26.51 Punkte

๐Ÿ“Œ Beat Saber ist nun ein Facebook-Spiel: รœbernahme von Beat Games bestรคtigt


๐Ÿ“ˆ 26.51 Punkte

๐Ÿ“Œ Beat Games: Facebook รผbernimmt die Beat-Saber-Macher


๐Ÿ“ˆ 26.51 Punkte

๐Ÿ“Œ Oculus just bought Beat Saber developer Beat Games


๐Ÿ“ˆ 26.51 Punkte

๐Ÿ“Œ KI bestraft mich fรผr Fehler in Beat Saber ? | BEAT THE TRร–TE


๐Ÿ“ˆ 26.51 Punkte

๐Ÿ“Œ Beat 'em up zum Beat: God of Rock zur Gamescom angespielt


๐Ÿ“ˆ 26.51 Punkte

๐Ÿ“Œ Beat Saber ist nun ein Facebook-Spiel: รœbernahme von Beat Games bestรคtigt


๐Ÿ“ˆ 26.51 Punkte

๐Ÿ“Œ Beat Saber: Facebook รผbernimmt das Entwicklerstudio Beat Games


๐Ÿ“ˆ 26.51 Punkte

๐Ÿ“Œ Purpose, Not Specificity, Limits the Pardon Power: A Rejoinder to Rappaport


๐Ÿ“ˆ 26.32 Punkte











matomo