Utility Classes
- Single purpose classes, focused on ONE aspect of styling.
- Modular friendly so write a lot less code in the long run.
- Write CSS class first then utilize that CSS classname in HTML.
- Benefits: 1. Rapid development. 2. easier maintenance.
/* ------------------- */
/* Utility classes */
/* ------------------- */
/* general */
.flex {
display: flex;
gap: var(--gap, 1rem);
}
.grid {
display: grid;
gap: var(--gap, 1rem);
}
.container {
padding-inline: 2em;
margin-inline: auto;
max-width: 60rem;
}
Screen reader only for the accessibility. It doesn’t show, but it reads
/* ------------------- */
/* Utility classes */
/* ------------------- */
/* screen reader only */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap; /* added line */
border: 0;
}
html
<div class = "flex container">
<div class="box sr-only">a</div>
<div class="box">b</div>
<div class="box">c</div>
<div class="box">d</div>
</div>
Utility classes color
css
:root {
/* colors */
--clr-dark: 230 35% 7%;
--clr-light: 231 77% 90%;
--clr-white: 0 0% 100%;
}
/* colors */
.bg-dark { background-color: hsl( var(--clr-dark) );}
.bg-accent { background-color: hsl( var(--clr-light) );}
.bg-white { background-color: hsl( var(--clr-white) );}
.text-dark { color: hsl( var(--clr-dark) );}
.text-accent { color: hsl( var(--clr-light) );}
.text-white { color: hsl( var(--clr-white) );}
html
<div class="container">
<h1>Design system</h1>
<div class="bg-accent text-dark" style="padding: 1rem">
light blue bg, with dark text
</div>
<div class="bg-white text-dark" style="padding: 1rem">
white bg, with dark text
</div>
<div class="component-ex text-white" style="padding: 1rem">
light blue bg with a 20% alpha and white text
</div>
</div>
css
//CSS write the custom css for that element
.component-ex {
background-color: hsl( var(--clr-light) / .2);
}
Utility Class Typography
:root {
/* font-sizes */
/* 150px */
--fs-900: 9.375rem;
/* 100px */
--fs-800: 6.25rem;
/* 56px */
--fs-700: 3.5rem;
/* 32px */
--fs-600: 2rem;
/* 28px */
--fs-500: 1.75rem;
/* 18px */
--fs-400: 1.125rem;
/* 16px */
--fs-300: 1rem;
/* 14px */
--fs-200: 0.875rem;
/* font-families */
--ff-serif: "Bellefair", serif;
--ff-sans-cond: "Barlow Condensed", sans-serif;
--ff-sans-normal: "Barlow", sans-serif;
}
/* typography */
.ff-serif { font-family: var(--ff-serif); }
.ff-sans-cond { font-family: var(--ff-sans-cond); }
.ff-sans-normal { font-family: var(--ff-sans-normal); }
.letter-spacing-1 { letter-spacing: 4.75px; }
.letter-spacing-2 { letter-spacing: 2.7px; }
.letter-spacing-3 { letter-spacing: 2.35px; }
.uppercase { text-transform: uppercase; }
.fs-900 { font-size: var(--fs-900); }
.fs-800 { font-size: var(--fs-800); }
.fs-700 { font-size: var(--fs-700); }
.fs-600 { font-size: var(--fs-600); }
.fs-500 { font-size: var(--fs-500); }
.fs-400 { font-size: var(--fs-400); }
.fs-300 { font-size: var(--fs-300); }
.fs-200 { font-size: var(--fs-200); }
.fs-900,
.fs-800,
.fs-700,
.fs-600 {
line-height: 1.1;
}
custom utility class for numbered title
//CSS
.numbered-title {
font-family: var(--ff-sans-cond);
font-size: var(--fs-500);
text-transform: uppercase;
letter-spacing: 4.72px;
}
.numbered-title span {
margin-right: .5em;
font-weight: 700;
color: hsl( var(--clr-white) / .25);
}
html
<h2 class="numbered-title"><span>01</span> colors</h2>
<h2 class="numbered-title"><span>02 </span>Typography</h2>
in one place:
css
/* ------------------- */
/* ---utility class--- */
/* ------------------- */
/* ------------------- */
/* ------------------- */
/* Custom properties */
/* ------------------- */
:root {
/* colors */
--clr-dark: 230 35% 7%;
--clr-light: 231 77% 90%;
--clr-white: 0 0% 100%;
/* font-sizes */
/* 150px */
--fs-900: 9.375rem;
/* 100px */
--fs-800: 6.25rem;
/* 56px */
--fs-700: 3.5rem;
/* 32px */
--fs-600: 2rem;
/* 28px */
--fs-500: 1.75rem;
/* 18px */
--fs-400: 1.125rem;
/* 16px */
--fs-300: 1rem;
/* 14px */
--fs-200: 0.875rem;
/* font-families */
--ff-serif: "Bellefair", serif;
--ff-sans-cond: "Barlow Condensed", sans-serif;
--ff-sans-normal: "Barlow", sans-serif;
}
/* ------------------- */
/* Reset */
/* ------------------- */
/* https://piccalil.li/blog/a-modern-css-reset/ */
/* Box sizing */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Reset margins */
body,
h1,
h2,
h3,
h4,
h5,
p,
figure,
picture {
margin: 0;
}
h1,
h2,
h3,
h4,
h5,
h6,
p {
font-weight: 400;
}
/* set up the body */
body {
font-family: var(--ff-sans-normal);
font-size: var(--fs-400);
color: hsl( var(--clr-white) );
background-color: hsl( var(--clr-dark) );
line-height: 1.5;
min-height: 100vh;
}
/* make images easier to work with */
img,
picutre {
max-width: 100%;
display: block;
}
/* make form elements easier to work with */
input,
button,
textarea,
select {
font: inherit;
}
/* remove animations for people who've turned them off */
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
/* ------------------- */
/* Utility classes */
/* ------------------- */
/* general */
.flex {
display: flex;
gap: var(--gap, 1rem);
}
.grid {
display: grid;
gap: var(--gap, 1rem);
}
.flow > * + * {
margin-top: 1rem;
outline: 1px solid red;
}
.container {
padding-inline: 2em;
margin-inline: auto;
max-width: 80rem;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap; /* added line */
border: 0;
}
/* colors */
.bg-dark { background-color: hsl( var(--clr-dark) );}
.bg-accent { background-color: hsl( var(--clr-light) );}
.bg-white { background-color: hsl( var(--clr-white) );}
.text-dark { color: hsl( var(--clr-dark) );}
.text-accent { color: hsl( var(--clr-light) );}
.text-white { color: hsl( var(--clr-white) );}
/* typography */
.ff-serif { font-family: var(--ff-serif); }
.ff-sans-cond { font-family: var(--ff-sans-cond); }
.ff-sans-normal { font-family: var(--ff-sans-normal); }
.letter-spacing-1 { letter-spacing: 4.75px; }
.letter-spacing-2 { letter-spacing: 2.7px; }
.letter-spacing-3 { letter-spacing: 2.35px; }
.uppercase { text-transform: uppercase; }
.fs-900 { font-size: var(--fs-900); }
.fs-800 { font-size: var(--fs-800); }
.fs-700 { font-size: var(--fs-700); }
.fs-600 { font-size: var(--fs-600); }
.fs-500 { font-size: var(--fs-500); }
.fs-400 { font-size: var(--fs-400); }
.fs-300 { font-size: var(--fs-300); }
.fs-200 { font-size: var(--fs-200); }
.fs-900,
.fs-800,
.fs-700,
.fs-600 {
line-height: 1.1;
}
Reference: Kevin Powell!! https://v2.scrimba.com/build-a-space-travel-website-c014/~0c