Generic CSS
These rules have very general selectors and hence apply to many elements. Together with custom properties they form the foundation of the design system.
The BasiCSS
Basic Box Reset
* {
margin: unset;
padding: unset;
border: unset;
}
Remove UA style margins, paddings, and borders. These basic box properties need to be redefined in any design system anyways.
Rationale
- The 8px body margin is a relic no longer needed.
- Generic
margin-block
is handled by flow spacing. - Generic
margin-inline
is handled by indentation. - Generic
padding-inline
is handled by indentation. - More specific boxes are handled on element level:
Related
Browser styles for reference [external]
Convenient Box Sizing
*,
*::before,
*::after {
box-sizing: border-box;
}
Set Box sizing default to border-box.
Target Scroll Spacing
:target {
// Give URL fragment target enough air.
scroll-margin-block-start: svar(length-3);
}
The current target element.
Abstract Coloring
* {
---clr-fg: var(---clr-pos);
---clr-bg: var(---clr-neg);
}
Set abstract colors.
Coloring Mixins
@mixin set-colors()
Set abstract color properties to the color specified by $name
and its counterpart (-contra
).
Parameters
$name: (String)
Name of the color token.
$default: null (String)
Name of the fall-back color token.
@mixin use-colors()
Use abstract color properties as color
and background-color
, respectively.
@mixin use-colors-inverse()
Use abstract color properties as background-color
and color
(inverse of use-colors
), respectively.
Embedding
Default Embedding Boxes
:is(img, picture, video, audio, embed, iframe, object, svg, canvas) {
display: block;
max-inline-size: 100%;
block-size: auto;
// Pictures in phrasing containers shall act like character boxes.
:is(h1, h2, h3, h4, h5, h6, p) & {
display: inline;
max-block-size: 1em;
}
}
Full-width Boxes
audio,
iframe:not([width]) {
inline-size: 100%;
}
Flow Spacing
Use common block margin to establish consistency in flow layout and to support a baseline grid.
Related
Recap of “The Stack” [external]
Flow Space Definition for General Blocks
:is(#{el.$blocks}, div) {
---flow-space: #{svar(spacing-flow-default)};
}
Flow Space Definition for Sections
:is(#{el.$sections}) {
---flow-space: #{svar(spacing-flow-sectioning)};
}
Flow Space Definition for Headings
:is(#{el.$headings}) {
---flow-space: #{svar(spacing-flow-heading, svar(spacing-flow-default))};
}
Flow Space Definition for List Items and Nested Lists
li,
li :is(#{el.$lists}) {
---flow-space: #{svar(spacing-flow-list, svar(spacing-flow-default))};
}
Flow Space Definition for Definition Terms
dt {
---flow-space: #{svar(
spacing-flow-defterm,
svar(spacing-flow-deflist, svar(spacing-flow-default))
)};
}
Flow Space Definition for Definition Data
dd {
---flow-space: #{svar(
spacing-flow-defdata,
svar(spacing-flow-deflist, svar(spacing-flow-default))
)};
}
Flow Space Applied Before General Blocks
* + :is(#{el.$blocks}),
:is(#{el.$blocks}) + div {
margin-block-start: var(---flow-space, 1em);
}
Flow Space Applied Around Sections
:is(#{el.$sections}) {
:where(&:not(:first-child)) {
margin-block-start: var(---flow-space);
}
:where(&:not(:last-child)) {
margin-block-end: var(---flow-space);
}
}
Indentation
List Root Indentation
:is(ol, ul, menu) {
---indent-root: #{svar(spacing-indent-list-root, 0)};
---indent: #{svar(spacing-indent-list, svar(spacing-indent-list-root, svar(spacing-indent-default)))};
padding-inline-start: var(---indent-root);
}
Indent list roots, i. e. top-level lists.
Custom properties:
---indent-root
{Number}: Indentation of the top level. If this is 0, the bullet points become hanging punctuation. ---indent
{Number}: Indentation of nested levels.
Nested Lists Indentation
li :is(ol, ul, menu) {
---indent: inherit;
padding-inline-start: var(---indent);
}
Indent nested lists. Inheritance allows to customize the indentation of the whole
ilst consistently by only changing the ---indent
property of the root list node.
Definition Details Indentation
dd {
---indent: #{svar(spacing-indent-deflist, svar(spacing-indent-default))};
margin-inline-start: var(---indent);
}
Indent definition details.
Block Quotations Indentation
blockquote {
---indent: #{svar(spacing-indent-quotation, svar(spacing-indent-default))};
margin-inline: var(---indent);
}
Indent quotation blocks symmetrically.
Figures Indentation
figure {
---indent: #{svar(spacing-indent-figure, svar(spacing-indent-default))};
margin-inline: var(---indent);
}
Indent figures symmetrically.
Generic Typography
Root Line Height and Font Size
html {
font-size: svar(font-size-root, 1rem);
line-height: svar(line-root);
}
The font size and line height of the root element.
Body Copy
body {
font-family: svar(font-family-body);
font-weight: svar(font-weight-body);
font-size: svar(font-size-0);
line-height: svar(line-0);
}
Fonts for body copy.
Headings
:is(h1, h2, h3, h4, h5, h6) {
font-family: svar(font-family-heading);
font-weight: svar(font-weight-heading);
}
Fonts for headings.
h#{$level}
h#{$level} {
font-size: svar(font-size-#{4 - $level});
line-height: svar(line-#{4 - $level});
}
Apply respective font-size and line-height to each heading level.
General Measure
:is(h1, h2, h3, h4, h5, h6),
:is(p, address, summary, figcaption, caption) {
max-inline-size: var(---measure, svar(measure-default));
}
Establish a generic measure.
Monospaced
:is(code, kbd, samp),
pre {
font-family: svar(font-family-mono);
}
Generic typography for monospaced elements.
Smaller
:is(small, sub, sup) {
font-size: svar(font-size-smaller);
}
Set scale-based font size.
Base Line Fix
:is(code, kbd, samp),
:is(small, sub, sup),
::marker {
line-height: 0;
}
Prevent certain inline (pseudo) elements from affecting the line height.