CSS Layout and Tailwind Cheat Sheet
The layout rules and Tailwind patterns I reach for most, with the HTML content categories they depend on.
Overview
| Topic | Rule or snippet |
|---|---|
| Flow layout | the default: block elements stack vertically and take the full available width; inline elements sit side by side within a line |
| Flexbox | one-dimensional layout along a row or a column (display: flex) |
| Grid | two-dimensional layout in rows and columns (display: grid) |
position: fixed |
placed relative to the viewport; stays put while the page scrolls |
position: sticky |
scrolls normally, then sticks at an offset while its scrolling parent is in view |
position: absolute |
placed relative to the nearest positioned ancestor (relative, absolute…); removed from the flow |
| Two fixed-width grid columns (Tailwind) | grid grid-cols-[300px_600px] (underscores stand for spaces in arbitrary values) |
Details
HTML Content Categories
Elements belong to content categories that say where they may appear; some categories are subsets of others.
- Flow content: most elements used in the body (text, images, video, forms,
<header>,<main>,<footer>). - Sectioning content:
<article>,<section>,<nav>,<aside>, which group related content into outline sections.<header>,<main>and<footer>are flow content, not sectioning content. - Heading content:
<h1>to<h6>; headings may only contain phrasing content. - Phrasing content: text and text-level elements (
<strong>,<em>,<a>,<img>); if it fits inside a sentence, it is phrasing content.
Tooltip on Hover with Tailwind
<a href="#" class="relative group">
<img src="add-task-icon.svg" alt="Add task" width="24" height="24">
<span class="invisible group-hover:visible absolute bottom-full left-1/2 -translate-x-1/2
mb-2 px-2 py-1 text-sm text-white bg-black rounded whitespace-nowrap">
Add a new task
<span class="absolute top-full left-1/2 -translate-x-1/2 border-4 border-transparent
border-t-black"></span>
</span>
</a>
relative groupon the parent: positioning context, and the hover state children react to.invisible group-hover:visible: hidden until the parent is hovered.absolute bottom-full left-1/2 -translate-x-1/2 mb-2: centred above the icon with a small gap.- The inner span is the arrow: a transparent 4px border with only the top side coloured draws a triangle.
References
- MDN: Introduction to CSS layout - flow, flexbox, grid and positioning.
- MDN: Content categories - flow, sectioning, heading and phrasing content.
- Tailwind CSS documentation: Hover, focus and other states -
groupandgroup-hover. - Programming Languages MOC - hub for language and web notes.
Review cards
Flashcard What is the default layout for block elements in CSS?
Block elements stack vertically and take the full available width.
Flashcard How does position: sticky differ from position: fixed?
Sticky scrolls with the page until it reaches its offset, then sticks while its scrolling parent is in view. Fixed is placed relative to the viewport and never scrolls.
Flashcard How do you show a tooltip on hover with Tailwind?
Put group (and relative) on the parent, and invisible group-hover:visible absolute on the tooltip, so it stays hidden until the parent is hovered.