HTML & CSS code snippets with use cases
28 ready-made code building blocks, sorted by topic.
Structure
- Ordered and Unordered Lists β Mark up bullet lists, navigation and step-by-step instructions semantically.
- Definition List and Table β Present key-value data (properties, price lists, profiles) clearly.
- Table with Zebra Stripes β Present data lists such as orders or rankings in a readable way.
Components
- CSS Classes as a Component (BEM) β Build reusable UI building blocks such as buttons and cards with variants.
- Collapsible Section Without JavaScript β Present FAQ lists, additional info and mobile menus in a space-saving way.
- Modal Dialog with <dialog> β Confirmations and forms in an overlay β with correct focus management.
- Tooltip with ::after β Short additional info for icons or abbreviations without JavaScript.
Forms
- Form Validation with :invalid β Show users immediately which inputs are invalid, without JavaScript.
- Form Layout with Grid β Tidy contact or registration forms that also work on mobile.
- Toggle Switch with CSS Only β Present settings (dark mode, notifications) as a switch instead of a checkbox.
Layout
- Centering with Flexbox β Align content horizontally and vertically in the center β e.g. hero sections, loginβ¦
- Navigation Bar with Flexbox β Header with logo on the left, links in the middle and a login button on the right.
- Page Layout with Grid Areas β Classic layout with header, sidebar, content and footer β single-column on the smartphone.
- Responsive Card Grid Without a Media Query β Product cards, galleries or blog overviews that adapt automatically to any width.
- Sticky Header Row β Keep navigation or a table header visible while scrolling.
- Container Query Card β Components that adapt to the space available to them β no matter where they are used.
More snippets
- Minimal HTML Document β The starting point of every web page: document skeleton with language, character set andβ¦
- CSS Custom Properties β Define colors, spacing and radii centrally so that design changes happen in one place.
- @media, @supports and :has() β Apply styles only under conditions: screen width, browser support or content.
- Repetition with Grid repeat() and Counters β Display many similar elements in a grid and number them automatically, e.g. galleries orβ¦
- calc(), clamp() and var() as Building Blocks β Calculate fluid sizes and spacing instead of writing many media queries.
- Images, Links and Embeds β Correctly include images, downloads, internal anchors and embedded content.
- Images with a Fixed Aspect Ratio β Crop thumbnails and videos uniformly without layout shift.
- Hover Effect with Transition β Buttons and cards with smooth feedback for mouse and keyboard.
- Loading Indicator with @keyframes β Visualize waiting states (loading, saving) without a GIF or library.
- Dark Mode with prefers-color-scheme β Automatically adapt the page to the "dark" system setting.
- Accessible Button with Focus Style β Buttons that are clearly recognizable by keyboard and large enough.
- Truncate Text After n Lines β Shorten preview texts in cards to the same length (e.g. article teasers).