Layout Fundamentals

Loading concept...

🏗️ Tailwind CSS: Layout Fundamentals

The Building Blocks Story

Imagine you’re building a house with LEGO blocks. Before you can make a beautiful castle or a cozy home, you need to understand how to size your blocks, leave space between them, and decide if they’re visible or hidden.

That’s exactly what Tailwind CSS Layout Fundamentals is about! These are your building superpowers for creating any web page layout.


📏 Sizing Utilities: How Big Should Things Be?

Think of sizing utilities like choosing the right size box for your toys.

Width & Height

Tailwind gives you easy classes to set how wide and how tall something should be.

<!-- Fixed sizes -->
<div class="w-32">I'm 128px wide</div>
<div class="h-16">I'm 64px tall</div>

<!-- Percentage sizes -->
<div class="w-1/2">I'm half as wide as my parent</div>
<div class="w-full">I take all the space!</div>

<!-- Screen-based -->
<div class="w-screen">I'm as wide as the screen</div>
<div class="h-screen">I'm as tall as the screen</div>

🎯 Quick Pattern:

  • w- = width
  • h- = height
  • Numbers = fixed sizes (4 = 16px, 8 = 32px, etc.)
  • Fractions = percentages (1/2, 1/3, 1/4)
  • full = 100%
  • screen = viewport size

Min & Max Sizes

Sometimes you want to say “be at least this big” or “never get bigger than this.”

<!-- Minimum -->
<div class="min-w-0">At least 0 wide</div>
<div class="min-h-screen">At least screen height</div>

<!-- Maximum -->
<div class="max-w-md">Never wider than 448px</div>
<div class="max-h-96">Never taller than 384px</div>

📐 Size Utility: One Class for Both!

Here’s a magic trick: What if you want something to be the same width AND height? Use size-!

<!-- Square boxes! -->
<div class="size-8">32px × 32px square</div>
<div class="size-16">64px × 64px square</div>
<div class="size-full">100% × 100%</div>

🧙‍♂️ Pro Tip: size-8 is the same as writing w-8 h-8. Less typing, same result!


🎁 Spacing Utilities: Give Things Room to Breathe

Imagine sitting on a crowded bus vs. having your own comfy seat. Spacing is about comfort!

Padding (Inside Space)

Padding is the space inside a box, between the edge and the content.

<!-- All sides -->
<div class="p-4">16px padding everywhere</div>

<!-- Specific sides -->
<div class="pt-2">8px padding top</div>
<div class="pr-4">16px padding right</div>
<div class="pb-6">24px padding bottom</div>
<div class="pl-8">32px padding left</div>

<!-- Horizontal & Vertical -->
<div class="px-4">16px left AND right</div>
<div class="py-2">8px top AND bottom</div>

Margin (Outside Space)

Margin is the space outside a box, pushing other things away.

<!-- All sides -->
<div class="m-4">16px margin everywhere</div>

<!-- Specific sides -->
<div class="mt-2">8px margin top</div>
<div class="mr-4">16px margin right</div>
<div class="mb-6">24px margin bottom</div>
<div class="ml-8">32px margin left</div>

<!-- Horizontal & Vertical -->
<div class="mx-auto">Center me horizontally!</div>
<div class="my-4">16px top AND bottom</div>

The Spacing Scale

0 = 0px
1 = 4px
2 = 8px
3 = 12px
4 = 16px
5 = 20px
6 = 24px
8 = 32px
10 = 40px
12 = 48px
16 = 64px

🎯 Memory Trick: Multiply the number by 4 to get pixels!


📦 Box Sizing: What’s Really Inside?

Here’s a puzzle: If you have a box that’s 100px wide, and you add 10px of padding, how wide is it now?

The Two Answers

Content-box (old way): 100px + 10px + 10px = 120px 😰

Border-box (Tailwind way): Still 100px! Padding fits inside. 😊

<!-- Border-box (default in Tailwind) -->
<div class="box-border w-32 p-4">
  I'm exactly 128px wide!
</div>

<!-- Content-box (if you ever need it) -->
<div class="box-content w-32 p-4">
  I'm 128px + 32px padding = 160px wide!
</div>

🎉 Good News: Tailwind uses box-border by default, so math stays simple!


🎭 Display Utilities: How Things Show Up

Display controls how an element appears in your layout. It’s like asking: “Should this act like a word, a box, or something fancy?”

Block vs Inline

<!-- Block: Takes full width, starts new line -->
<div class="block">I'm a block</div>
<div class="block">I start on a new line</div>

<!-- Inline: Flows with text, no line break -->
<span class="inline">I flow</span>
<span class="inline">with the text</span>

