Jadual Kandungan
The problem
The solution
Demo
Rumah hujung hadapan web tutorial css Menggunakan kekhususan: di mana () sebagai tetapan semula CSS

Menggunakan kekhususan: di mana () sebagai tetapan semula CSS

Mar 22, 2025 am 10:38 AM

Using the Specificity of :where() as a CSS Reset

I don’t know about you, but I write these three declarations many times in my CSS:

1

2

3

4

5

ul {

  padding: 0;

  margin: 0;

  list-style-type: none;

}

Salin selepas log masuk

You might yell at me and say I can just put those in my CSS resets. I wish I could, but I don‘t want to and I’ll tell you why in a second.

User agents set values to those properties in a list for a purpose, and that is to make lists more readable. These are the default styles in chromium browsers for a

    element:

    1

    2

    3

    4

    5

    6

    7

    8

    ul {

      list-style-type: disc;

      margin-block-start: 1em;

      margin-block-end: 1em;

      margin-inline-start: 0px;

      margin-inline-end: 0px;

      padding-inline-start: 40px;

    }

    Salin selepas log masuk

    So, without adding any class in HTML or style in CSS, we get those for free. That‘s a nice thing and I don‘t want to lose it. But I would appreciate it if I could make the browser understand that there is very high chance I don’t want that default feature in cases where I add a class to the element.

    So here is a quick solution to reset a

      element that has a class:

      1

      2

      3

      4

      5

      ul[class] {

        padding: 0;

        margin: 0;

        list-style-type: none;

      }

      Salin selepas log masuk

      Now I don’t lose the default style except when I add a class to my

        element.

        The problem

        There is a problem with this solution. Imagine there is a list that we want to have a different list-style-type for it, like the following:

        1

        2

        3

        4

        5

        6

        7

        8

        9

        ul[class] {

          padding: 0;

          margin: 0;

          list-style-type: none;

        }

         

        .list {

          list-style-type: square;

        }

        Salin selepas log masuk

        This doesn’t work since ul[class] has higher specificity. That’s where our solution breaks down.

        We could add more weight to the selector’s specificity:

        1

        2

        3

        4

        5

        6

        7

        8

        9

        ul.list {

          list-style-type: square; /* Specificity: 0, 1, 1 */

        }

         

        /* or */

         

        .sidebar .list {

          list-style-type: square; /* Specificity: 0, 2, 0 */

        }

        Salin selepas log masuk

        If you are OK adding more weight to the selector, you are good to go. But I’m not OK with it, personally. For example, I don’t want to put the element’s name in my CSS most of the times due to a separation of concerns principle. Or, if you are following BEM methodology, problems will most certainly arise as this conflicts with it.

        So what can we do?

        The solution

        A few months ago, I learned about some hot selectors, including :is() and :where(). One thing about these two functional pseudo selectors, is that they can change specificity, giving us the power to nullify or increase that specificity.

        The key about :where() is that it always has 0 specificity. So we can get rid of our problem very easily like this:

        1

        2

        3

        4

        5

        6

        7

        :where(ul[class]) {

          list-style: none;

        }

         

        .list {

          list-style: square; /* Now this works like a charm! */

        }

        Salin selepas log masuk

        With the power of this selector, libraries can give us style with no specificity. So there would be no specificity to compete with when we as authors write CSS.

        Demo

        In the following demo, you can remove :where() to see what we talked about in action:

        Atas ialah kandungan terperinci Menggunakan kekhususan: di mana () sebagai tetapan semula CSS. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

<🎜>: Bubble Gum Simulator Infinity - Cara Mendapatkan dan Menggunakan Kekunci Diraja
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Sistem Fusion, dijelaskan
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

Tutorial Java
1664
14
Tutorial PHP
1269
29
Tutorial C#
1248
24
Cara membuat pemasa undur animasi dengan HTML, CSS dan JavaScript Cara membuat pemasa undur animasi dengan HTML, CSS dan JavaScript Apr 11, 2025 am 11:29 AM

Pernahkah anda memerlukan pemasa undur dalam projek? Untuk sesuatu seperti itu, mungkin semula jadi untuk mencapai plugin, tetapi sebenarnya lebih banyak lagi

Panduan Atribut Data HTML Panduan Atribut Data HTML Apr 11, 2025 am 11:50 AM

Semua yang anda ingin tahu mengenai atribut data dalam HTML, CSS, dan JavaScript.

Walaupun anda tidak kelihatan, kecerunan CSS menjadi lebih baik Walaupun anda tidak kelihatan, kecerunan CSS menjadi lebih baik Apr 11, 2025 am 09:16 AM

Satu perkara yang menarik perhatian saya pada senarai ciri-ciri untuk Lea Verou &#039; s conic-gradient () polyfill adalah item terakhir:

Bukti konsep untuk menjadikan sass lebih cepat Bukti konsep untuk menjadikan sass lebih cepat Apr 16, 2025 am 10:38 AM

Pada permulaan projek baru, kompilasi SASS berlaku dalam sekejap mata. Ini terasa hebat, terutamanya apabila ia dipasangkan dengan BrowserSync, yang dimuat semula

Perbandingan pembekal bentuk statik Perbandingan pembekal bentuk statik Apr 16, 2025 am 11:20 AM

Mari kita cuba menyusun istilah di sini: "Penyedia Borang Statik." Anda membawa html anda

Cara Membina Komponen Vue dalam Tema WordPress Cara Membina Komponen Vue dalam Tema WordPress Apr 11, 2025 am 11:03 AM

Arahan template inline membolehkan kita membina komponen Vue yang kaya sebagai peningkatan progresif ke atas markup WordPress yang sedia ada.

PHP adalah A-OK untuk templat PHP adalah A-OK untuk templat Apr 11, 2025 am 11:04 AM

Templat PHP sering mendapat rap buruk untuk memudahkan kod subpar - tetapi itu tidak perlu berlaku. Mari kita lihat bagaimana projek PHP dapat menguatkuasakan asas

Tiga jenis kod Tiga jenis kod Apr 11, 2025 pm 12:02 PM

Setiap kali saya memulakan projek baru, saya menyusun kod yang saya cari dalam tiga jenis, atau kategori jika anda suka. Dan saya fikir jenis ini boleh digunakan

See all articles