Skip to content

CSS and styling#

One can be forgiven for assuming that CSS is easy. After all, your stylesheets will probably work even when there are mistakes in them. However, this 'freedom' can be a trap.

It is crucial to do CSS right from the very beginning.

Otherwise, you will find yourself making a fix of a fix, and so on. Also, refactoring and enhancements will often affect code deeper down as well.

Styling structure#

To write more structured and uniform CSS code, so both you and your coworkers can more easily read and use your code. It even helps during development, because you are always aware of what CSS properties you have already used.

Using the same principles helps coworkers quickly find and understand the structure and meaning of your CSS code.

Rational CSS properties order#

  1. Start with the most influential and important properties first, then work progressively toward aesthetics and animations.
  2. Leave one empty line between these "logical" groups.

For the "logical" groups we recommend the following rational order principle:

.my-selector {
/* Positioning */
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
inset: 0;
z-index: 10;
display: flex;
/* Box Model */
width: 16rem;
height: 16rem;
margin: 2rem;
padding: 1rem;
color: #111;
/* Typography */
font:
normal 1rem Helvetica,
sans-serif;
line-height: 1.5rem;
text-align: left;
/* Visual */
background-color: #eee;
border: 1px solid #888;
border-radius: 0.25rem;
opacity: 1;
/* Animation */
transition: all 1s;
/* Misc */
user-select: none;
cursor: pointer;
}

You may check out this Prettier Plugin for handling it automatic with prettier.

Hover styles#

Touch browsers can keep :hover on the last tapped element until something else is tapped. Hover styles then stay visible, and can make a control look checked or selected when it isn't.

Use the shared utilities.hover() mixin, as Button does. It shows hover styles only on devices with a pointer that can hover (@media (any-hover: hover)), and not while the current input is touch or keyboard. Eufemia's shared input tracking sets data-whatintent on <html>: a tap hides hover styles, a key press keeps them hidden, and using the mouse brings them back. Typing in a form field preserves pointer intent so it does not change the surrounding hover styling. Before the input tracking has started, or without Eufemia's JavaScript, the media query alone decides.

@media (hover: hover) describes whether the primary pointer can hover. On a laptop with a touchscreen and a mouse, it can remain true while the touchscreen is being used. On an iPad with a trackpad, or an Android tablet with a mouse, it is false. any-hover is true when any connected pointer can hover, so the mixin uses it. Both describe a capability rather than the current input, so the mixin also checks the tracked input to follow each switch between touch, keyboard and mouse.

  • Keep pressed (:active) styles outside the hover mixin, so touch users still get feedback when they tap.
  • Keep focus styles outside it too, so keyboard users see focus on every device.
  • If a focus style excludes hovered elements with :not(:hover), also show it when the tracked intent is not mouse. A tapped element can keep :hover when the user returns with the keyboard.
  • Add :not(:active) inside the hover mixin when needed to keep hover styles from hiding the pressed style.
@use '@dnb/eufemia/style/core/utilities.scss' as utilities;
.my-toggle {
@include utilities.hover() {
&:not(:active) {
border-color: var(--token-color-stroke-action-hover);
}
}
@include utilities.active() {
border-color: var(--token-color-stroke-action-pressed);
}
}

For custom selectors that cannot use the mixin, put the rule inside @media (any-hover: hover) and use html:not([data-whatintent='touch'], [data-whatintent='keyboard']) &:hover. Keep this guard beside other input guards instead of nesting them, so they refer to the same <html> element.

CSS Units#

Here is a list of what we should use as layout and styling units to embrace the best possible accessibility experience and visual correctness.

  • rem: Use rem as a default sizing unit - as long as no other unit if preferred.
  • em: Use em only on complex layouts, whenever you need the sizes to respond to constraints.
  • px: Use pixels on visual helper lines and borders. Borders do not necessarily need to be responsive.
  • viewport units and percentage: Use these units to make layout and component widths responsive. Use also for placing and positioning layout wrappers which can give a better user experience.

Use em for CSS @media queries for the best browser compatibility. Read more about viewport units, Media Queries and breakpoints.

Suggest an edit
Eufemia · /web/development-guidelines/for-styling/ · Version [LOCAL BUILD]