Typography and Spacing
Typography in Tailwind CSS
Typography is a fundamental aspect of web design, and Tailwind provides a comprehensive set of utilities to control text styling, including font family, size, weight, line height, letter spacing, and more.
Font Family
Tailwind provides several default font family utilities:
<!-- Sans-serif font (default UI font) -->
<p class="font-sans">This text uses a sans-serif font.</p>
<!-- Serif font -->
<p class="font-serif">This text uses a serif font.</p>
<!-- Monospace font -->
<p class="font-mono">This text uses a monospace font.</p>
Custom Font Families
To use custom fonts, first import them (e.g., from Google Fonts), then configure them in your tailwind.config.js:
// tailwind.config.js
module.exports = {
theme: {
extend: {
fontFamily: {
display: ["Poppins", "sans-serif"],
body: ["Inter", "sans-serif"],
heading: ["Playfair Display", "serif"],
code: ["Fira Code", "monospace"],
},
},
},
};
Then use them in your HTML:
<h1 class="font-heading">Beautiful Heading</h1>
<p class="font-body">This is the body text using Inter.</p>
<code class="font-code">const code = 'formatted';</code>
Font Size
Tailwind provides a range of font size utilities:
<p class="text-xs">Extra small text (12px)</p>
<p class="text-sm">Small text (14px)</p>
<p class="text-base">Base text (16px)</p>
<p class="text-lg">Large text (18px)</p>
<p class="text-xl">Extra large text (20px)</p>
<p class="text-2xl">2XL text (24px)</p>
<p class="text-3xl">3XL text (30px)</p>
<!-- ... up to text-9xl -->
Font Size with Line Height
In Tailwind v2.0+, font size utilities also set appropriate line heights by default. You can customize this in your config:
// tailwind.config.js
module.exports = {
theme: {
extend: {
fontSize: {
base: ["16px", "24px"], // 16px font size, 24px line height
lg: ["18px", "28px"],
xl: ["20px", "32px"],
// More complex configuration with letter spacing
display: [
"64px",
{
lineHeight: "72px",
letterSpacing: "-0.02em",
fontWeight: "700",
},
],
},
},
},
};
Font Weight
Control the font weight with these utilities:
<p class="font-thin">Thin text (100)</p>
<p class="font-extralight">Extra light text (200)</p>
<p class="font-light">Light text (300)</p>
<p class="font-normal">Normal text (400)</p>
<p class="font-medium">Medium text (500)</p>
<p class="font-semibold">Semi-bold text (600)</p>
<p class="font-bold">Bold text (700)</p>
<p class="font-extrabold">Extra bold text (800)</p>
<p class="font-black">Black text (900)</p>
Text Alignment
Align text with these utilities:
<p class="text-left">Left-aligned text</p>
<p class="text-center">Center-aligned text</p>
<p class="text-right">Right-aligned text</p>
<p class="text-justify">
Justified text that aligns along the left and right edges
</p>
Text Color
Tailwind provides a comprehensive color palette for text:
<p class="text-black">Black text</p>
<p class="text-white">White text</p>
<p class="text-gray-500">Medium gray text</p>
<p class="text-red-600">Bright red text</p>
<p class="text-blue-700">Dark blue text</p>
<p class="text-green-500">Medium green text</p>
<!-- And many more colors with different intensities (100-900) -->
Text Opacity
You can also control text opacity:
<p class="text-blue-500 text-opacity-100">100% opacity</p>
<p class="text-blue-500 text-opacity-75">75% opacity</p>
<p class="text-blue-500 text-opacity-50">50% opacity</p>
<p class="text-blue-500 text-opacity-25">25% opacity</p>
Text Decoration
Add underlines, line-throughs, and more:
<p class="underline">Underlined text</p>
<p class="line-through">Strikethrough text</p>
<p class="no-underline">No underline (removes underline)</p>
<!-- Decoration color -->
<p class="underline decoration-blue-500">Blue underline</p>
<!-- Decoration style -->
<p class="underline decoration-dotted">Dotted underline</p>
<p class="underline decoration-dashed">Dashed underline</p>
<p class="underline decoration-wavy">Wavy underline</p>
<!-- Decoration thickness -->
<p class="underline decoration-2">Thicker underline</p>
<p class="underline decoration-4">Even thicker underline</p>
Text Transform
Change text case with these utilities:
<p class="uppercase">all uppercase</p>
<p class="lowercase">ALL LOWERCASE</p>
<p class="capitalize">capitalize each word</p>
<p class="normal-case">Normal case (resets transformations)</p>
Letter Spacing
Control the spacing between letters:
<p class="tracking-tighter">Tighter letter spacing</p>
<p class="tracking-tight">Tight letter spacing</p>
<p class="tracking-normal">Normal letter spacing</p>
<p class="tracking-wide">Wide letter spacing</p>
<p class="tracking-wider">Wider letter spacing</p>
<p class="tracking-widest">Widest letter spacing</p>
Line Height
Control the line height (leading):
<p class="leading-none">Leading none (1)</p>
<p class="leading-tight">Tight leading (1.25)</p>
<p class="leading-snug">Snug leading (1.375)</p>
<p class="leading-normal">Normal leading (1.5)</p>
<p class="leading-relaxed">Relaxed leading (1.625)</p>
<p class="leading-loose">Loose leading (2)</p>
<!-- Fixed line heights -->
<p class="leading-3">0.75rem line height</p>
<p class="leading-4">1rem line height</p>
<!-- ... and so on -->
Text Overflow
Control how overflowing text is handled:
<!-- Truncate text with ellipsis -->
<p class="truncate w-48">
This text is too long and will be truncated with an ellipsis.
</p>
<!-- Allow text to wrap -->
<p class="overflow-auto w-48">
This text will wrap to the next line when it reaches the width of the
container.
</p>
<!-- Prevent text from wrapping -->
<p class="whitespace-nowrap overflow-hidden w-48">
This text won't wrap and will be clipped.
</p>
Vertical Alignment
Align text vertically within a line:
<span class="align-baseline">baseline</span>
<span class="align-top">top</span>
<span class="align-middle">middle</span>
<span class="align-bottom">bottom</span>
<span class="align-text-top">text-top</span>
<span class="align-text-bottom">text-bottom</span>
Typography Plugin
For more advanced typography control, especially for content-heavy sites, Tailwind offers the official Typography plugin:
npm install @tailwindcss/typography
Add to your configuration:
// tailwind.config.js
module.exports = {
plugins: [require("@tailwindcss/typography")],
};
Then use the prose classes for beautifully styled content:
<article class="prose lg:prose-xl">
<h1>Article Heading</h1>
<p>
This is a paragraph with <strong>bold text</strong> and
<em>italic text</em>.
</p>
<ul>
<li>List item one</li>
<li>List item two</li>
</ul>
<blockquote>This is a blockquote</blockquote>
</article>
Spacing in Tailwind CSS
Tailwind provides a comprehensive spacing system for margin, padding, gap, and more. The default spacing scale is based on a 0.25rem (4px) grid.
Padding
Add padding to elements:
<!-- All sides -->
<div class="p-0">No padding</div>
<div class="p-1">0.25rem padding (4px)</div>
<div class="p-2">0.5rem padding (8px)</div>
<div class="p-4">1rem padding (16px)</div>
<div class="p-8">2rem padding (32px)</div>
<!-- Individual sides -->
<div class="pt-4">Padding top</div>
<div class="pr-4">Padding right</div>
<div class="pb-4">Padding bottom</div>
<div class="pl-4">Padding left</div>
<!-- Horizontal and vertical -->
<div class="px-4">Padding left and right</div>
<div class="py-4">Padding top and bottom</div>
Margin
Add margin to elements:
<!-- All sides -->
<div class="m-0">No margin</div>
<div class="m-1">0.25rem margin (4px)</div>
<div class="m-2">0.5rem margin (8px)</div>
<div class="m-4">1rem margin (16px)</div>
<div class="m-8">2rem margin (32px)</div>
<!-- Individual sides -->
<div class="mt-4">Margin top</div>
<div class="mr-4">Margin right</div>
<div class="mb-4">Margin bottom</div>
<div class="ml-4">Margin left</div>
<!-- Horizontal and vertical -->
<div class="mx-4">Margin left and right</div>
<div class="my-4">Margin top and bottom</div>
<!-- Auto margins (for centering) -->
<div class="mx-auto w-1/2">Horizontally centered with auto margins</div>
Negative Margins
Tailwind also supports negative margins for advanced layouts:
<div class="-m-4">Negative margin on all sides</div>
<div class="-mt-4">Negative margin top</div>
<div class="-mr-4">Negative margin right</div>
<!-- And so on... -->
Space Between
The space-x and space-y utilities add spacing between child elements:
<!-- Horizontal spacing between children -->
<div class="flex space-x-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<!-- Vertical spacing between children -->
<div class="flex flex-col space-y-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<!-- Reverse spacing direction -->
<div class="flex flex-row-reverse space-x-4 space-x-reverse">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
Gap (for Grid and Flex)
The gap utilities control spacing between grid and flex items:
<!-- Grid with gap -->
<div class="grid grid-cols-3 gap-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<!-- Different row and column gaps -->
<div class="grid grid-cols-3 gap-x-8 gap-y-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<!-- Flex with gap -->
<div class="flex gap-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
Customizing the Spacing Scale
You can customize the spacing scale in your tailwind.config.js:
// tailwind.config.js
module.exports = {
theme: {
spacing: {
// Override the entire spacing scale
0: "0",
1: "4px",
2: "8px",
3: "12px",
4: "16px",
5: "20px",
6: "24px",
// ... and so on
},
// Or extend the default scale
extend: {
spacing: {
72: "18rem",
84: "21rem",
96: "24rem",
128: "32rem",
},
},
},
};
Visual Layout Diagram
Padding and Margin Visualization:
┌───────────────────────────────────────────┐
│ Margin │
│ ┌───────────────────────────────────┐ │
│ │ Padding │ │
│ │ ┌───────────────────────────┐ │ │
│ │ │ │ │ │
│ │ │ Content │ │ │
│ │ │ │ │ │
│ │ └───────────────────────────┘ │ │
│ │ │ │
│ └───────────────────────────────────┘ │
│ │
└───────────────────────────────────────────┘
Classes:
- Content: The element itself
- Padding: p-{size}, pt-{size}, pr-{size}, pb-{size}, pl-{size}, px-{size}, py-{size}
- Margin: m-{size}, mt-{size}, mr-{size}, mb-{size}, ml-{size}, mx-{size}, my-{size}
Common Pitfalls & Anti-Patterns
❌ Inconsistent Spacing
<!-- Bad: Inconsistent, arbitrary spacing -->
<div class="p-3 mb-7 mt-5">
<h2 class="mb-3.5">Heading</h2>
<p class="mb-6">Content</p>
</div>
✅ Consistent Spacing System
<!-- Good: Consistent spacing using the scale -->
<div class="p-4 mb-8 mt-6">
<h2 class="mb-4">Heading</h2>
<p class="mb-6">Content</p>
</div>
❌ Overriding with Custom CSS
<!-- Bad: Mixing Tailwind with custom CSS -->
<div class="p-4" style="margin-bottom: 22px;">Content</div>
✅ Extending the Config
// Good: Extend the config if you need custom values
module.exports = {
theme: {
extend: {
spacing: {
22: "5.5rem", // 88px
},
},
},
};
<div class="p-4 mb-22">Content</div>
When and Why to Use Typography and Spacing
Typography
- When to use: For all text content on your site
- Why: Consistent typography creates visual hierarchy, improves readability, and establishes brand identity
- Best practices:
- Use a limited set of font sizes and weights
- Maintain consistent line heights
- Use appropriate letter spacing for different font sizes
- Consider responsive typography (smaller on mobile, larger on desktop)
Spacing
- When to use: For layout, component design, and content separation
- Why: Consistent spacing creates rhythm, improves readability, and creates visual relationships
- Best practices:
- Use the spacing scale consistently
- Use larger spacing between major sections, smaller within components
- Consider responsive spacing (tighter on mobile, more generous on desktop)
- Use auto margins for centering when appropriate
Mini Challenges
Challenge 1: Typography Hierarchy
Create a typographic hierarchy for a blog post with:
- Main heading
- Subheading
- Body text
- Caption text
- Links
Challenge 2: Card Component
Create a card component with proper spacing for:
- Card container
- Card image
- Card title
- Card description
- Card actions
Challenge 3: Responsive Typography
Create a heading that:
- Is smaller on mobile devices
- Increases in size at medium and large breakpoints
- Has appropriate line height at each size
Interview Tips
Q: "How do you ensure consistent typography across a large project?"
A: "I establish a typography system in Tailwind by:
- Defining a limited set of font families in the config
- Creating a clear type scale with appropriate line heights
- Using component abstractions for common text elements
- Creating documentation with examples
- Using the @tailwindcss/typography plugin for long-form content
- Setting up responsive typography rules for different screen sizes"
Q: "How do you approach spacing in a design system?"
A: "I approach spacing systematically by:
- Using Tailwind's spacing scale consistently
- Following an 8px grid system (or 4px for finer control)
- Using larger spacing between sections, smaller within components
- Creating spacing patterns for common components
- Adjusting spacing responsively for different screen sizes
- Extending the spacing scale when needed, but maintaining the rhythm"
Challenge Answers
Answer 1: Typography Hierarchy
<article class="max-w-2xl mx-auto p-6">
<h1 class="font-bold text-3xl md:text-4xl text-gray-900 mb-4 leading-tight">
Main Heading for Blog Post
</h1>
<h2 class="font-semibold text-xl md:text-2xl text-gray-800 mb-6 leading-snug">
Subheading that provides additional context
</h2>
<p class="text-base text-gray-700 mb-6 leading-relaxed">
Body text that forms the main content of the article. This should be highly
readable with good line height and comfortable font size. Links within the
text should be
<a href="#" class="text-blue-600 hover:text-blue-800 underline"
>clearly distinguishable</a
>
from regular text.
</p>
<figure class="mb-6">
<img
src="https://via.placeholder.com/800x400"
alt="Example image"
class="rounded-lg"
/>
<figcaption class="text-sm text-gray-500 mt-2 italic">
Caption text for images or additional information that's secondary to the
main content.
</figcaption>
</figure>
<p class="text-base text-gray-700 mb-6 leading-relaxed">
More body text to demonstrate the consistency of the typography system
across multiple paragraphs and sections.
</p>
</article>
Answer 2: Card Component
<div class="max-w-sm rounded-lg overflow-hidden shadow-lg bg-white">
<!-- Card image -->
<img
class="w-full h-48 object-cover"
src="https://via.placeholder.com/400x200"
alt="Card image"
/>
<!-- Card content -->
<div class="px-6 py-4">
<h3 class="font-bold text-xl mb-2 text-gray-800">Card Title</h3>
<p class="text-gray-600 text-base mb-4">
This is the card description with properly spaced content. The spacing
between elements creates a comfortable reading experience.
</p>
</div>
<!-- Card tags -->
<div class="px-6 pt-2 pb-4">
<span
class="inline-block bg-gray-100 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2"
>#photography</span
>
<span
class="inline-block bg-gray-100 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2"
>#travel</span
>
</div>
<!-- Card actions -->
<div
class="px-6 py-4 bg-gray-50 border-t border-gray-100 flex justify-end space-x-2"
>
<button
class="px-4 py-2 bg-gray-200 text-gray-800 text-sm font-medium rounded-md hover:bg-gray-300"
>
Cancel
</button>
<button
class="px-4 py-2 bg-blue-500 text-white text-sm font-medium rounded-md hover:bg-blue-600"
>
View
</button>
</div>
</div>
Answer 3: Responsive Typography
<h1
class="
font-bold
text-2xl leading-tight tracking-tight
md:text-3xl md:leading-tight
lg:text-4xl lg:leading-tight
xl:text-5xl xl:leading-tight
text-gray-900
"
>
This heading adjusts its size, line height, and tracking responsively
</h1>
<p
class="
mt-4
text-base leading-relaxed
md:text-lg md:leading-relaxed
lg:text-xl lg:leading-relaxed
text-gray-700
"
>
This paragraph also adjusts its typography based on screen size, ensuring
optimal readability across devices from mobile to desktop.
</p>