| 1 | # sanitize.css [<img src="https://csstools.github.io/sanitize.css/logo.svg" alt="sanitize" width="90" height="90" align="right">][sanitize.css]
|
|---|
| 2 |
|
|---|
| 3 | [sanitize.css] is a CSS library that provides consistent, cross-browser
|
|---|
| 4 | default styling of HTML elements alongside useful defaults.
|
|---|
| 5 |
|
|---|
| 6 | **sanitize.css** is developed alongside [normalize.css], which means every
|
|---|
| 7 | normalization is included, and every normalization and opinion are clearly
|
|---|
| 8 | marked and documented.
|
|---|
| 9 |
|
|---|
| 10 | **sanitize.css** wraps styles in zero-specificity selectors using `:where()`.
|
|---|
| 11 |
|
|---|
| 12 | ## Usage
|
|---|
| 13 |
|
|---|
| 14 | ```html
|
|---|
| 15 | <link href="https://cdn.skypack.dev/sanitize.css" rel="stylesheet" />
|
|---|
| 16 | ```
|
|---|
| 17 |
|
|---|
| 18 | [Learn more about `sanitize.css`](#features).
|
|---|
| 19 |
|
|---|
| 20 | #### Forms CSS
|
|---|
| 21 |
|
|---|
| 22 | A separate stylesheet that normalizes form controls without side effects.
|
|---|
| 23 |
|
|---|
| 24 | ```html
|
|---|
| 25 | <link href="https://unpkg.com/sanitize.css/forms.css" rel="stylesheet" />
|
|---|
| 26 | ```
|
|---|
| 27 |
|
|---|
| 28 | [Learn more about `forms.css`](#forms).
|
|---|
| 29 |
|
|---|
| 30 | #### Assets CSS
|
|---|
| 31 |
|
|---|
| 32 | A separate stylesheet that applies a comfortable measure to plain documents.
|
|---|
| 33 |
|
|---|
| 34 | ```html
|
|---|
| 35 | <link href="https://unpkg.com/sanitize.css/assets.css" rel="stylesheet" />
|
|---|
| 36 | ```
|
|---|
| 37 |
|
|---|
| 38 | [Learn more about `assets.css`](#assets).
|
|---|
| 39 |
|
|---|
| 40 | #### Typography CSS
|
|---|
| 41 |
|
|---|
| 42 | A separate stylesheet that normalizes typography using system interface fonts.
|
|---|
| 43 |
|
|---|
| 44 | ```html
|
|---|
| 45 | <link href="https://unpkg.com/sanitize.css/typography.css" rel="stylesheet" />
|
|---|
| 46 | ```
|
|---|
| 47 |
|
|---|
| 48 | [Learn more about `typography.css`](#typography).
|
|---|
| 49 |
|
|---|
| 50 | #### Reduce Motion CSS
|
|---|
| 51 |
|
|---|
| 52 | A separate stylesheet for restricting motion when the user has requested this at system level.
|
|---|
| 53 |
|
|---|
| 54 | ```html
|
|---|
| 55 | <link href="https://unpkg.com/sanitize.css/reduce-motion.css" rel="stylesheet" />
|
|---|
| 56 | ```
|
|---|
| 57 |
|
|---|
| 58 | [Learn more about `reduce-motion.css`](#reduce-motion).
|
|---|
| 59 |
|
|---|
| 60 | #### System-UI
|
|---|
| 61 |
|
|---|
| 62 | A separate stylesheet that adds support for using `system-ui` in Firefox.
|
|---|
| 63 |
|
|---|
| 64 | ```html
|
|---|
| 65 | <link href="https://unpkg.com/sanitize.css/system-ui.css" rel="stylesheet" />
|
|---|
| 66 | ```
|
|---|
| 67 |
|
|---|
| 68 | #### UI-Monospace
|
|---|
| 69 |
|
|---|
| 70 | A separate stylesheet that adds support for using `ui-monospace` in Chrome, Edge, and Firefox.
|
|---|
| 71 |
|
|---|
| 72 | ```html
|
|---|
| 73 | <link href="https://unpkg.com/sanitize.css/ui-monospace.css" rel="stylesheet" />
|
|---|
| 74 | ```
|
|---|
| 75 |
|
|---|
| 76 | ## Install
|
|---|
| 77 |
|
|---|
| 78 | ```sh
|
|---|
| 79 | npm install sanitize.css --save
|
|---|
| 80 | ```
|
|---|
| 81 |
|
|---|
| 82 | #### Webpack Usage
|
|---|
| 83 |
|
|---|
| 84 | Import [sanitize.css] in CSS:
|
|---|
| 85 |
|
|---|
| 86 | ```css
|
|---|
| 87 | @import '~sanitize.css';
|
|---|
| 88 | @import '~sanitize.css/forms.css';
|
|---|
| 89 | @import '~sanitize.css/typography.css';
|
|---|
| 90 | ```
|
|---|
| 91 |
|
|---|
| 92 | Alternatively, import [sanitize.css] in JS:
|
|---|
| 93 |
|
|---|
| 94 | ```js
|
|---|
| 95 | import 'sanitize.css';
|
|---|
| 96 | import 'sanitize.css/forms.css';
|
|---|
| 97 | import 'sanitize.css/typography.css';
|
|---|
| 98 | ```
|
|---|
| 99 |
|
|---|
| 100 | In `webpack.config.js`, be sure to use the appropriate loaders:
|
|---|
| 101 |
|
|---|
| 102 | ```js
|
|---|
| 103 | module.exports = {
|
|---|
| 104 | module: {
|
|---|
| 105 | rules: [
|
|---|
| 106 | {
|
|---|
| 107 | test: /\.css$/,
|
|---|
| 108 | use: [ 'style-loader', 'css-loader' ]
|
|---|
| 109 | }
|
|---|
| 110 | ]
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | ```
|
|---|
| 114 |
|
|---|
| 115 | **Download**
|
|---|
| 116 |
|
|---|
| 117 | See https://csstools.github.io/sanitize.css/latest/sanitize.css
|
|---|
| 118 |
|
|---|
| 119 | ## What does it do?
|
|---|
| 120 |
|
|---|
| 121 | * Normalizes styles for a wide range of elements.
|
|---|
| 122 | * Corrects bugs and common browser inconsistencies.
|
|---|
| 123 | * Provides common, useful defaults.
|
|---|
| 124 | * Explains what code does using detailed comments.
|
|---|
| 125 |
|
|---|
| 126 | ## Browser support
|
|---|
| 127 |
|
|---|
| 128 | * Chrome (last 2)
|
|---|
| 129 | * Edge (last 2)
|
|---|
| 130 | * Firefox (last 2)
|
|---|
| 131 | * Firefox ESR
|
|---|
| 132 | * Opera (last 2)
|
|---|
| 133 | * Safari (last 2)
|
|---|
| 134 | * iOS Safari (last 2)
|
|---|
| 135 | * Internet Explorer 9+
|
|---|
| 136 |
|
|---|
| 137 | ## Differences
|
|---|
| 138 |
|
|---|
| 139 | [normalize.css] and [sanitize.css] correct browser bugs while carefully testing
|
|---|
| 140 | and documenting changes. normalize.css styles adhere to css specifications.
|
|---|
| 141 | sanitize.css styles adhere to common developer expectations and preferences.
|
|---|
| 142 | [reset.css] unstyles all elements. Both sanitize.css and normalize.css are
|
|---|
| 143 | maintained in sync.
|
|---|
| 144 |
|
|---|
| 145 | ## Features
|
|---|
| 146 |
|
|---|
| 147 | ##### Box sizing defaults to border-box
|
|---|
| 148 |
|
|---|
| 149 | ```css
|
|---|
| 150 | *, ::before, ::after {
|
|---|
| 151 | box-sizing: border-box;
|
|---|
| 152 | }
|
|---|
| 153 | ```
|
|---|
| 154 |
|
|---|
| 155 | ##### Backgrounds do not repeat by default
|
|---|
| 156 |
|
|---|
| 157 | ```css
|
|---|
| 158 | *, ::before, ::after {
|
|---|
| 159 | background-repeat: no-repeat;
|
|---|
| 160 | }
|
|---|
| 161 | ```
|
|---|
| 162 |
|
|---|
| 163 | ##### Pseudo-elements inherit text decoration and vertical alignment
|
|---|
| 164 |
|
|---|
| 165 | ```css
|
|---|
| 166 | ::before,
|
|---|
| 167 | ::after {
|
|---|
| 168 | text-decoration: inherit;
|
|---|
| 169 | vertical-align: inherit;
|
|---|
| 170 | }
|
|---|
| 171 | ```
|
|---|
| 172 |
|
|---|
| 173 | ##### Cursors only change to hint non-obvious interfaces
|
|---|
| 174 |
|
|---|
| 175 | ```css
|
|---|
| 176 | html {
|
|---|
| 177 | cursor: default;
|
|---|
| 178 | }
|
|---|
| 179 | ```
|
|---|
| 180 |
|
|---|
| 181 | ##### Text has a comfortable line height in all browsers
|
|---|
| 182 |
|
|---|
| 183 | ```css
|
|---|
| 184 | html {
|
|---|
| 185 | line-height: 1.5;
|
|---|
| 186 | }
|
|---|
| 187 | ```
|
|---|
| 188 |
|
|---|
| 189 | ##### Tabs appear the same on the web as in a typical editor
|
|---|
| 190 |
|
|---|
| 191 | ```css
|
|---|
| 192 | html {
|
|---|
| 193 | tab-size: 4;
|
|---|
| 194 | }
|
|---|
| 195 | ```
|
|---|
| 196 |
|
|---|
| 197 | ##### Words break to prevent overflow
|
|---|
| 198 |
|
|---|
| 199 | ```css
|
|---|
| 200 | html {
|
|---|
| 201 | word-break: break-all;
|
|---|
| 202 | }
|
|---|
| 203 | ```
|
|---|
| 204 |
|
|---|
| 205 | ##### Documents do not use a margin for outer padding
|
|---|
| 206 |
|
|---|
| 207 | ```css
|
|---|
| 208 | body {
|
|---|
| 209 | margin: 0;
|
|---|
| 210 | }
|
|---|
| 211 | ```
|
|---|
| 212 |
|
|---|
| 213 | ##### Navigation lists do not include a marker style
|
|---|
| 214 |
|
|---|
| 215 | ```css
|
|---|
| 216 | nav ol, nav ul {
|
|---|
| 217 | list-style: none;
|
|---|
| 218 | padding: 0;
|
|---|
| 219 | }
|
|---|
| 220 | ```
|
|---|
| 221 |
|
|---|
| 222 | ##### Media elements align to the text center of other content
|
|---|
| 223 |
|
|---|
| 224 | ```css
|
|---|
| 225 | audio, canvas, iframe, img, svg, video {
|
|---|
| 226 | vertical-align: middle;
|
|---|
| 227 | }
|
|---|
| 228 | ```
|
|---|
| 229 |
|
|---|
| 230 | ##### SVGs fallback to the current text color
|
|---|
| 231 |
|
|---|
| 232 | ```css
|
|---|
| 233 | svg:not([fill]) {
|
|---|
| 234 | fill: currentColor;
|
|---|
| 235 | }
|
|---|
| 236 | ```
|
|---|
| 237 |
|
|---|
| 238 | ##### Tables do not include additional border spacing
|
|---|
| 239 |
|
|---|
| 240 | ```css
|
|---|
| 241 | table {
|
|---|
| 242 | border-collapse: collapse;
|
|---|
| 243 | }
|
|---|
| 244 | ```
|
|---|
| 245 |
|
|---|
| 246 | ##### Textareas only resize vertically by default
|
|---|
| 247 |
|
|---|
| 248 | ```css
|
|---|
| 249 | textarea {
|
|---|
| 250 | resize: vertical;
|
|---|
| 251 | }
|
|---|
| 252 | ```
|
|---|
| 253 |
|
|---|
| 254 | ##### Single taps are dispatched immediately on clickable elements
|
|---|
| 255 |
|
|---|
| 256 | ```css
|
|---|
| 257 | a, area, button, input, label, select, summary, textarea, [tabindex] {
|
|---|
| 258 | -ms-touch-action: manipulation;
|
|---|
| 259 | touch-action: manipulation;
|
|---|
| 260 | }
|
|---|
| 261 | ```
|
|---|
| 262 |
|
|---|
| 263 | ##### ARIA roles include visual cursor hints
|
|---|
| 264 |
|
|---|
| 265 | ```css
|
|---|
| 266 | [aria-busy="true"] {
|
|---|
| 267 | cursor: progress;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | [aria-controls] {
|
|---|
| 271 | cursor: pointer;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | [aria-disabled="true"], [disabled] {
|
|---|
| 275 | cursor: default;
|
|---|
| 276 | }
|
|---|
| 277 | ```
|
|---|
| 278 |
|
|---|
| 279 | ##### Visually hidden content remains accessible
|
|---|
| 280 |
|
|---|
| 281 | ```css
|
|---|
| 282 | [aria-hidden="false"][hidden] {
|
|---|
| 283 | display: initial;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | [aria-hidden="false"][hidden]:not(:focus) {
|
|---|
| 287 | clip: rect(0, 0, 0, 0);
|
|---|
| 288 | position: absolute;
|
|---|
| 289 | }
|
|---|
| 290 | ```
|
|---|
| 291 |
|
|---|
| 292 | ---
|
|---|
| 293 |
|
|---|
| 294 | ## Forms
|
|---|
| 295 |
|
|---|
| 296 | [sanitize.css] includes a separate stylesheet for normalizing forms using
|
|---|
| 297 | minimal, standards-like styling.
|
|---|
| 298 |
|
|---|
| 299 | ```html
|
|---|
| 300 | <link href="https://unpkg.com/sanitize.css" rel="stylesheet" />
|
|---|
| 301 | <link href="https://unpkg.com/sanitize.css/forms.css" rel="stylesheet" />
|
|---|
| 302 | ```
|
|---|
| 303 |
|
|---|
| 304 | ### Forms Features
|
|---|
| 305 |
|
|---|
| 306 | ##### Form controls appear visually consistent and restyle consistently
|
|---|
| 307 |
|
|---|
| 308 | ```css
|
|---|
| 309 | button, input, select, textarea {
|
|---|
| 310 | background-color: transparent;
|
|---|
| 311 | border: 1px solid WindowFrame;
|
|---|
| 312 | color: inherit;
|
|---|
| 313 | font: inherit;
|
|---|
| 314 | letter-spacing: inherit;
|
|---|
| 315 | padding: 0.25em 0.375em;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | [type="color"],
|
|---|
| 319 | [type="range"] {
|
|---|
| 320 | border-width: 0;
|
|---|
| 321 | padding: 0;
|
|---|
| 322 | }
|
|---|
| 323 | ```
|
|---|
| 324 |
|
|---|
| 325 | ##### Expandable select controls appear visually consistent
|
|---|
| 326 |
|
|---|
| 327 | ```css
|
|---|
| 328 | select {
|
|---|
| 329 | -moz-appearance: none;
|
|---|
| 330 | -webkit-appearance: none;
|
|---|
| 331 | background: no-repeat right center / 1em;
|
|---|
| 332 | border-radius: 0;
|
|---|
| 333 | padding-right: 1em;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | select:not([multiple]):not([size]) {
|
|---|
| 337 | background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='4'%3E%3Cpath d='M4 0h6L7 4'/%3E%3C/svg%3E");
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | ::-ms-expand {
|
|---|
| 341 | display: none;
|
|---|
| 342 | }
|
|---|
| 343 | ```
|
|---|
| 344 |
|
|---|
| 345 | ##### Placeholders appear visually consistent in Internet Explorer
|
|---|
| 346 |
|
|---|
| 347 | ```css
|
|---|
| 348 | :-ms-input-placeholder {
|
|---|
| 349 | color: rgba(0, 0, 0, 0.54);
|
|---|
| 350 | }
|
|---|
| 351 | ```
|
|---|
| 352 |
|
|---|
| 353 | ## Assets
|
|---|
| 354 |
|
|---|
| 355 | [sanitize.css] includes a separate stylesheet for normalizing restricting the
|
|---|
| 356 | size of assets in all browsers.
|
|---|
| 357 |
|
|---|
| 358 | ```html
|
|---|
| 359 | <link href="https://unpkg.com/sanitize.css" rel="stylesheet" />
|
|---|
| 360 | <link href="https://unpkg.com/sanitize.css/assets.css" rel="stylesheet" />
|
|---|
| 361 | ```
|
|---|
| 362 |
|
|---|
| 363 | ### Assets Features
|
|---|
| 364 |
|
|---|
| 365 | ##### Assets use a comfortable measure in all browsers
|
|---|
| 366 |
|
|---|
| 367 | ```css
|
|---|
| 368 | iframe,
|
|---|
| 369 | img,
|
|---|
| 370 | input,
|
|---|
| 371 | select,
|
|---|
| 372 | textarea {
|
|---|
| 373 | height: auto;
|
|---|
| 374 | max-width: 100%;
|
|---|
| 375 | }
|
|---|
| 376 | ```
|
|---|
| 377 |
|
|---|
| 378 | ## Typography
|
|---|
| 379 |
|
|---|
| 380 | [sanitize.css] includes a separate stylesheet for normalizing typography using
|
|---|
| 381 | system interface fonts.
|
|---|
| 382 |
|
|---|
| 383 | ```html
|
|---|
| 384 | <link href="https://unpkg.com/sanitize.css" rel="stylesheet" />
|
|---|
| 385 | <link href="https://unpkg.com/sanitize.css/typography.css" rel="stylesheet" />
|
|---|
| 386 | ```
|
|---|
| 387 |
|
|---|
| 388 | ### Typography Features
|
|---|
| 389 |
|
|---|
| 390 | ##### Typography uses the default system font
|
|---|
| 391 |
|
|---|
| 392 | ```css
|
|---|
| 393 | html {
|
|---|
| 394 | font-family:
|
|---|
| 395 | system-ui,
|
|---|
| 396 | /* macOS 10.11-10.12 */ -apple-system,
|
|---|
| 397 | /* Windows 6+ */ Segoe UI,
|
|---|
| 398 | /* Android 4+ */ Roboto,
|
|---|
| 399 | /* Ubuntu 10.10+ */ Ubuntu,
|
|---|
| 400 | /* Gnome 3+ */ Cantarell,
|
|---|
| 401 | /* KDE Plasma 5+ */ Noto Sans,
|
|---|
| 402 | /* fallback */ sans-serif,
|
|---|
| 403 | /* macOS emoji */ "Apple Color Emoji",
|
|---|
| 404 | /* Windows emoji */ "Segoe UI Emoji",
|
|---|
| 405 | /* Windows emoji */ "Segoe UI Symbol",
|
|---|
| 406 | /* Linux emoji */ "Noto Color Emoji";
|
|---|
| 407 | }
|
|---|
| 408 | ```
|
|---|
| 409 |
|
|---|
| 410 | ##### Pre-formatted and code-formatted text uses the monospace system font
|
|---|
| 411 |
|
|---|
| 412 | ```css
|
|---|
| 413 | code, kbd, pre, samp {
|
|---|
| 414 | font-family:
|
|---|
| 415 | /* macOS 10.10+ */ Menlo,
|
|---|
| 416 | /* Windows 6+ */ Consolas,
|
|---|
| 417 | /* Android 4+ */ Roboto Mono,
|
|---|
| 418 | /* Ubuntu 10.10+ */ Ubuntu Monospace,
|
|---|
| 419 | /* KDE Plasma 5+ */ Noto Mono,
|
|---|
| 420 | /* KDE Plasma 4+ */ Oxygen Mono,
|
|---|
| 421 | /* Linux/OpenOffice fallback */ Liberation Mono,
|
|---|
| 422 | /* fallback */ monospace;
|
|---|
| 423 | }
|
|---|
| 424 | ```
|
|---|
| 425 |
|
|---|
| 426 | ## Reduce Motion
|
|---|
| 427 |
|
|---|
| 428 | [sanitize.css] includes a separate stylesheet for restricting motion when the
|
|---|
| 429 | user has requested this at a system level.
|
|---|
| 430 |
|
|---|
| 431 | ```html
|
|---|
| 432 | <link href="https://unpkg.com/sanitize.css" rel="stylesheet" />
|
|---|
| 433 | <link href="https://unpkg.com/sanitize.css/reduce-motion.css" rel="stylesheet" />
|
|---|
| 434 | ```
|
|---|
| 435 |
|
|---|
| 436 | ### Reduce Motion Features
|
|---|
| 437 |
|
|---|
| 438 | ##### Animations, scrolling effects, and transitions are reduced in all browsers
|
|---|
| 439 |
|
|---|
| 440 | ```css
|
|---|
| 441 | @media (prefers-reduced-motion: reduce) {
|
|---|
| 442 | *,
|
|---|
| 443 | ::before,
|
|---|
| 444 | ::after {
|
|---|
| 445 | animation-delay: -1ms !important;
|
|---|
| 446 | animation-duration: 1ms !important;
|
|---|
| 447 | animation-iteration-count: 1 !important;
|
|---|
| 448 | background-attachment: initial !important;
|
|---|
| 449 | scroll-behavior: auto !important;
|
|---|
| 450 | transition-delay: 0s !important;
|
|---|
| 451 | transition-duration: 0s !important;
|
|---|
| 452 | }
|
|---|
| 453 | }
|
|---|
| 454 | ```
|
|---|
| 455 |
|
|---|
| 456 | ## Contributing
|
|---|
| 457 |
|
|---|
| 458 | Please read the [contribution guidelines](CONTRIBUTING.md) in order to make the
|
|---|
| 459 | contribution process easy and effective for everyone involved.
|
|---|
| 460 |
|
|---|
| 461 | ## Acknowledgements
|
|---|
| 462 |
|
|---|
| 463 | sanitize.css is a project by [Jonathan Neal](https://github.com/jonathantneal),
|
|---|
| 464 | built upon normalize.css, a project by
|
|---|
| 465 | [Jonathan Neal](https://github.com/jonathantneal),
|
|---|
| 466 | co-created with [Nicolas Gallagher](https://github.com/necolas).
|
|---|
| 467 |
|
|---|
| 468 | [normalize.css]: https://github.com/csstools/normalize.css
|
|---|
| 469 | [reset.css]: http://meyerweb.com/eric/tools/css/reset/
|
|---|
| 470 | [sanitize.css]: https://github.com/csstools/sanitize.css
|
|---|