Lesser-Known And Underused CSS Features In 2022
After reading Louis Lazaris’ insightful article “Those HTML Attributes You Never Use”, I’ve asked myself (and the community) which properties and selectors are lesser-known or should be used more often. Some answers from the community surprised me, as they’ve included some very useful and often-requested CSS features which were made available in the past year or two.
The following list is created with community requests and my personal picks. So, let’s get started!
all
Property
This is a shorthand property which is often used for resetting all properties to their respective initial value by effectively stopping inheritance, or to enforce inheritance for all properties.
initial
Sets all properties to their respective initial values.inherit
Sets all properties to their inherited values.unset
Changes all values to their respective default value which is eitherinherit
orinitial
.revert
Resulting values depend on the stylesheet origin where this property is located.revert-layer
Resulting values will match a previous cascade layer or the next matching rule.
aspect-ratio
for Sizing Control
When aspect-ratio
was initially released, I thought I won’t use it outside image and video elements and in very narrow use-cases. I was surprised to find myself using it in a similar way I would use currentColor
— to avoid unnecessarily setting multiple properties with the same value.
With aspect-ratio
, we can easily control size of an element. For example, equal width and height buttons will have an aspect ratio of 1
. That way, we can easily create buttons that adapt to their content and varying icon sizes, while maintaining the required shape.
I assumed that this issue cannot be fixed, and I moved on. One of the tweets from the community poll suggested that I should look into font-variant-numeric: tabular-nums
, and I was surprised to find a plethora of options that affect font rendering.
For example, tabular-nums
fixed the aforementioned issue by setting the equal width for all numeric characters.
When it comes to rendering performance, it’s very rare to run into these issues when working on regular projects. However, in the case of large DOM trees with several thousands of elements or other similar edge cases, we can run into some performance issues related to CSS and rendering. Luckily, we have a direct way of dealing with these performance issues that cause lag, unresponsiveness to user inputs, low FPS, etc.
This is where contain
property comes in. It tells the browser what won’t change in the render cycle, so the browser can safely skip it. This can have consequences on the layout and style, so make sure to test if this property doesn’t introduce any visual bugs.
.container {
/* child elements won't display outside of this container so only the contents of this container should be rendered*/
contain: paint;
{
This property is quite complex, and Rachel Andrew has covered it in great detail in her article. This property is somewhat difficult to demonstrate, as it is most useful in those very specific edge cases. For example, Johan Isaksson covered one of those examples in his article, where he noticed a major scroll lag on Google Search Console. It was caused by having over 38 000 elements on a page and was fixed by containing property!
As you can see, contain
relies on the developer knowing exactly which properties won’t change and knowing how to avoid potential regressions. So, it’s a bit difficult to use this property safely.
However, there is an option where we can signal the browser to apply the required contain
value automatically. We can use the content-visibility
property. With this property, we can defer the rendering of off-screen and below-the-fold content. Some even refer to this as “lazy-rendering”.
Una Kravets and Vladimir Levin covered this property in their travel blog example. They apply the following class name to the below-the-fold blog sections.
.story {
content-visibility: auto; /* Behaves like overflow: hidden; */
contain-intrinsic-size: 100px 1000px;
}
With contain-intrinsic-size
, we can estimate the size of the section that is going to be rendered. Without this property, the size of the content would be 0
, and page dimensions would keep increasing, as content is loaded.
Going back to Una Kravets and Vladimir Levin’s travel blog example. Notice how the scrollbar jumps around, as you scroll or drag it. This is because of the difference between the placeholder (estimated) size set with contain-intrinsic-size
and the actual render size. If we omit this property, the scroll jumps would be even more jarring.
See the Pen Content-visibility Demo: Base (With Content Visibility) by Vladimir Levin.
Thijs Terluin covers several ways of calculating this value including PHP and JavaScript. Server-side calculation using PHP is especially impressive, as it can automate the value estimation on larger set of various pages and make it more accurate for a subset of screen sizes.
Keep in mind that these properties should be used to fix issues once they happen, so it’s safe to omit them until you encounter render performance issues.
ConclusionCSS evolves constantly, with more features being added each year. It’s important to keep up with the latest features and best practices, but also keep an eye out on browser support and use progressive enhancement.
I’m sure there are more CSS properties and selectors that aren’t included here. Feel free to let us know in the comments which properties or selectors are less known or should be used more often, but may be a bit convoluted or there is not enough buzz around them.
Further Reading on Smashing Magazine
- CSS Custom Properties In The Cascade
- Simplifying Form Styles With
accent-color
- Understanding CSS Grid: Creating A Grid Container
- HTML5 SVG Fill Animation With CSS3 And Vanilla JavaScript