Sub topic 1
How Tailwind Utility Classes Map to CSS
Prerequisites
Assume you know basic HTML, the CSS box model, and units such as rem, px, and percentages. Tailwind CSS is already installed in your project.
How utility classes map to CSS
Tailwind uses a utility-first model. A utility class is a small class that applies one focused CSS rule. The class name describes the rule and its value, so the HTML shows the design decisions directly.
For example, p-4 means padding from the spacing scale, and text-center means centered text. Tailwind generates CSS for these classes and applies it to matching elements.
The spacing scale: one number for every length
Tailwind's spacing scale provides consistent values for padding, margin, and gaps. In the default scale, 1 is usually 0.25rem, 4 is 1rem, and 8 is 2rem. The number is a shared token, so p-4, mt-4, and gap-4 use the same length.
Padding and margin: p-, m-, axis, and side variants
Padding adds space inside an element. Margin adds space outside it. Use p-4 for all sides, px-6 for the horizontal axis, and py-4 for the vertical axis. Side variants include pt-, pr-, pb-, and pl-.
The m- family works the same way for margins. Prefix a margin with - for a negative value, such as -mb-2. Use mx-auto to set automatic left and right margins, commonly centering a width-limited block.
<div class="mx-auto mt-8 -mb-2 px-6 py-4">
Content
</div>Spacing children with gap and space-x/space-y
Use gap-4 on a flex or grid container to separate all its children. Use space-y-2 or space-x-2 when you need vertical or horizontal spacing between normal block siblings. Prefer gap for flex and grid layouts; space-* works by adding margins between adjacent children.
<ul class="flex gap-4">
<li>One</li>
<li>Two</li>
</ul>Exercise
Which class centers a block horizontally, and which class adds space between flex children?
Show answer
mx-auto centers the block when its width is constrained; gap-4 adds spacing between flex children.