<!-- Inline-block: Inline but respects width/height -->
<div class="inline-block w-20 h-10">Box in line</div>

Flex & Grid (Layout Superpowers!)

<!-- Flexbox: Line things up easily -->
<div class="flex">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

<!-- Grid: Create rows and columns -->
<div class="grid grid-cols-3">
  <div>Cell 1</div>
  <div>Cell 2</div>
  <div>Cell 3</div>
</div>

Hidden

<!-- Make it disappear completely -->
<div class="hidden">You can't see me!</div>

Quick Reference

Class Behavior
block Full width, new line
inline Flows with text
inline-block Inline + dimensions
flex Flexible box layout
grid Grid-based layout
hidden Completely hidden

👀 Visibility Utilities: Hide but Keep Space

Here’s a fun difference:

  • hidden = Element is gone (no space)
  • invisible = Element is hidden but space is kept
<!-- Invisible: Space is reserved -->
<div class="invisible">
  I'm invisible but I still take up space!
</div>

<!-- Visible: Show it again -->
<div class="visible">
  I'm visible!
</div>

🎬 Real Example:

<div class="flex gap-2">
  <button class="p-2 bg-blue-500">Button 1</button>
  <button class="p-2 bg-blue-500 invisible">
    Hidden Button
  </button>
  <button class="p-2 bg-blue-500">Button 3</button>
</div>
<!-- Button 3 stays in place because middle
     button's space is preserved! -->

🌫️ Opacity Utilities: See-Through Magic

Opacity controls how transparent something is. Think of it like tinting a car window!

The Scale

<!-- Fully transparent (invisible) -->
<div class="opacity-0">Ghost mode</div>

<!-- Mostly transparent -->
<div class="opacity-25">25% visible</div>

<!-- Half transparent -->
<div class="opacity-50">50% visible</div>

<!-- Slightly transparent -->
<div class="opacity-75">75% visible</div>

<!-- Fully visible (default) -->
<div class="opacity-100">100% solid</div>

Cool Uses

<!-- Hover effect -->
<button class="opacity-75 hover:opacity-100">
  Hover me to see full color!
</button>

<!-- Disabled look -->
<button class="opacity-50 cursor-not-allowed">
  I'm disabled
</button>

<!-- Overlay effect -->
<div class="bg-black opacity-50">
  Semi-transparent overlay
</div>

📊 Opacity Values:

  • 0 = 0% (invisible)
  • 5 = 5%
  • 10 = 10%
  • 20 = 20%
  • 25 = 25%
  • 30 = 30%
  • 40 = 40%
  • 50 = 50%
  • 60 = 60%
  • 70 = 70%
  • 75 = 75%
  • 80 = 80%
  • 90 = 90%
  • 95 = 95%
  • 100 = 100% (solid)

🎯 Putting It All Together

Let’s build a simple card using everything we learned:

<div class="w-full max-w-sm mx-auto">
  <div class="p-4 bg-white box-border">
    <!-- Visible content -->
    <div class="block mb-4">
      <h2 class="text-xl">Card Title</h2>
    </div>

    <!-- Image with size -->
    <div class="size-24 bg-gray-200 mb-4"></div>

    <!-- Text with opacity -->
    <p class="opacity-75">
      Some description text here...
    </p>

    <!-- Invisible spacer -->
    <div class="invisible h-4"></div>

    <!-- Flex buttons -->
    <div class="flex gap-2 mt-4">
      <button class="px-4 py-2">Cancel</button>
      <button class="px-4 py-2">Save</button>
    </div>
  </div>
</div>

🚀 Quick Cheat Sheet

graph TD A[Layout Utilities] --> B[Sizing] A --> C[Spacing] A --> D[Display] A --> E[Visibility] B --> B1["w-* h-*<br>width & height"] B --> B2["size-*<br>both at once"] B --> B3["min-* max-*<br>limits"] C --> C1["p-*<br>padding inside"] C --> C2["m-*<br>margin outside"] D --> D1["block inline flex grid"] D --> D2["hidden"] E --> E1["visible invisible"] E --> E2["opacity-*"]

🎉 You Did It!

You now know the foundation of Tailwind CSS layouts:

  1. Sizing - Control width and height with w-, h-, size-
  2. Spacing - Add breathing room with p- and m-
  3. Box Sizing - Keep math simple with box-border
  4. Display - Choose how things flow with block, flex, grid
  5. Visibility - Hide things with hidden, invisible
  6. Opacity - Make things see-through with opacity-*

These are your LEGO bricks for building any layout. Master these, and you can build anything! 🏰


Next up: Learn how to position elements exactly where you want them!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.