음수 마진과 요소 사이 간격
-mt-4와 같은 음수 마진으로 요소를 서로 가깝게 배치하고, space-x-*와 space-y-*로 자식 요소 사이에 간격을 추가합니다.
음수 마진과 요소 사이 간격은(는) CoddyKit의 무료 Tailwind CSS Academy 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Tailwind CSS Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Tailwind CSS Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What Are Negative Margins?
Negative margins pull an element toward or beyond the boundaries of its container or sibling. In CSS, a negative margin-top pulls an element upward; a negative margin-left pulls it leftward. This creates overlapping effects and allows elements to break out of their natural document flow. Tailwind supports negative margins with a - prefix on any margin utility.
<!-- Pull an element upward over the previous element -->
<div class="bg-indigo-600 h-32 rounded-t-xl"></div>
<div class="bg-white rounded-xl shadow p-6 -mt-8 mx-4">
This card overlaps the colored header above
</div>Negative Margin Syntax in Tailwind
Prefix any margin utility with a hyphen to make it negative: -mt-4 is margin-top: -16px, -mx-4 is margin-left: -16px; margin-right: -16px. The same spacing scale applies — -mt-1 is -4px, -mt-8 is -32px. You cannot make padding negative in CSS (padding is always non-negative), only margins.
<div class="bg-blue-600 h-24 rounded-xl"></div>
<div class="-mt-6 ml-6 inline-block bg-white shadow-lg rounded-lg px-6 py-4">
<span class="text-blue-700 font-semibold">Overlapping Badge</span>
</div>Breaking Out of Container Padding
A common use of negative margins is making a full-bleed image break out of a padded container. If a content container has px-6 (24px horizontal padding), a negative -mx-6 on an image inside it removes that padding, making the image stretch edge-to-edge while the surrounding text remains padded. This technique is common in blog article layouts.
<article class="max-w-2xl mx-auto px-6">
<h1 class="text-3xl font-bold mb-4">Article Title</h1>
<p class="mb-6">Intro paragraph within the padded container.</p>
<!-- Full-bleed image that breaks out of the padding -->
<img src="wide-photo.jpg" class="-mx-6 w-[calc(100%+3rem)] mb-6" alt="" />
<p>Content continues with normal padding.</p>
</article>Overlapping Card Elements
Profile cards, pricing cards, and feature cards often have an avatar or icon that overlaps the card's top edge. Apply a negative top margin to the card body, or a negative bottom margin to the header element, to create this overlap. Pair with a defined z-index using Tailwind's z-* utilities to control which element appears on top.
<div class="text-center">
<img src="avatar.jpg" class="w-20 h-20 rounded-full border-4 border-white shadow mx-auto" alt="" />
<div class="-mt-10 pt-12 bg-white rounded-xl shadow p-6">
<h3 class="font-bold text-gray-900">Jane Doe</h3>
<p class="text-gray-500 text-sm">Product Designer</p>
</div>
</div>Negative Margin for Grid Bleed
When a grid needs to bleed to the edges of a screen while maintaining inner gutters, use negative horizontal margins on the grid container combined with positive padding on grid items. This technique, sometimes called the negative margin grid, ensures items align to the container edges while maintaining consistent gutters between them.
<!-- Grid that bleeds to container edges -->
<div class="-mx-4">
<div class="grid grid-cols-2 md:grid-cols-3 gap-4 px-4">
<div class="bg-gray-100 p-4 rounded">Item 1</div>
<div class="bg-gray-100 p-4 rounded">Item 2</div>
<div class="bg-gray-100 p-4 rounded">Item 3</div>
</div>
</div>Introducing space-x-* and space-y-*
The space-x-* and space-y-* utilities add margin between sibling elements without adding margin to the first child. Apply them to a parent container, and Tailwind automatically adds margin-left to every direct child except the first. This is a convenient alternative to gap-* when flex or grid is not in use, or for adding space between items in a list.
<!-- space-x-4 adds left margin to every child except the first -->
<div class="flex space-x-4">
<button class="px-4 py-2 bg-gray-100 rounded">Option A</button>
<button class="px-4 py-2 bg-gray-100 rounded">Option B</button>
<button class="px-4 py-2 bg-gray-100 rounded">Option C</button>
</div>space-y-* for Vertical Stacking
space-y-* adds top margin to every direct child except the first, creating uniform vertical spacing in a stacked list. This is particularly useful for navigation menus, card stacks, and form field groups where you want consistent spacing without adding padding to each item individually.
<nav class="space-y-1">
<a href="#" class="block px-4 py-2 text-sm font-medium text-indigo-600 bg-indigo-50 rounded-lg">
Dashboard
</a>
<a href="#" class="block px-4 py-2 text-sm font-medium text-gray-600 hover:bg-gray-50 rounded-lg">
Profile
</a>
<a href="#" class="block px-4 py-2 text-sm font-medium text-gray-600 hover:bg-gray-50 rounded-lg">
Settings
</a>
</nav>space-* vs gap-*
Both space-x-* and gap-x-* add horizontal space between children, but they work differently. space-x-* uses margin-left on children (works with any display type), while gap-* uses CSS gap property (only works in flex or grid contexts). Use gap-* for flex and grid layouts as it also handles wrapping correctly. Use space-x-* when elements are not in a flex/grid context.
<!-- In flex: prefer gap -->
<div class="flex gap-4">
<div>A</div><div>B</div><div>C</div>
</div>
<!-- Not in flex/grid: use space-x -->
<div class="space-x-4">
<span class="inline-block">A</span>
<span class="inline-block">B</span>
<span class="inline-block">C</span>
</div>Reversed Space: space-x-reverse
When using flex-row-reverse to flip the order of flex children, space-x-* adds margin to the wrong side because children are now visually ordered right-to-left. Fix this by adding space-x-reverse alongside space-x-* — it switches the margin from margin-left to margin-right to match the reversed direction.
<!-- Reversed flex row with correct spacing -->
<div class="flex flex-row-reverse space-x-4 space-x-reverse">
<button class="px-4 py-2 bg-gray-100 rounded">Cancel</button>
<button class="px-4 py-2 bg-indigo-600 text-white rounded">Confirm</button>
</div>Practical: Badge Stack Overlap
User avatar stacks (common in collaboration features) overlap avatars using negative margins. Each subsequent avatar has a negative left margin that partially overlaps the previous one. Apply a border to each avatar and use hover:z-10 to bring a hovered avatar to the top of the stack. This creates the visual impression of a pile of overlapping avatars.
<div class="flex">
<img src="user1.jpg" class="w-8 h-8 rounded-full border-2 border-white" alt="" />
<img src="user2.jpg" class="w-8 h-8 rounded-full border-2 border-white -ml-2 hover:z-10" alt="" />
<img src="user3.jpg" class="w-8 h-8 rounded-full border-2 border-white -ml-2 hover:z-10" alt="" />
<div class="w-8 h-8 rounded-full border-2 border-white -ml-2 bg-gray-200 flex items-center justify-center text-xs font-medium text-gray-600">
+5
</div>
</div>When to Avoid Negative Margins
Negative margins are powerful but should be used sparingly. They can cause layout bugs when elements collapse or overflow unexpectedly, are confusing in code reviews, and may conflict with parent overflow-hidden. Prefer gap-* for flex/grid spacing, space-* for sibling spacing, and transform -translate-y-* for visual-only overlaps that should not affect document flow.
<!-- Use transform for visual overlap without flow impact -->
<div class="relative">
<div class="bg-indigo-600 h-24 rounded-t-xl"></div>
<div class="bg-white rounded-b-xl shadow p-6 transform -translate-y-4">
Visually shifted up without changing layout flow
</div>
</div>Quick Check
Test your understanding of Tailwind CSS Mastery concepts from this lesson.
Lesson Recap
In this lesson you learned: negative margins like -mt-4 pull elements upward and can create overlap effects, space-x-* and space-y-* add margin between siblings without affecting the first child, and space-* works for any display type while gap-* is preferred inside flex and grid containers. Next up we explore Tailwind's flexbox utilities for building powerful horizontal and vertical layouts.
AI 튜터와 함께 HTML을(를) 배우세요 — 무료
브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.
- 코스
- 30
- 레슨
- 120
자주 묻는 질문
“음수 마진과 요소 사이 간격” 강의는 무료인가요?
네 — “음수 마진과 요소 사이 간격” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Tailwind CSS Academy 강의 전체를 잠금 해제할 수 있습니다. Tailwind CSS Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“음수 마진과 요소 사이 간격”에서 뭘 배우나요?
-mt-4와 같은 음수 마진으로 요소를 서로 가깝게 배치하고, space-x-*와 space-y-*로 자식 요소 사이에 간격을 추가합니다. 브라우저에서 직접 실행하는 실습 코드로 Tailwind CSS Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Tailwind CSS Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Tailwind CSS Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“음수 마진과 요소 사이 간격” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Tailwind CSS Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Tailwind CSS Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Tailwind 간격 척도
- 패딩 유틸리티
- 마진 유틸리티와 자동 마진
- 음수 마진과 요소 사이 간격