| Previous | Notes index | Next |
|---|---|---|
| Previous: CSS architecture and modern CSS | Notes index | Next: Complete questions and answers |
Collected 105 code examples from 16 core chapters.
This chapter collects every fenced code example from the 16 core chapters. Each entry links to its source chapter so the surrounding explanation is easy to find. Examples are grouped by their original chapter and section.
p {
color: #334155;
line-height: 1.6;
}<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS notes</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<main>
<h1>Learning CSS</h1>
<p>Small style changes can make content easier to read.</p>
<a href="#details">Read the details</a>
</main>
</body>
</html>body {
margin: 0;
font-family: system-ui, sans-serif;
color: #1f2937;
background: #f8fafc;
}
main {
width: min(100% - 2rem, 42rem);
margin-inline: auto;
padding-block: 3rem;
}
a {
color: #075985;
}<p class="notice">Your changes have been saved.</p>p {
line-height: 1.5;
}
.notice {
padding: 1rem;
border: 1px solid #64748b;
background-color: #f1f5f9;
}h1,
h2,
h3 {
font-family: Georgia, serif;
}/* Keep the reading column comfortable on wide screens. */
.article {
max-width: 68ch;
}<article class="card">
<h2 class="card__title">CSS notes</h2>
<p>Patterns I want to remember.</p>
<a href="/notes">Open notes</a>
</article>/* Type selector */
article {
display: block;
}
/* Class selector */
.card {
padding: 1rem;
}
/* Descendant selector */
.card a {
color: #075985;
}
/* Attribute selector */
a[href^="/"] {
text-decoration-thickness: 0.08em;
}p { color: navy; } /* 0-0-1 */
.notice { color: darkgreen; } /* 0-1-0 */
article p.notice { color: maroon; } /* 0-1-2 */
#summary { color: purple; } /* 1-0-0 */@layer reset, components, utilities;
@layer components {
.card { color: navy; }
}
@layer utilities {
.text-muted { color: #475569; }
}.card {
width: 32rem;
opacity: 0.96;
color: rgb(30 41 59);
border-color: transparent;
}.hero {
min-height: 100vh;
min-height: 100svh;
}.page {
width: min(100% - 2rem, 72rem);
margin-inline: auto;
}
h1 {
font-size: clamp(2rem, 1.2rem + 3vw, 4rem);
}.card-list {
container-type: inline-size;
}
.card {
padding: clamp(1rem, 4cqi, 2rem);
}.palette {
color: darkslategray;
border-color: #475569;
background-color: rgb(248 250 252);
}
.button {
background-color: hsl(205 75% 32%);
color: oklch(98% 0.01 250);
}:root {
--color-text: #172033;
--color-muted: #475569;
--color-surface: #ffffff;
--color-accent: #075985;
}.article {
font-size: 1rem;
line-height: 1.65;
}*,
*::before,
*::after {
box-sizing: border-box;
}
.card {
width: 20rem;
padding: 1.25rem;
border: 2px solid #94a3b8;
}.article {
width: min(100% - 2rem, 68ch);
margin-inline: auto;
}.tag {
display: inline-block;
padding: 0.25rem 0.6rem;
border: 1px solid currentColor;
border-radius: 999px;
}
[hidden] {
display: none;
}.code-example {
max-width: 100%;
overflow: auto;
}
.avatar {
width: 4rem;
aspect-ratio: 1;
overflow: clip;
border-radius: 50%;
}.page-section {
padding-block: 2rem;
padding-inline: 1rem;
border-inline-start: 0.25rem solid #0f766e;
}:root {
font-family: system-ui, sans-serif;
color: #1e293b;
}
body {
margin: 0;
font-size: 1rem;
line-height: 1.6;
}
.article {
max-width: 68ch;
margin-inline: auto;
padding: 2rem 1rem;
}h1 {
max-width: 18ch;
margin-block: 0 1rem;
font-size: clamp(2.25rem, 1.5rem + 3vw, 4rem);
font-weight: 700;
line-height: 1.05;
letter-spacing: -0.03em;
text-wrap: balance;
}
p {
max-width: 68ch;
line-height: 1.65;
}a {
color: #075985;
text-decoration-line: underline;
text-decoration-thickness: 0.08em;
text-underline-offset: 0.18em;
}
.eyebrow {
font-size: 0.8rem;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}.message {
overflow-wrap: anywhere;
}
h2 {
text-wrap: balance;
}
.article-copy {
text-wrap: pretty;
}.title {
font: italic 600 1.25rem / 1.3 system-ui, sans-serif;
}body {
font-size: 1rem;
line-height: 1.6;
}
small,
.caption {
font-size: 0.875rem;
line-height: 1.5;
}
h2 {
font-size: clamp(1.5rem, 1.2rem + 1.2vw, 2.25rem);
}.hero {
color: white;
background-color: #123047;
background-image:
linear-gradient(rgb(9 25 40 / 0.78), rgb(9 25 40 / 0.18)),
url("../images/coast.jpg");
background-position: center;
background-size: cover;
background-repeat: no-repeat;
}<img class="profile-photo" src="profile.jpg" alt="Ashish Ranjan">.profile-photo {
width: 12rem;
aspect-ratio: 1;
object-fit: cover;
object-position: center 35%;
border-radius: 50%;
}.notice {
background-color: #0f766e;
background-image: linear-gradient(135deg, #0f766e, #164e63);
}.card {
border: 1px solid #cbd5e1;
border-radius: 0.75rem;
}
.card--selected {
border-color: currentColor;
}.card {
box-shadow: 0 0.5rem 1.5rem rgb(15 23 42 / 0.12);
}
.inset-field {
box-shadow: inset 0 1px 2px rgb(15 23 42 / 0.12);
}a:focus-visible,
button:focus-visible {
outline: 3px solid #f97316;
outline-offset: 3px;
}<nav class="site-nav" aria-label="Main navigation">
<a class="site-nav__brand" href="/">Study notes</a>
<div class="site-nav__links">
<a href="/css">CSS</a>
<a href="/html">HTML</a>
<a href="/javascript">JavaScript</a>
</div>
</nav>.site-nav {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding: 1rem;
}
.site-nav__links {
display: flex;
flex-wrap: wrap;
gap: 0.75rem 1rem;
}.card-list {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 16rem;
}.sidebar {
flex: 0 0 16rem;
}
.main-content {
flex: 1 1 0;
min-width: 0;
}.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
}
.toolbar__actions {
display: flex;
align-items: center;
gap: 0.5rem;
}.toolbar__account {
margin-inline-start: auto;
}.card-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 1rem;
}.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1rem;
}.dashboard {
display: grid;
grid-template-columns: minmax(14rem, 1fr) 3fr;
grid-template-rows: auto 1fr auto;
gap: 1rem 1.5rem;
}.gallery {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
grid-auto-rows: minmax(8rem, auto);
}.featured-card {
grid-column: span 2;
}.page-layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 18rem;
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
gap: 1rem;
}
.page-header { grid-area: header; }
.page-main { grid-area: main; }
.page-sidebar { grid-area: sidebar; }
.page-footer { grid-area: footer; }@media (max-width: 48rem) {
.page-layout {
grid-template-columns: minmax(0, 1fr);
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
}<article class="card">
<span class="card__badge">New</span>
<h2>CSS layout notes</h2>
</article>.card {
position: relative;
padding: 1.5rem;
border: 1px solid #cbd5e1;
}
.card__badge {
position: absolute;
inset-block-start: 0.75rem;
inset-inline-end: 0.75rem;
}.section-nav {
position: sticky;
inset-block-start: 0;
z-index: 10;
background: white;
}.page-header {
position: sticky;
inset-block-start: 0;
z-index: 20;
}
.card {
position: relative;
z-index: 1;
}
.card__menu {
position: absolute;
z-index: 2;
}<meta name="viewport" content="width=device-width, initial-scale=1">.page {
width: min(100% - 2rem, 72rem);
margin-inline: auto;
}
img,
video {
max-width: 100%;
height: auto;
}.article-layout {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 2rem;
}
@media (min-width: 52rem) {
.article-layout {
grid-template-columns: minmax(0, 1fr) 18rem;
}
}.resource-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 17rem), 1fr));
gap: 1rem;
}.profile-card-list {
container-type: inline-size;
}
.profile-card {
display: grid;
gap: 1rem;
}
@container (min-width: 34rem) {
.profile-card {
grid-template-columns: 8rem minmax(0, 1fr);
align-items: center;
}
}@media print {
nav,
.screen-only {
display: none;
}
body {
color: black;
background: white;
}
a {
color: inherit;
text-decoration: underline;
}
}.card {
transition: transform 160ms ease;
}
@media (prefers-reduced-motion: reduce) {
.card {
transition: none;
}
}a:hover {
color: #0f766e;
}
button:focus-visible {
outline: 3px solid #f97316;
outline-offset: 3px;
}
input:disabled {
cursor: not-allowed;
opacity: 0.65;
}
.menu-item:first-child {
border-block-start: 0;
}.external-link::after {
content: "!�";
font-size: 0.85em;
}
.article li::marker {
color: #0f766e;
}
.article p::selection {
color: white;
background: #0f766e;
}<form>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<p class="field-hint">We will send the confirmation to this address.</p>
<button type="submit">Continue</button>
</form>input,
select,
textarea,
button {
font: inherit;
}
input,
select,
textarea {
max-width: 100%;
padding: 0.65rem 0.75rem;
border: 1px solid #64748b;
border-radius: 0.35rem;
}
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible {
outline: 3px solid #f97316;
outline-offset: 2px;
}
input:invalid {
border-color: #b91c1c;
}
input[type="checkbox"],
input[type="radio"] {
accent-color: #0f766e;
}.button:hover {
background-color: #134e4a;
}
.button:focus-visible {
outline: 3px solid #f97316;
outline-offset: 3px;
}
.button:active {
transform: translateY(1px);
}.icon {
transform: translateY(-0.1rem) rotate(-8deg);
transform-origin: center;
}
.card:hover {
transform: translateY(-0.2rem);
}.button {
color: white;
background-color: #0f766e;
transform: translateY(0);
transition:
background-color 160ms ease,
transform 160ms ease;
}
.button:hover {
background-color: #115e59;
transform: translateY(-2px);
}@keyframes progress-pulse {
0% {
opacity: 0.55;
transform: scaleX(0.35);
}
100% {
opacity: 1;
transform: scaleX(1);
}
}
.progress-indicator {
transform-origin: left;
animation: progress-pulse 800ms ease-in-out infinite alternate;
}.panel {
transition: opacity 180ms ease, transform 180ms ease;
}
@media (prefers-reduced-motion: reduce) {
.panel {
transition: none;
}
.progress-indicator {
animation: none;
}
}:root {
--color-text: #172033;
--color-surface: #ffffff;
--color-accent: #0f766e;
--space-card: 1.25rem;
--radius-card: 0.75rem;
}
.card {
padding: var(--space-card);
color: var(--color-text);
background: var(--color-surface);
border-radius: var(--radius-card);
}
.card--quiet {
--color-surface: #f1f5f9;
}.button {
color: var(--button-text, white);
background-color: var(--button-background, #0f766e);
border-color: var(--button-border, currentColor);
}.content {
width: calc(100% - 2rem);
max-width: 72rem;
margin-inline: auto;
}
.section {
padding-inline: clamp(1rem, 4vw, 3rem);
}
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
}:root {
--brand: oklch(52% 0.12 185);
--brand-soft: color-mix(in oklab, var(--brand), white 82%);
}
.badge {
color: var(--brand);
background-color: var(--brand-soft);
}.badge {
background-color: #e6f2ef;
background-color: color-mix(in oklab, var(--brand), white 82%);
}@property --progress {
syntax: "<number>";
inherits: false;
initial-value: 0;
}
.progress-bar {
transform: scaleX(var(--progress));
transform-origin: left;
}:root {
color-scheme: light;
--surface: #ffffff;
--text: #172033;
}
[data-theme="dark"] {
color-scheme: dark;
--surface: #111827;
--text: #f8fafc;
}
body {
color: var(--text);
background-color: var(--surface);
}:where(a, button, input, select, textarea, summary):focus-visible {
outline: 3px solid #f97316;
outline-offset: 3px;
}
section[id],
h2[id] {
scroll-margin-block-start: 5rem;
}.field-error {
color: #b91c1c;
border-inline-start: 0.25rem solid currentColor;
padding-inline-start: 0.75rem;
}
.status--success::before {
content: "Success: ";
font-weight: 700;
}.notice {
min-height: 3rem;
height: auto;
padding: 0.75rem 1rem;
overflow-wrap: anywhere;
}
.page {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}.icon-button {
display: inline-grid;
min-width: 2.75rem;
min-height: 2.75rem;
place-items: center;
padding: 0.5rem;
}@media (forced-colors: active) {
.icon-button {
color: ButtonText;
background: ButtonFace;
border: 1px solid ButtonText;
}
.selected-item {
outline: 2px solid Highlight;
}
}@media (prefers-contrast: more) {
.card {
border: 2px solid currentColor;
}
:where(a, button, input):focus-visible {
outline-width: 4px;
}
}83. 14. Accessibility and inclusive CSS: Keep visually hidden text available
.visually-hidden:not(:focus, :active) {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}.card {
color: #172033;
}
.page .card {
color: #334155;
}
.card {
color: #0f766e;
}*,
*::before,
*::after {
box-sizing: border-box;
}
.panel {
width: 20rem;
padding: 1.5rem;
border: 1px solid #cbd5e1;
}.results {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 1rem;
}
.result {
min-width: 0;
overflow-wrap: anywhere;
}.article {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}
@media (min-width: 48rem) {
.article {
width: min(100% - 4rem, 70rem);
}
}.debug * {
outline: 1px solid rgb(255 0 0 / 35%);
}const viewportWidth = document.documentElement.clientWidth;
const overflowing = [...document.querySelectorAll("*")].filter((element) => {
return element.getBoundingClientRect().right > viewportWidth;
});
console.table(
overflowing.map((element) => ({
element,
right: Math.round(element.getBoundingClientRect().right),
width: Math.round(element.getBoundingClientRect().width),
})),
);.heading {
font-size: 2rem;
font-size: clamp(1.75rem, 4vw, 3rem);
}const cards = [...document.querySelectorAll(".card")];
const heights = cards.map((card) => card.getBoundingClientRect().height);
cards.forEach((card, index) => {
card.style.setProperty("--measured-height", String(heights[index]) + "px");
});.independent-widget {
contain: layout paint;
}.long-section {
content-visibility: auto;
contain-intrinsic-size: auto 36rem;
}.notice {
opacity: 0;
transform: translateY(0.5rem);
transition:
opacity 180ms ease,
transform 180ms ease;
}
.notice.is-visible {
opacity: 1;
transform: translateY(0);
}
@media (prefers-reduced-motion: reduce) {
.notice {
transition: none;
}
}.card-list {
display: block;
}
@supports (display: grid) {
.card-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1rem;
}
}styles/
reset.css
tokens.css
base.css
layout.css
components/
button.css
card.css
utilities.css
@layer reset, base, layout, components, utilities;
@layer reset {
*,
*::before,
*::after {
box-sizing: border-box;
}
}
@layer base {
body {
margin: 0;
color: #172033;
font-family: system-ui, sans-serif;
}
}
@layer components {
.button {
border: 0;
border-radius: 0.5rem;
padding: 0.75rem 1rem;
}
}.notice {
padding: 1rem;
border-inline-start: 0.25rem solid currentColor;
}
.notice--warning {
color: #7c2d12;
background: #ffedd5;
}
.notice[hidden] {
display: none;
}@scope (.profile-card) {
img {
display: block;
max-width: 100%;
}
h2 {
margin-block: 0 0.5rem;
}
}.card {
padding-block: 1rem;
padding-inline: 1.25rem;
border-inline-start: 0.25rem solid #0f766e;
margin-block-end: 1rem;
}.project-list {
container-type: inline-size;
}
.project-card {
display: grid;
gap: 1rem;
}
@container (min-width: 36rem) {
.project-card {
grid-template-columns: 10rem 1fr;
align-items: center;
}
}.directory {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 1rem;
}
.directory-card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
gap: 0.75rem;
}.button {
color: white;
background: #0f766e;
&:hover {
background: #115e59;
}
&:focus-visible {
outline: 3px solid #f97316;
outline-offset: 3px;
}
&.button--quiet {
color: #172033;
background: #e2e8f0;
}
}@import url("./vendor.css") layer(vendor);
@layer reset, vendor, base, components, utilities;.card-list {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
@supports (display: grid) {
.card-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr));
}
}