What is sanitize.css?
The sanitize.css package is a CSS library that provides consistent, cross-browser default styling for HTML elements. It's designed to normalize styles across different browsers, remove default margins, and apply a consistent box-sizing model. This helps in creating a more predictable styling baseline for web projects.
What are sanitize.css's main functionalities?
Normalize styles
This code ensures that every element on the page, including pseudo-elements, inherits the box-sizing property from the html element, which is set to border-box. This makes it easier to manage layouts as it includes padding and border in the element's total width and height.
html { box-sizing: border-box; } *, *::before, *::after { box-sizing: inherit; }
Remove default margins
This code removes the default margins from common elements like body, headings, paragraphs, and others. It provides a clean slate for adding custom spacing without having to override browser defaults.
body, h1, h2, h3, h4, h5, h6, p, figure, blockquote, dl, dd { margin: 0; }
Set default font-family
This sets the default font family to sans-serif for the entire HTML document, ensuring a consistent typography baseline across different browsers.
html { font-family: sans-serif; }
Other packages similar to sanitize.css
normalize.css
normalize.css is a popular CSS reset library that aims to normalize styles across browsers. It focuses on fixing common inconsistencies and preserving useful defaults rather than unifying all styles like sanitize.css. While both aim to provide a consistent styling baseline, normalize.css is more about correcting quirks without imposing a specific styling philosophy.
reset-css
reset-css is another CSS reset library that aims to strip away all default browser styling on elements. Unlike sanitize.css, which applies minimalistic default styles and normalizes differences, reset-css provides a clean slate by removing all styles, allowing developers to start styling from scratch.
sanitize.css
sanitize.css is a CSS library that provides consistent, cross-browser
default styling of HTML elements alongside useful defaults.
How to get it
NPM
npm install --save sanitize.css
Download
See https://csstools.github.io/sanitize.css/latest/sanitize.css
CDN
see https://cdnjs.com/libraries/10up-sanitize.css
Usage in npm and webpack
Import sanitize.css in CSS:
@import '~sanitize.css';
Alternatively, import sanitize.css in JS:
import 'sanitize.css';
In webpack.config.js
, be sure to use the appropriate loaders:
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
}
}
What does it do?
- Normalizes styles for a wide range of elements.
- Corrects bugs and common browser inconsistencies.
- Provides common, useful defaults.
- Explains what code does using detailed comments.
Features
Box sizing defaults to border-box
*, ::before, ::after {
box-sizing: border-box;
}
Backgrounds do not repeat by default
*, ::before, ::after {
background-repeat: no-repeat;
}
Pseudo-elements inherit text decoration and vertical alignment
::before,
::after {
text-decoration: inherit;
vertical-align: inherit;
}
Cursors only change to hint non-obvious interfaces
html {
cursor: default;
}
The default font is the system ui font
html {
font-family:
system-ui,
-apple-system,
Segoe UI,
Roboto,
Ubuntu,
Cantarell,
Oxygen,
sans-serif,
"Apple Color Emoji",
"Segoe UI Emoji",
"Segoe UI Symbol",
"Noto Color Emoji";
}
Tabs appear the same on the web as in a typical editor
html {
tab-size: 4;
}
Words break to prevent prevent overflow
html {
word-break: break-all;
}
Documents do not use a margin for outer padding
body {
margin: 0;
}
Navigation lists do not include a marker style
nav ol, nav ul {
list-style: none;
}
Pre-formatted and code-formatted text uses the monospace system ui font
code, kbd, pre, samp {
font-family:
Menlo,
Consolas,
Roboto Mono,
Ubuntu Monospace,
Oxygen Mono,
Liberation Mono,
monospace;
}
Text selections do not include text shadows
::selection {
background-color: #b3d4fc;
color: #000;
text-shadow: none;
}
Media elements align to the text center of other content
audio, canvas, iframe, img, svg, video {
vertical-align: middle;
}
SVGs fallback to the current text color
svg {
fill: currentColor;
}
Tables do not include additional border spacing
table {
border-collapse: collapse;
}
Form controls are easily style-able
button, input, select, textarea {
font-family: inherit;
font-size: inherit;
line-height: inherit;
}
Textareas only resize vertically by default
textarea {
resize: vertical;
}
Single taps are dispatched immediately on clickable elements
a, area, button, input, label, select, summary, textarea, [tabindex] {
-ms-touch-action: manipulation;
touch-action: manipulation;
}
ARIA roles include visual cursor hints
[aria-busy="true"] {
cursor: progress;
}
[aria-controls] {
cursor: pointer;
}
[aria-disabled], [disabled] {
cursor: default;
}
Visually hidden content remains accessible
[aria-hidden="false"][hidden]:not(:focus) {
clip: rect(0, 0, 0, 0);
display: inherit;
position: absolute;
}
Differences
normalize.css and sanitize.css correct browser bugs while carefully testing
and documenting changes. normalize.css styles adhere to css specifications.
sanitize.css styles adhere to common developer expectations and preferences.
reset.css unstyles all elements. Both sanitize.css and normalize.css are
maintained in sync.
Browser support
- Chrome (last 3)
- Edge (last 3)
- Firefox (last 3)
- Firefox ESR
- Opera (last 3)
- Safari (last 3)
- iOS Safari (last 2)
- Internet Explorer 9+
Contributing
Please read the contribution guidelines in order to make the
contribution process easy and effective for everyone involved.
Acknowledgements
sanitize.css is a project by Jonathan Neal,
built upon normalize.css, a project by
Jonathan Neal,
co-created with Nicolas Gallagher.