You want enabling CSS selectors, not disabling ones
source link: https://www.silvestar.codes/articles/you-want-a-single-enabling-selector-not-the-one-that-disables-the-rule-of-the-previous-one/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
You want enabling CSS selectors, not disabling ones
An enabling selector is what I call a selector that does a job without disabling the particular rule. I'll explain using the following example.
Let's say we have list items and we want to add the margin to the last one.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<ul>
Here's the usual, disabling way.
li {
margin-bottom: 1em;
}
li:last-child {
margin-bottom: 0;
}
First, we add margin-bottom
to all elements. Then, we disable the bottom margin on the last element. I call this technique disabling selector since the li:last-child
selector disables the previous selector's rule.
But we could do better using enabling selectors.
li:not(:last-child) {
margin-bottom: 1em;
}
The selector li:not(:last-child)
is enabling margin-bottom
on all elements except the last one. There's no need to disable anything. That is much readable and maintainable if you ask me.
Here's another version of the enabling selector for the same example.
li + li {
margin-top: 1em;
}
In this version, we add margin-top
to all elements that have a previous sibling, which means to all elements except the first one, which doesn't have a previous sibling.
There you have it, enabling selectors that do a better job in less code. Here's the demo:
Recommend
-
126
CSS selectors in Go This package implements a CSS selector compiler for Go's HTML parsing package golang.org/x/net/html. package main import ( "fmt" "os" "strings" "github.com/ericchiang/css" "golang.org/x/n...
-
38
In this article, we’re going to learn the fundamentals of CSS Selectors. We use selectors to select the content we want to style with our CSS. By associating one (or many) CSS declarations to one (or many) elements on the...
-
11
SharesubscribeNew CSS functional pseudo-class selectors :is() and :where()These seemingly small additions to CSS selector syntax are going to have a big i...
-
10
CSS selectors and pseudo selectors browser compatibility This page has not been updated for some time and some of the browser versions are obsolete - I'm working on a better format for the page, so check back every now and then :)...
-
4
-
7
Closed Bug 1736859 Opened 29 days ago Closed 21 days ago...
-
6
Selector? Combinator? Subject? As described in the Selectors Level 4 spec, a selector represents a particular pattern of element(s) in a tree structure. We can select specifi...
-
9
When web-scraping, CSS selectors are one of the best friends. This tutorial will tell you what they're, their pros and cons, and why they matter from a web scraping perspective with Python examples to get you going.
-
7
The wasted potential of attribute selectors in CSS October 16th, 2022 I’m a long-time
-
9
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK