# Atomic CSS ## What is Atomic CSS? Atomic CSS is an approach to styling that reduces payload sizes for style delivery, and allows style composition and reuse easily. This document describes the concept of Atomic CSS, its advantage and use cases. Atomic CSS is a way of writing CSS such that each CSS rule has exactly one declaration (an "atom"). For example, ```css /** A normal class */ .myClass { background: red; width: 100%; height: 100%; } /** Can be written atomically as: */ .a { background: red; } .b { width: 100%; } .c { height: 100%; } ``` ## Usage in Linaria Atomic styles do not require a separate config option. Use them by importing the `css` template literal from `@linaria/atomic`: ```tsx import { cx } from '@linaria/core'; import { css } from '@linaria/atomic'; const atomicCss = css` background: red; width: 100%; height: 100%; border: 1px solid black; `; const blueBackground = css` background: blue; border: 1px solid black; `; // In React:
; // In vanilla JS: const div = document.createElement('div'); div.setAttribute('class', cx(atomicCss, blueBackground)); document.body.appendChild(div); ``` Which at build time, is transformed into: ```ts import { cx } from '@linaria/core'; import { css } from '@linaria/atomic'; const atomicCss = 'atm_background_abcd atm_width_efgh atm_height_ijkl atm_border_mnop'; const blueBackground = 'atm_background_qrst atm_border_mnop'; // In React:
; //
// In vanilla JS: const div = document.createElement('div'); div.setAttribute('class', cx(atomicCss, blueBackground)); // same as React example document.body.appendChild(div); ``` (Note: in the example above, the slugs in the atoms are lengthened for readability) The format of these atoms is `atm_${propertySlug}_${valueSlug}` which lets us deduplicate based on the `propertySlug` part of the atom. As you can see in the above example, `atm_border_mnop` can be removed as it duplicated, and we see two atoms with the `background` property slug, and can remove one of them. ### at-rules, pseudo classes and keyframes Linaria's atomic css also supports creating (nested) at-rules, pseudo classes and keyframes: ```ts import { css } from '@linaria/atomic'; // Note: animation name namespacing does not happen automatically with @linaria/atomic. Keyframe animations are pulled out to the top level and not atomized. export const animation = css` @keyframes my-animation { from { opacity: 0; } to { opacity: 1; } } animation: my-animation 1s infinite; `; export const pseudoClass = css` &:hover { color: pink; } `; export const mediaQuery = css` @media (max-width: 400px) { background: orange; } `; ``` These can also be combined for further nesting. ### Property priorities Using atomic CSS, longhand properties such as `padding-top` have a _higher_ priority than their shorthand equivalents like `padding`. For example: ```ts import { css } from '@linaria/atomic'; const noPadding = css` padding: 0; `; const paddingTop = css` padding-top: 5px: `; // In react:
...
; ``` The result will be that the div has `padding-top: 5px;`, as that is higher priority than `padding: 0`. The way linaria achieves this is through property priorities. See [this blog post](https://weser.io/blog/the-shorthand-longhand-problem-in-atomic-css) for more details on the concept, and the problems it solves. The method used in linaria is to increase the specificity of the rules: see `@linaria/atomic`'s `propertyPriority` function for a list of longhand and shorthand properties supported by this. The basic rules are: - Longhand properties have higher priority than shorthand properties - Declarations in @media rules (and any @-rule, such as @supports) have higher priority than those outside of them ## Use cases ### Reducing number of rules One advantage of writing styles in this way is that we can reuse CSS more effectively. In many cases, declarations are repeated in CSS, and atomic CSS allows heavy reuse of these classes. For example if we have the classes, ```html