HTML & CSS: More snippets – code examples
- Minimal HTML Document
- CSS Custom Properties
- @media, @supports and :has()
- Repetition with Grid repeat() and Counters
- calc(), clamp() and var() as Building Blocks
- Images, Links and Embeds
- Images with a Fixed Aspect Ratio
- Hover Effect with Transition
- Loading Indicator with @keyframes
- Dark Mode with prefers-color-scheme
- Accessible Button with Focus Style
- Truncate Text After n Lines
Minimal HTML Document
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
<p>My first web page.</p>
</body>
</html>The minimum of a page: <!DOCTYPE html> activates standards mode, lang helps screen readers and translators, charset ensures correct umlauts and special characters, and the viewport tag ensures correct display on smartphones.
CSS Custom Properties
<!DOCTYPE html>
<style>
:root { --primaer: #e34f26; --abstand: 1rem; --radius: 10px; }
body { font-family: system-ui, sans-serif; margin: 12px; background: #fff; color: #222; }
.karte { padding: var(--abstand); border: 2px solid var(--primaer); border-radius: var(--radius); }
.karte.gruen { --primaer: seagreen; }
h2 { color: var(--primaer); margin-top: 0; }
</style>
<div class="karte"><h2>Default</h2>Uses <code>--primaer</code></div>
<br>
<div class="karte gruen"><h2>Overridden</h2>Locally <code>--primaer: seagreen</code></div>CSS has no classic variables, but custom properties: --name defines, var(--name) uses. They are inherited and can be overridden in subtrees – the basis for themes.
@media, @supports and :has()
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.box { padding: 14px; background: #ddd; margin-bottom: 8px; }
.box::after { content: " – schmaler Bildschirm"; }
@media (min-width: 420px) { .box { background: #cfe8ff; } .box::after { content: " – breiter Bildschirm"; } }
@supports (display: grid) { .raster { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; } }
@supports not (display: grid) { .raster { color: red; } }
.raster div { background: #fde; padding: 8px; }
.karte { border: 2px solid #ccc; padding: 8px; }
.karte:has(img, svg) { border-color: seagreen; }
</style>
<div class="box">@media (min-width: 420px)</div>
<div class="raster"><div>@supports grid</div><div>Cell 2</div></div>
<p class="karte">Card without an image (gray border)</p>
<p class="karte"><svg width="16" height="16"><circle cx="8" cy="8" r="7" fill="green"/></svg> Card with an image: :has() colors the border</p>CSS has no if, but it has conditions: @media checks environment/device, @supports checks feature support, and :has() selects elements depending on their content or state. Change the width of the preview to see @media.
Repetition with Grid repeat() and Counters
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.raster { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; counter-reset: nr; }
.raster div { counter-increment: nr; background: #cde; border-radius: 8px; padding: 18px 0; text-align: center; }
.raster div::before { content: "Nr. " counter(nr); font-weight: 700; }
</style>
<div class="raster">
<div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div>
</div>HTML/CSS has no loops: elements are written out individually in the HTML (or generated with JavaScript). But CSS repeats columns with repeat(4, 1fr) and counts elements automatically with counter-increment/counter().
calc(), clamp() and var() as Building Blocks
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
:root { --basis: 8px; }
.titel { font-size: clamp(1.2rem, 1rem + 3vw, 2.4rem); margin: 0; }
.box { padding: calc(var(--basis) * 2); width: min(100%, 22rem); background: #eef; margin-top: calc(var(--basis) * 2); }
.spalte { width: calc(100% - 40px); background: #fed; padding: 6px; margin-top: 8px; }
.max { width: max(50%, 12rem); background: #dfd; padding: 6px; margin-top: 8px; }
</style>
<h1 class="titel">Fluid Heading</h1>
<div class="box">Padding = 2 × --basis, width = min(100%, 22rem)</div>
<div class="spalte">calc(100% - 40px)</div>
<div class="max">max(50%, 12rem)</div>CSS functions are the substitute for function calls: calc() calculates with mixed units, min()/max() pick the smallest/largest value, clamp(min, ideal, max) limits fluid values, var() reads variables.
Images, Links and Embeds
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222} img { border-radius: 8px; vertical-align: middle; } figure { margin: 8px 0; }</style>
<!-- Image: alt is mandatory; here a data: URI instead of a file -->
<img width="64" height="64" alt="Blue circle with a star"
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='64' height='64'%3E%3Ccircle cx='32' cy='32' r='30' fill='%232b59c3'/%3E%3Ctext x='32' y='42' font-size='30' text-anchor='middle' fill='white'%3E%E2%98%85%3C/text%3E%3C/svg%3E">
<!-- Links: external, internal (anchor), download -->
<p><a href="https://developer.mozilla.org" target="_blank" rel="noopener noreferrer">External link</a> ·
<a href="#ziel">Jump down</a> ·
<a href="daten.csv" download>Download CSV</a></p>
<!-- Inline SVG: scalable, stylable with CSS -->
<svg width="60" height="30" role="img" aria-label="Green bar"><rect width="60" height="30" rx="6" fill="seagreen"/></svg>
<p id="ziel" style="margin-top:80px">Here is the anchor <code>#ziel</code>.</p>You include files by URL: <img src>, <a href>, <link href>, <script src>, <iframe src>. Here a data: URI and inline SVG replace the external file. External links with target="_blank" should also get rel="noopener".
Images with a Fixed Aspect Ratio
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.galerie { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.galerie img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 10px; background: #e5e7eb; }
</style>
<div class="galerie">
<img alt="Red area" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='100'%3E%3Crect width='300' height='100' fill='%23ef4444'/%3E%3C/svg%3E">
<img alt="Blue area" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='300'%3E%3Crect width='100' height='300' fill='%233b82f6'/%3E%3C/svg%3E">
<img alt="Green area" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Crect width='200' height='200' fill='%2310b981'/%3E%3C/svg%3E">
</div>aspect-ratio reserves the space before loading (no jumping), object-fit: cover crops images without distortion. The result: uniform tiles from source images of any shape.
Hover Effect with Transition
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.karte { display: inline-block; width: 170px; padding: 16px; margin: 8px; background: #fff; border-radius: 12px;
box-shadow: 0 2px 6px rgb(0 0 0 / 15%); transition: transform .25s ease, box-shadow .25s ease; }
.karte:hover, .karte:focus-visible { transform: translateY(-6px); box-shadow: 0 14px 24px rgb(0 0 0 / 22%); }
@media (prefers-reduced-motion: reduce) { .karte { transition: none; } }
</style>
<div class="karte" tabindex="0"><h3>Card 1</h3><p>Hover or Tab focus.</p></div>
<div class="karte" tabindex="0"><h3>Card 2</h3><p>Smooth transition.</p></div>transition interpolates between the normal and the :hover state. transform and box-shadow run smoothly; :focus-visible gives keyboard users the same effect, prefers-reduced-motion respects user preferences.
Loading Indicator with @keyframes
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.spinner { width: 42px; height: 42px; border: 5px solid #e5e7eb; border-top-color: #e34f26; border-radius: 50%;
animation: dreh .9s linear infinite; }
@keyframes dreh { to { transform: rotate(360deg); } }
.balken { width: 220px; height: 10px; background: #e5e7eb; border-radius: 99px; overflow: hidden; margin-top: 20px; }
.balken::after { content: ""; display: block; height: 100%; width: 40%; background: #2563eb; border-radius: 99px;
animation: laufen 1.2s ease-in-out infinite; }
@keyframes laufen { from { transform: translateX(-100%); } to { transform: translateX(260%); } }
@media (prefers-reduced-motion: reduce) { .spinner, .balken::after { animation-duration: 3s; } }
</style>
<div class="spinner" role="status" aria-label="Loading"></div>
<div class="balken" role="progressbar" aria-label="Progress"></div>@keyframes describes the sequence, animation assigns duration, timing and repetition. Animate only transform and opacity so it stays smooth. role="status" and aria-label make the loading state understandable for screen readers.
Dark Mode with prefers-color-scheme
<!DOCTYPE html>
<style>
:root { color-scheme: light dark; --bg: #ffffff; --text: #1a1a1a; --karte: #f1f3f9; --akzent: #2b59c3; }
@media (prefers-color-scheme: dark) { :root { --bg: #14161c; --text: #ececec; --karte: #232733; --akzent: #8fb0ff; } }
body { font-family: system-ui, sans-serif; margin: 12px; background: var(--bg); color: var(--text); }
.karte { background: var(--karte); padding: 1rem; border-radius: 10px; }
a { color: var(--akzent); }
input { font: inherit; padding: 6px; }
</style>
<div class="karte"><h3>Adapts automatically</h3>
<p>Switch between light and dark in the operating system. <a href="#x">A link</a></p>
<input placeholder="Input field (follows color-scheme)"></div>All colors live in variables; the media query overrides them for dark mode. color-scheme: light dark makes sure form elements and scrollbars switch along. For a manual toggle, you set a data-theme attribute instead.
Accessible Button with Focus Style
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.btn { font: inherit; font-weight: 600; padding: .7rem 1.3rem; min-height: 44px; border: 0; border-radius: 10px;
background: #1d4ed8; color: #fff; cursor: pointer; }
.btn:hover { background: #1e40af; }
.btn:focus-visible { outline: 3px solid #f59e0b; outline-offset: 3px; }
.btn:disabled { background: #9ca3af; cursor: not-allowed; }
</style>
<button class="btn">Save</button>
<button class="btn" disabled>Disabled</button>
<p>Press the Tab key to see the focus ring.</p>Native <button> elements are keyboard-operable. A visible :focus-visible ring, a 44 px minimum height (touch target) and sufficient contrast (white on #1d4ed8 ≈ 6:1) make it accessible.
Truncate Text After n Lines
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.teaser { width: 230px; padding: 12px; border: 1px solid #d1d5db; border-radius: 10px; }
.teaser p { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; margin: 0; }
.einzeilig { width: 230px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 12px; }
</style>
<div class="teaser"><h3 style="margin-top:0">Long preview</h3>
<p>CSS can truncate text after a certain number of lines with an ellipsis, without needing JavaScript. The rest of the text stays in the document and remains available to screen readers.</p></div>
<div class="einzeilig">This single-line heading is too long for the space and gets truncated.</div>-webkit-line-clamp (now supported in all browsers, also as line-clamp) truncates multi-line text. For a single line, text-overflow: ellipsis works with white-space: nowrap and overflow: hidden.