Contents
Tailwind CSS Basics
Tailwind CSS provides a comprehensive set of utilities that you can use to build custom designs directly in your HTML. In this guide, we’ll explore some of the core utilities in Tailwind, including spacing, typography, color, layout, and responsive design. Understanding these basics will help you create clean, responsive, and consistent designs.
Working with Spacing Utilities (Padding, Margin)
Tailwind CSS offers a range of utilities to control spacing, including padding (p-*
) and margin (m-*
). These utilities allow you to add space around and inside elements.
Padding Utilities
Padding utilities add space inside an element.
Example: Adding Padding
This div has 1rem (16px) padding on all sides.
p-4
: Adds padding of1rem
(16px) on all sides.px-4
: Adds horizontal padding (left and right) of1rem
(16px).py-2
: Adds vertical padding (top and bottom) of0.5rem
(8px).pl-6
: Adds left padding of1.5rem
(24px).
Margin Utilities
Margin utilities add space outside an element.
Example: Adding Margin
This div has 1rem (16px) margin on all sides.
m-4
: Adds margin of1rem
(16px) on all sides.mt-2
: Adds top margin of0.5rem
(8px).mb-2
: Adds bottom margin of0.5rem
(8px).ml-6
: Adds left margin of1.5rem
(24px).
Auto Margin
Tailwind also provides utilities for auto margins, which are useful for centering elements.
Example: Centering a Block Element Horizontally
This div is centered horizontally.
mx-auto
: Automatically adjusts the left and right margins to center the element.w-64
: Sets the width of the element to16rem
(256px).
Applying Typography Utilities (Font Size, Weight, Line Height)
Tailwind’s typography utilities allow you to control the size, weight, and spacing of text.
Font Size
Font size utilities set the size of the text.
Example: Setting Font Size
This text is large.
text-xs
: Extra small text size (0.75rem
/ 12px).text-base
: Base text size (1rem
/ 16px).text-2xl
: Large text size (1.5rem
/ 24px).text-4xl
: Extra-large text size (2.25rem
/ 36px).
Font Weight
Font weight utilities control the thickness of the text.
Example: Setting Font Weight
This text is bold.
font-thin
: Thin font weight (100
).font-normal
: Normal font weight (400
).font-semibold
: Semi-bold font weight (600
).font-bold
: Bold font weight (700
).
Line Height
Line height utilities control the spacing between lines of text.
Example: Setting Line Height
This text has loose line spacing.
leading-none
: No extra space between lines.leading-tight
: Tight line spacing.leading-normal
: Normal line spacing.leading-loose
: Loose line spacing.
Using Color Utilities for Text, Background, Borders
Tailwind provides extensive color utilities for applying colors to text, backgrounds, and borders.
Text Color
Text color utilities apply color to the text.
Example: Setting Text Color
This text is blue.
text-red-500
: Medium red text color.text-green-700
: Dark green text color.text-gray-900
: Very dark gray text color.
Background Color
Background color utilities set the background color of an element.
Example: Setting Background Color
This div has a green background.
bg-blue-200
: Light blue background.bg-yellow-400
: Bright yellow background.bg-gray-100
: Very light gray background.
Border Color
Border color utilities set the color of an element’s borders.
Example: Setting Border Color
This div has a red border.
border-blue-500
: Medium blue border.border-gray-300
: Light gray border.border-black
: Black border.
Working with Layout Utilities (Flexbox, Grid, Positioning)
Tailwind CSS offers powerful layout utilities, including Flexbox, Grid, and positioning utilities, to create complex layouts easily.
Flexbox
Flexbox utilities allow you to create flexible and responsive layouts.
Example: Using Flexbox for Layout
Flex item 1
Flex item 2
Flex item 3
flex
: Makes the container a flexbox.flex-1
: Each child takes up an equal portion of the available space.justify-center
: Centers items horizontally.items-center
: Centers items vertically.
Grid
Grid utilities enable you to create grid-based layouts.
Example: Using Grid for Layout
Grid item 1
Grid item 2
Grid item 3
grid
: Makes the container a grid.grid-cols-3
: Creates a grid with three columns.gap-4
: Adds a gap between grid items.
Positioning
Positioning utilities control the positioning of elements.
Example: Absolute Positioning
Positioned box
relative
: Sets the container as a reference for absolute positioning.absolute
: Absolutely positions the element relative to the nearest positioned ancestor.top-0
,bottom-0
,left-0
,right-0
: Position the element on the respective side.
Responsive Design with Tailwind’s Responsive Utilities (sm:, md:, lg:, xl:)
Tailwind makes it easy to create responsive designs by using responsive utility prefixes like sm:
, md:
, lg:
, and xl:
.
Responsive Utilities
Responsive utilities apply styles at specific breakpoints.
Example: Responsive Padding
This div has different padding on various screen sizes.
sm:
Applies at screen widths of640px
and up.md:
Applies at screen widths of768px
and up.lg:
Applies at screen widths of1024px
and up.xl:
Applies at screen widths of1280px
and up.
Example: Responsive Flexbox Layout
Item 1
Item 2
flex-col
: Stacks items vertically (column) by default.sm:flex-row
: Stacks items horizontally (row) on small screens and larger.
Summary
Tailwind CSS provides a powerful set of utility classes that allow you to style your HTML directly with consistent, maintainable, and responsive designs. Understanding the basics—such as working with spacing, typography, color, layout, and responsive utilities—will enable you to build complex and visually appealing UIs quickly and efficiently. Tailwind’s utility-first approach ensures that you have the flexibility to design custom interfaces while maintaining a clean and concise codebase.
Related Chapters
- What is Tailwind CSS?
- Setting Up Tailwind CSS
- Understanding Utility-First CSS
- Tailwind CSS Basics
- Customizing Tailwind CSS
- Handling State Variants
- Pseudo-Classes and Conditional Styling
- Working with Flexbox and Grid
- Component Styling with Tailwind CSS
- Typography and Prose
- Optimizing for Production
- Dark Mode and Theming
- Animations and Transitions
- Using Tailwind with JavaScript Frameworks
- Planning the Project
- Building the UI Components
- Finalizing and Deploying