The cascade is such an intrinsic part of CSS that they put it right there in the name. If you’ve ever needed to use !important
to affect specificity in the cascade, you’ll know that it can be a tricky thing to deal with. In the early days of CSS, it was common to see highly specific selectors like this:
#sidebar ul li {}
We’re all much better at managing specificity nowadays. It’s a widely accepted best practice to keep specificity low and flat—to shun ID selectors, to make liberal use of classes, and to avoid unnecessary nesting. But there are still plenty of situations where a more specific selector will be useful. With the introduction of a newly proposed pseudo-class, more support of the shadow DOM, and the use of the all
property, we will soon be able to handle inheritance and specificity in new and exciting ways.
The :is Pseudo-Class
Lea Verou recently proposed this new pseudo-class specifically designed to control specificity. It’s already made its way to the CSS Level 4 Selectors spec. Lea has a write up of why it’s useful and there’s some coverage of it in the CSS-Tricks almanac.
Let’s take :not
as an example. The specificity of :not
is equal to the specificity of its argument. This makes using :not
rather painful. Take the following as an example:
See the Pen :not and specificity by CSS-Tricks (@css-tricks) on CodePen.
We might expect that the .red
class would have higher specificity because it is lower in the cascade. However, for any styles to override div:not(.foobar)
they would need to at least match the specificity of a combined element selector (div
) and class selector (.foobar
). Another approach would be div.red
, but there is a better way. This is where :is
can help.
div:is(:not(.foobar)) {
background-color: black;
}
The :not
selector no longer adds any specificity, so the total specificity of the above selector is simply that of one element selector (div
). The .red
class would now be able to override it in the cascade. Once implemented, specificity hacks will be a thing of the past.
Shadow DOM
Today, many people are using classes in HTML like this:
<form class="site-search site-search--full">
<input type="text" class="site-search__field">
<button type="Submit" class="site-search__button">search</button>
</form>
When using shadow DOM, rather than following a verbose naming convention, we’ll be able to omit classes altogether. Styles defined within the shadow DOM are scoped to apply only within the component. Styling can be achieved with simple element selectors without worrying about whether the selectors will interfere with elements elsewhere on the page.
See the Pen shadow dom by CSS GRID (@cssgrid) on CodePen.
It’s liberating to write such easy CSS. No more effort spent naming things. Shadow DOM looks like it is finally making its way to full browser support. It's likely to make it into the next release of Firefox while Edge have implementation as a high priority.
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Opera | Firefox | IE | Edge | Safari |
---|---|---|---|---|---|
53 | 40 | No | No | No | TP |
Mobile / Tablet
iOS Safari | Opera Mobile | Opera Mini | Android | Android Chrome | Android Firefox |
---|---|---|---|---|---|
11.0-11.2 | No | No | 62 | 62 | No |
The all Property
The all
property is a way of setting all CSS properties at once—everything from align-content
to z-index
. What values does it accept? I can’t think of any use case when I’d want all properties to inherit
, but that’s an option. Then there’s initial
which is more like applying a CSS reset where all the styles are gone. No padding. No margin. The initial value is set per property, regardless of the element it is applied to. The initial value of display
is inline
, even if you apply it to a div. The font-style
of an em
tag is normal
, as is the font-weight
of a strong
tag. Link text will be black. You get the idea. (You can find the initial value of any CSS property on MDN.) This does perhaps limit its usefulness, going further than we might like by removing all styles, regardless of context.
See the Pen all: initial by CSS GRID (@cssgrid) on CodePen.
Sadly, the most useful value for all
is also the least widely implemented: revert
. It can remove the styles that you as a developer have applied, while leaving the default user-agent styles intact. We’ve all seen a page of HTML without a stylesheet—black Times New Roman on a white (transparent) background with blue underlined links. If you really want to avoid inheritance, then all: revert
has you covered. All divs will be display: block
and spans will be inline
. All em
tags will be italic and strong
tags will be bold. Links will be blue and underlined.
See the Pen all: revert by CSS GRID (@cssgrid) on CodePen.
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Opera | Firefox | IE | Edge | Safari |
---|---|---|---|---|---|
No | No | No | No | No | 9.1 |
Mobile / Tablet
iOS Safari | Opera Mobile | Opera Mini | Android | Android Chrome | Android Firefox |
---|---|---|---|---|---|
9.3 | No | No | No | No | No |
The future?
CSS-in-JS is a cry for help. We at @csswg should pay attention to this and address the issues before it gets worse.https://t.co/lWQ4ct61ir
— Lea Verou (@LeaVerou) May 24, 2017
The miscellany of rival unstandardized methods for writing CSS-in-JS was an attempt to sidestep these same issues. That approach has gained popularity over the last several years. Some of its proponents have deemed inheritance, the cascade and specificity as fundamentally flawed design decisions of the language. The CSS Working Group at the W3C is responding by improving the power of CSS and the native web platform. It will be interesting to see the outcome…
The latest ways to deal with the cascade, inheritance and specificity is a post from CSS-Tricks
from CSS-Tricks http://ift.tt/2mhtR86
via IFTTT
No comments:
Post a Comment