Tailwind CSS Academy · 강의

마진 유틸리티와 자동 마진

m-* 유틸리티로 요소 바깥쪽에 간격을 추가하고, mx-auto로 가로 중앙 정렬을 적용하며, 방향별 마진을 조합합니다.

레슨 3/413개 단계

마진 유틸리티와 자동 마진은(는) CoddyKit의 무료 Tailwind CSS Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Tailwind CSS Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Tailwind CSS Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Margin vs Padding Revisited

Margin creates space outside an element, between it and adjacent elements. Unlike padding, margin does not add to the element's clickable area or background extent. Margins are transparent — they show whatever is behind them. Use margin to separate components (cards, sections, paragraphs) from each other, and padding to create space within a component.

<div class="bg-blue-100 p-4 mb-4 rounded">
  Card 1 — mb-4 creates 16px below this card
</div>
<div class="bg-blue-100 p-4 rounded">
  Card 2 — pushed down by Card 1's margin
</div>

Uniform Margin: m-*

m-* applies equal margin on all four sides. m-0 is commonly used to override default browser margins — browsers add default margin to elements like <body>, <h1>, and <p>. Tailwind's Preflight CSS reset removes these, but m-0 remains useful when third-party components introduce unexpected margins.

<!-- Third-party component with default margin -->
<ThirdPartyWidget class="m-0" />

<!-- Adding space around all sides -->
<div class="m-4 bg-white rounded shadow p-4">
  Floated card with all-side margin
</div>

Directional Margin Utilities

Like padding, margin has directional variants: mt-* (margin-top), mr-* (margin-right), mb-* (margin-bottom), ml-* (margin-left), mx-* (horizontal), my-* (vertical). The most commonly used is mb-* — adding bottom margin below headings and paragraphs to create vertical rhythm in text layouts.

<article>
  <h1 class="text-3xl font-bold mb-4">Article Title</h1>
  <p class="text-gray-600 mb-6 text-lg">Lead paragraph with extra bottom space.</p>
  <p class="text-gray-700 mb-4">Regular paragraph.</p>
  <p class="text-gray-700 mb-4">Another paragraph.</p>
</article>

mx-auto: The Centering Trick

mx-auto is one of the most used Tailwind utilities. It sets margin-left: auto and margin-right: auto, which horizontally centers a block element within its parent. For this to work, the element must have a defined width (either fixed or max-width). It does not work on flex or grid children — those use justify-center or justify-self-center.

<!-- Center a container: the most common pattern -->
<div class="max-w-2xl mx-auto px-4">
  Centered content with maximum width
</div>

<!-- Center a specific element -->
<img src="logo.svg" class="w-32 mx-auto block" alt="Logo" />

mt-auto: Push to Bottom

In a flex column layout, mt-auto pushes an element to the bottom of the container by consuming all available space above it. This is the standard technique for creating card footers that always sit at the bottom, regardless of how much content is in the card body. It works in any flex or grid container.

<!-- Card with action button always at the bottom -->
<div class="flex flex-col h-64 bg-white rounded-xl shadow p-6">
  <h3 class="font-bold text-gray-900 mb-2">Card Title</h3>
  <p class="text-gray-500 text-sm">Variable amount of card content here.</p>
  <button class="mt-auto bg-indigo-600 text-white py-2 rounded-lg font-medium">
    Action
  </button>
</div>

ml-auto: Push to Right

In a flex row layout, ml-auto pushes an element to the right edge of its flex container by consuming all available horizontal space to its left. This is the standard navbar pattern: logo on the left, navigation links in the middle, and a CTA button pushed to the right with ml-auto.

<nav class="flex items-center px-6 py-4 bg-white border-b">
  <span class="font-bold text-indigo-600">Brand</span>
  <div class="flex gap-6 mx-8">
    <a href="#" class="text-gray-600 text-sm">Home</a>
    <a href="#" class="text-gray-600 text-sm">About</a>
  </div>
  <button class="ml-auto bg-indigo-600 text-white text-sm px-4 py-2 rounded">
    Sign Up
  </button>
</nav>

Margin Collapse Awareness

CSS has a concept called margin collapse: when two block-level elements' vertical margins meet, the larger margin wins and the margins merge rather than add. For example, a paragraph with mb-4 followed by another with mt-4 creates a total gap of 16px, not 32px. Margin collapse does not occur in flex or grid containers — only between block-level siblings and parent-child blocks.

