CSS Protips 
A collection of tips to help take your CSS skills pro.
[!TIP] For other great lists check out @sindresorhus's curated list of awesome lists.
Contents
Protips
- Use a CSS Reset
- Inherit
box-sizing - Use
unsetInstead of Resetting All Properties - Use
:not()to Apply/Unapply Borders on Navigation - Check if Font Is Installed Locally
- Add
line-heighttobody - Set
:focusfor Form Elements - Vertically-Center Anything
- Use
aspect-ratioInstead of Height/Width - Comma-Separated Lists
- Select Items Using Negative
nth-child - Use SVG for Icons
- Use the "Lobotomized Owl" Selector
- Use
max-heightfor Pure CSS Sliders - Equal-Width Table Cells
- Get Rid of Margin Hacks With Flexbox
- Use Attribute Selectors with Empty Links
- Control Specificity Better With
:is() - Style "Default" Links
- Intrinsic Ratio Boxes
- Style Broken Images
- Use
remfor Global Sizing; Useemfor Local Sizing - Hide Autoplay Videos That Aren't Muted
- Use
:rootfor Flexible Type - Set
font-sizeon Form Elements for a Better Mobile Experience - Use Pointer Events to Control Mouse Events
- Set
display: noneon Line Breaks Used as Spacing - Use
:emptyto Hide Empty HTML Elements - Use
margin-inlineinstead ofmargin
Use a CSS Reset
CSS resets help enforce style consistency across different browsers with a clean slate for styling elements. There are plenty of reset patterns to find, or you can use a more simplified reset approach:
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
Now elements will be stripped of margins and padding, and box-sizing lets you manage layouts with the CSS box model.
Demo
[!TIP] If you follow the Inherit
box-sizingtip below you might opt to not include thebox-sizingproperty in your CSS reset.
Inherit box-sizing
Let box-sizing be inherited from html:
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
This makes it easier to change box-sizing in plugins or other components that leverage other behavior.
Demo
Use unset Instead of Resetting All Properties
When resetting an element's properties, it's not necessary to reset each individual property:
button {
background: none;
border: none;
color: inherit;
font: inherit;
outline: none;
padding: 0;
}
You can specify all of an element's properties using the all shorthand. Setting the value to unset changes an element's properties to their initial values:
button {
all: unset;
}
Use :not() to Apply/Unapply Borders on Navigation
Instead of putting on the border...
/* add border */
.nav li {
border-right: 1px solid #666;
}
...and then taking it off the last element...
/* remove border */
.nav li:last-child {
border-right: none;
}
...use the :not() pseudo-class to only apply to the elements you want:
.nav li:not(:last-child) {
border-right: 1px solid #666;
}
Here, the CSS selector is read as a human would describe it.
Demo
Check if Font Is Installed Locally
You can check if a font is installed locally before fetching it remotely, which is a good performance tip, too.
```css @font-face { font-family: "Dank Mono"; src: