Narzędzia dopełnienia
Stosować jednolite dopełnienie za pomocą p-* oraz kontrolować poszczególne strony przy użyciu pt-*, pr-*, pb-*, pl-*, px-* i py-*.
Narzędzia dopełnienia to bezpłatna lekcja Tailwind CSS Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Tailwind CSS Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Tailwind CSS Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
What Is Padding?
Padding is the space between an element's content and its border. It is part of the CSS box model: every element has content, padding, border, and margin layers. Padding adds breathing room inside an element — a button with no padding has its text touching the edges, while py-2 px-4 creates comfortable vertical and horizontal cushioning around the label.
<!-- No padding: cramped -->
<button class="bg-indigo-600 text-white rounded">Submit</button>
<!-- With padding: comfortable -->
<button class="bg-indigo-600 text-white rounded py-2 px-6">Submit</button>Uniform Padding: p-*
The p-* utility applies equal padding to all four sides of an element at once. p-4 sets 16px on top, right, bottom, and left. This is the most common starting point for cards, modals, and section containers. Use p-0 to explicitly remove all padding when an inherited or default style adds unwanted spacing.
<div class="p-0 bg-gray-100">No padding</div>
<div class="p-2 bg-gray-200 mt-2">Compact (8px all sides)</div>
<div class="p-4 bg-gray-300 mt-2">Standard (16px all sides)</div>
<div class="p-8 bg-gray-400 mt-2">Spacious (32px all sides)</div>Axis Padding: px-* and py-*
Control padding on horizontal and vertical axes independently. px-* adds padding to left and right (padding-left and padding-right in CSS). py-* adds padding to top and bottom. This is essential for buttons, navigation items, and pill badges where you want more horizontal padding than vertical padding, or for sections where top/bottom padding differs from left/right padding.
<!-- Button: more horizontal than vertical padding -->
<button class="bg-indigo-600 text-white rounded-lg py-2 px-6 font-medium">
Get Started
</button>
<!-- Section: larger vertical than horizontal padding -->
<section class="bg-gray-50 py-16 px-6">
Section content
</section>Side-Specific Padding: pt, pr, pb, pl
For precise control, apply padding to individual sides. pt-* is padding-top, pr-* is padding-right, pb-* is padding-bottom, and pl-* is padding-left. Mix and match to create asymmetric padding. A common case is adding extra top padding to a section that needs clearance below a sticky navbar: pt-20 (80px) accounts for a 64px navbar plus extra breathing room.
<!-- Asymmetric padding for a callout box -->
<div class="bg-yellow-50 border-l-4 border-yellow-400 pt-3 pr-4 pb-3 pl-5">
<p class="text-yellow-800">Note: extra left padding for visual offset from the border.</p>
</div>Combining Axis and Side Padding
You can combine axis and side utilities. Start with px-6 py-4 for general padding, then override a specific side. For example, a list item with a left icon might use px-4 py-3 pl-12 — the pl-12 overrides the left padding from px-4, creating extra room for the icon without affecting the right padding.
<!-- List item with left icon space -->
<div class="relative flex items-start px-4 py-3 pl-12 hover:bg-gray-50">
<!-- Icon positioned absolutely in the left padding zone -->
<span class="absolute left-4 top-3 text-indigo-500">•</span>
<p class="text-gray-700">List item content with room for the icon</p>
</div>Padding vs Margin
A key distinction: padding is inside the element (affects the clickable/tapable area and background color extent), while margin is outside (creates space between elements). For clickable elements like buttons and links, always use padding — it expands the hit area and the colored background region. Use margin to separate one component from another.
<!-- Padding expands the clickable area -->
<a href="#" class="bg-indigo-100 text-indigo-700 p-3 rounded">
Larger click target
</a>
<!-- Margin separates this from the next element -->
<div class="bg-white p-4 mb-4 rounded shadow">
Card 1
</div>
<div class="bg-white p-4 rounded shadow">
Card 2
</div>Padding on Form Elements
Form inputs require careful padding to be both readable and visually balanced. Text inputs typically use px-3 py-2 or px-4 py-2.5 for a comfortable text field. Larger inputs for rich text or search bars might use px-4 py-3. Always pair input padding with a rounded utility and a border to visually delineate the input field.
<input
type="text"
placeholder="Enter email"
class="w-full px-4 py-2.5 border border-gray-300 rounded-lg text-gray-900
focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500"
/>Responsive Padding
Section padding commonly increases at larger viewports. Use responsive prefixes to create this scaling effect. A container might have px-4 py-8 on mobile, md:px-8 md:py-16 on tablets, and lg:px-16 lg:py-24 on desktop. This progressive increase in whitespace is a key characteristic of professional, spacious desktop layouts.
<section class="px-4 py-8 md:px-8 md:py-16 lg:px-16 lg:py-24 bg-white">
<h2 class="text-2xl md:text-3xl lg:text-4xl font-bold text-gray-900">
Scales with the viewport
</h2>
</section>Padding in Grid and Flex Containers
When using flex or grid layouts, you generally do not pad the container but instead use gap-* between children. However, padding the container itself creates consistent inner margins from the container edges. The combination of p-6 on a grid container with gap-4 between items gives a visually balanced layout with equal-feeling spacing throughout.
<div class="grid grid-cols-3 gap-4 p-6 bg-gray-100 rounded-xl">
<div class="bg-white p-4 rounded-lg shadow-sm">Item 1</div>
<div class="bg-white p-4 rounded-lg shadow-sm">Item 2</div>
<div class="bg-white p-4 rounded-lg shadow-sm">Item 3</div>
</div>Practical: Navigation Item Padding
Navigation items need padding that creates a comfortable tap target (minimum 44px height per Apple's guidelines) while keeping the nav visually compact. A nav link with py-2 px-4 gives 8px vertical and 16px horizontal padding. The link's visible area with text plus padding typically meets the tap target requirement. Add hover:bg-gray-100 rounded-lg for visual feedback.
<nav class="flex items-center gap-1 px-4 py-3 bg-white border-b">
<a href="#" class="py-2 px-4 text-sm font-medium text-indigo-600 bg-indigo-50 rounded-lg">
Dashboard
</a>
<a href="#" class="py-2 px-4 text-sm font-medium text-gray-600 hover:bg-gray-100 rounded-lg">
Settings
</a>
</nav>Logical Padding in RTL Layouts
For right-to-left (RTL) language support, Tailwind v3.3 added logical property utilities: ps-* (padding-inline-start) and pe-* (padding-inline-end). In LTR, ps-4 equals pl-4. In RTL, ps-4 automatically becomes pr-4. Use ps-* and pe-* instead of pl-* and pr-* when building globally accessible applications.
<!-- RTL-aware padding -->
<div class="ps-4 pe-4 py-3">
Works correctly in both LTR and RTL layouts
</div>Quick Check
Test your understanding of Tailwind CSS Mastery concepts from this lesson.
Lesson Recap
In this lesson you learned: p-* applies uniform padding on all four sides, px-* and py-* control horizontal and vertical axes independently, and pt-*, pr-*, pb-*, pl-* target individual sides for asymmetric padding. Next up we apply margin utilities and learn how auto margins enable horizontal centering.
Często zadawane pytania
Czy lekcja „Narzędzia dopełnienia” jest bezpłatna?
Tak — pełny tekst „Narzędzia dopełnienia” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Tailwind CSS Academy, przejdź na CoddyKit PRO. Kurs Tailwind CSS Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Narzędzia dopełnienia”?
Stosować jednolite dopełnienie za pomocą p-* oraz kontrolować poszczególne strony przy użyciu pt-*, pr-*, pb-*, pl-*, px-* i py-*. Ćwiczysz Tailwind CSS Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Tailwind CSS Academy?
Nie wymagamy żadnego doświadczenia. Tailwind CSS Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Narzędzia dopełnienia”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Tailwind CSS Academy?
Tak. Każda lekcja Tailwind CSS Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Skala odstępów Tailwind
- Narzędzia dopełnienia
- Narzędzia marginesów i marginesy automatyczne
- Ujemne marginesy i odstępy między elementami