<!-- Margin collapse in block context -->
<p class="mb-4">Paragraph 1 (margin-bottom: 16px)</p>
<p class="mt-4">Paragraph 2 (margin-top: 16px)</p>
<!-- Gap between them = 16px, NOT 32px -->

<!-- No margin collapse in flex context -->
<div class="flex flex-col">
  <p class="mb-4">Paragraph 1</p>
  <p class="mt-4">Paragraph 2</p>
</div>
<!-- Gap between them = 32px -->

Margin in Vertical Rhythm

Vertical rhythm is the consistent spacing between typographic elements that makes content feel organized. Establish it with consistent bottom margins: headings get mb-4, paragraphs get mb-4, code blocks and blockquotes get mb-6, and section headings get mb-8. The consistent multiples create a visual beat that guides the reader's eye downward.

<article class="prose">
  <h2 class="text-2xl font-bold mb-4">Section Title</h2>
  <p class="mb-4">First paragraph.</p>
  <p class="mb-4">Second paragraph.</p>
  <pre class="mb-6 p-4 bg-gray-100 rounded">Code block</pre>
  <p class="mb-4">Paragraph after code.</p>
</article>

Responsive Margins

Margins often need to scale with the viewport. Section bottom margins may be mb-8 on mobile and mb-16 on desktop. Feature card groups may use mt-8 md:mt-16 to create more generous spacing above them on large screens where there is room. Responsive margin adjustments are a quick win for making designs feel native at each breakpoint.

<section class="mt-8 md:mt-16 lg:mt-24">
  Feature section with responsive top margin
</section>

Using my-auto for Vertical Centering

In a flex column or grid with a defined height, my-auto vertically centers an element by consuming available space equally above and below it. This is an alternative to flex items-center for centering a single child element. It is particularly useful when you need to center one specific element in a mixed-content container where not all elements should be centered.

<div class="h-48 flex flex-col bg-gray-100 rounded-xl p-4">
  <p class="text-sm text-gray-500">Top label</p>
  <span class="my-auto text-2xl font-bold text-gray-900">
    Centered number
  </span>
  <p class="text-xs text-gray-400">Bottom label</p>
</div>

Practical: Article Section Layout

A well-structured article page uses margin heavily for flow: the main content container uses mx-auto max-w-3xl to center it, sections within get mb-12 between them, the hero has mb-8 before the content, and the bottom of the page has a generous mb-24 to prevent the last paragraph from sitting too close to the footer.

<main class="max-w-3xl mx-auto px-4 py-8">
  <header class="mb-8">
    <h1 class="text-4xl font-bold">Article Title</h1>
  </header>
  <section class="mb-12">
    <h2 class="text-2xl font-semibold mb-4">Section 1</h2>
    <p class="text-gray-700 mb-4">Content...</p>
  </section>
  <section class="mb-24">
    <h2 class="text-2xl font-semibold mb-4">Section 2</h2>
    <p class="text-gray-700">Final section.</p>
  </section>
</main>

Quick Check

Test your understanding of Tailwind CSS Mastery concepts from this lesson.

Lesson Recap

In this lesson you learned: mx-auto horizontally centers a block element with a defined width, mt-auto in a flex column pushes an element to the bottom of its container, and ml-auto in a flex row pushes an element to the right edge. Next up we explore negative margins and the space-between utilities for controlling gaps between sibling elements.

무료로 시작

AI 튜터와 함께 HTML을(를) 배우세요 — 무료

브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.

코스
30
레슨
120

자주 묻는 질문

“마진 유틸리티와 자동 마진” 강의는 무료인가요?

네 — “마진 유틸리티와 자동 마진” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Tailwind CSS Academy 강의 전체를 잠금 해제할 수 있습니다. Tailwind CSS Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“마진 유틸리티와 자동 마진”에서 뭘 배우나요?

m-* 유틸리티로 요소 바깥쪽에 간격을 추가하고, mx-auto로 가로 중앙 정렬을 적용하며, 방향별 마진을 조합합니다. 브라우저에서 직접 실행하는 실습 코드로 Tailwind CSS Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Tailwind CSS Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Tailwind CSS Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“마진 유틸리티와 자동 마진” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Tailwind CSS Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Tailwind CSS Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Tailwind 간격 척도
  2. 패딩 유틸리티
  3. 마진 유틸리티와 자동 마진
  4. 음수 마진과 요소 사이 간격
← Tailwind CSS Academy(으)로 돌아가기