--- name: tailwindcss-advanced-layouts description: Tailwind CSS advanced layout techniques including CSS Grid and Flexbox patterns --- # Tailwind CSS Advanced Layout Techniques ## CSS Grid Mastery ### Complex Grid Layouts ```html
Header
Main Content
Header
Main
``` ### Grid Template Areas ```css @utility grid-areas-dashboard { grid-template-areas: "header header header" "nav main aside" "nav footer footer"; } @utility area-header { grid-area: header; } @utility area-nav { grid-area: nav; } @utility area-main { grid-area: main; } @utility area-aside { grid-area: aside; } @utility area-footer { grid-area: footer; } ``` ```html
Header
Main Content
``` ### Auto-Fill and Auto-Fit Grids ```html
Card 1
Card 2
Card 3
``` ### Subgrid ```css /* Enable subgrid in v4 */ @utility subgrid-cols { grid-template-columns: subgrid; } @utility subgrid-rows { grid-template-rows: subgrid; } ``` ```html
Aligned to parent column 1
Aligned to parent column 2
``` ## Advanced Flexbox Patterns ### Space Distribution ```html
First
Second
Third
Item
Item
Item
Item
Item
Item
``` ### Flexible Item Sizing ```html
1/3
1/3
1/3
2/4
1/4
1/4
Fixed 256px
Flexible (can shrink)
Icon
Very long text that should truncate
``` ### Masonry-Like with Flexbox ```html
Item 1
Item 2
Item 3
``` ## Container Queries ### Basic Container Queries ```css @plugin "@tailwindcss/container-queries"; ``` ```html
Item 1
Item 2
Item 3
``` ### Named Containers ```html
Content
``` ### Container Query Units ```html

Scales with container width

Scales with container inline size

``` ## Position and Layering ### Sticky Positioning ```html
Navigation
...
Corner cell Column 2
``` ### Fixed Elements ```html ``` ### Z-Index Management ```css @theme { --z-dropdown: 100; --z-sticky: 200; --z-fixed: 300; --z-modal-backdrop: 400; --z-modal: 500; --z-popover: 600; --z-tooltip: 700; --z-toast: 800; } @utility z-dropdown { z-index: var(--z-dropdown); } @utility z-sticky { z-index: var(--z-sticky); } @utility z-fixed { z-index: var(--z-fixed); } @utility z-modal-backdrop { z-index: var(--z-modal-backdrop); } @utility z-modal { z-index: var(--z-modal); } @utility z-popover { z-index: var(--z-popover); } @utility z-tooltip { z-index: var(--z-tooltip); } @utility z-toast { z-index: var(--z-toast); } ``` ## Overflow and Scrolling ### Custom Scrollbars ```css @utility scrollbar-thin { scrollbar-width: thin; } @utility scrollbar-none { scrollbar-width: none; -ms-overflow-style: none; } @utility scrollbar-none::-webkit-scrollbar { display: none; } /* Custom scrollbar styling */ @utility scrollbar-custom { scrollbar-color: oklch(0.7 0 0) oklch(0.95 0 0); } @utility scrollbar-custom::-webkit-scrollbar { width: 8px; height: 8px; } @utility scrollbar-custom::-webkit-scrollbar-track { background: oklch(0.95 0 0); border-radius: 4px; } @utility scrollbar-custom::-webkit-scrollbar-thumb { background: oklch(0.7 0 0); border-radius: 4px; } @utility scrollbar-custom::-webkit-scrollbar-thumb:hover { background: oklch(0.5 0 0); } ``` ### Scroll Snap ```html
Card 1
Card 2
Card 3
Section 1
Section 2
Section 3
...
``` ### Scroll Margin for Anchors ```html
``` ## Aspect Ratio and Object Fit ### Responsive Aspect Ratios ```html
4:3 content
Ultra-wide content
``` ### Object Positioning ```html
``` ## Advanced Spacing ### Logical Properties ```html
Padding and margin that respect text direction
Block-direction spacing
``` ### Space Between with Dividers ```html
Section 1
Section 2
Section 3
``` ### Negative Margins for Bleeds ```html

Padded content

More padded content

Normal content...

Featured quote that extends beyond content width
``` ## Multi-Column Layout ### Text Columns ```html

Content flows across columns...

Creates as many 300px columns as fit

Card that stays together
``` ## Responsive Patterns ### Container Queries + Media Queries ```html
Content
``` ### Breakpoint-Based Visibility ```html ``` ### Fluid Sizing with Clamp ```html
Content with responsive padding
Responsive container
``` ## Print Styles ```html

Heading

Link (shows as text when printed)
Keep this content together on one page
Start on new page
``` ## Best Practices ### 1. Use Modern Layout Methods ```html
``` ### 2. Handle Edge Cases ```html
Long text
Content that might overflow
``` ### 3. Use Semantic Sizing ```html
``` ### 4. Test All Breakpoints Create systematic tests for all responsive layouts to ensure they work at every breakpoint.