HTML & CSS: Components – code examples
CSS Classes as a Component (BEM)
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.btn { display: inline-block; padding: .6rem 1.2rem; border: 2px solid transparent; border-radius: 8px;
font: inherit; font-weight: 600; cursor: pointer; background: #ddd; color: #222; }
.btn--primaer { background: #2b59c3; color: #fff; }
.btn--gefahr { background: #c62828; color: #fff; }
.btn--outline { background: transparent; border-color: #2b59c3; color: #2b59c3; }
.btn:hover { filter: brightness(.92); }
.btn--klein { padding: .3rem .7rem; font-size: .85rem; }
</style>
<button class="btn">Default</button>
<button class="btn btn--primaer">Primary</button>
<button class="btn btn--gefahr">Delete</button>
<button class="btn btn--outline btn--klein">Small outline</button>A base class (.btn) defines what is shared, modifiers (.btn--primaer) add variants – this is the BEM naming convention (Block, Element, Modifier). Classes are the "class/inheritance principle" of CSS.
Collapsible Section Without JavaScript
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
details { border: 1px solid #d1d5db; border-radius: 10px; padding: 0 14px; margin-bottom: 8px; max-width: 420px; }
summary { cursor: pointer; padding: 12px 0; font-weight: 700; }
details[open] { background: #f9fafb; border-color: #2563eb; }
details[open] summary { color: #2563eb; }
details p { margin-top: 0; }
</style>
<details open><summary>What is HTML?</summary><p>The markup language for web pages.</p></details>
<details><summary>What is CSS?</summary><p>The language for styling and layout.</p></details>
<details><summary>Do I need JavaScript?</summary><p>Not for this element – the browser takes care of everything, including keyboard control.</p></details><details>/<summary> are a native, accessible accordion (keyboard, state). With [open] you style the open state. With name="gruppe" on several details, only one opens at a time (modern browsers).
Modal Dialog with <dialog>
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
dialog { border: 0; border-radius: 14px; padding: 20px; max-width: 320px; box-shadow: 0 20px 50px rgb(0 0 0 / 35%); }
dialog::backdrop { background: rgb(15 23 42 / 55%); }
.btn { font: inherit; padding: 8px 14px; border-radius: 8px; border: 1px solid #94a3b8; background: #fff; cursor: pointer; }
.btn.primaer { background: #dc2626; color: #fff; border-color: #dc2626; }
</style>
<button class="btn" onclick="dlg.showModal()">Delete account …</button>
<dialog id="dlg" aria-labelledby="titel">
<h3 id="titel" style="margin-top:0">Really delete?</h3>
<p>This action cannot be undone.</p>
<form method="dialog"><button class="btn">Cancel</button> <button class="btn primaer" value="ja">Delete</button></form>
</dialog><dialog> brings an overlay (::backdrop), focus trap, Esc-to-close and the right role – without a library. showModal() opens it modally, form method="dialog" closes it on submit.
Tooltip with ::after
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
body { padding-top: 40px; }
.tip { position: relative; border-bottom: 1px dotted #555; cursor: help; }
.tip::after { content: attr(data-tip); position: absolute; bottom: 130%; left: 50%; transform: translateX(-50%);
background: #111827; color: #fff; padding: 6px 10px; border-radius: 6px; font-size: .8rem; white-space: nowrap;
opacity: 0; pointer-events: none; transition: opacity .15s; }
.tip:hover::after, .tip:focus::after { opacity: 1; }
</style>
<p>The abbreviation <span class="tip" tabindex="0" data-tip="Cascading Style Sheets">CSS</span> stands for styling.</p>content: attr(data-tip) reads the text from an attribute. The tooltip appears on hover and focus. For important information this is not accessible enough – there, better use visible text or aria-describedby.