Part 2: Display, Positioning, Flexbox, Grid, Float, Overflow & More
display: block; /* Full width, new line (div, p, h1-h6, section) */
display: inline; /* Flows with text, no width/height (span, a, em) */
display: inline-block; /* Inline flow + width/height control */
display: none; /* Completely removed from layout */
display: flex; /* Flex container */
display: inline-flex; /* Inline flex container */
display: grid; /* Grid container */
display: inline-grid; /* Inline grid container */
display: contents; /* Element disappears, children promoted */
display: table; /* Behaves like <table> */
display: list-item; /* Shows with list marker */
display: flow-root; /* Creates new BFC (clears floats) */
/* visibility vs display:none */
visibility: hidden; /* Hidden but KEEPS space */
display: none; /* Hidden and REMOVES from layout */
/* STATIC (default) - normal document flow */
position: static;
/* RELATIVE - offset from normal position
Element still takes up original space */
position: relative;
top: 10px; /* Moves down 10px */
left: 20px; /* Moves right 20px */
/* ABSOLUTE - removed from flow
Positioned relative to nearest positioned ancestor */
position: absolute;
top: 0;
right: 0;
/* FIXED - removed from flow
Positioned relative to viewport (stays on scroll) */
position: fixed;
bottom: 20px;
right: 20px;
/* STICKY - hybrid of relative & fixed
Sticks when scrolled past threshold */
position: sticky;
top: 0; /* Sticks at top when scrolled */
/* z-index only works on positioned elements
(relative, absolute, fixed, sticky) */
.behind { z-index: 1; }
.middle { z-index: 10; }
.front { z-index: 100; }
.overlay { z-index: 999; }
/* Negative z-index: behind parent */
.bg { z-index: -1; }
/* inset shorthand (top right bottom left) */
inset: 0; /* = top:0;right:0;bottom:0;left:0 */
inset: 10px 20px; /* top/bottom:10px left/right:20px */
inset: 10px 20px 30px 40px;
/* Common pattern: full-cover overlay */
.overlay {
position: absolute;
inset: 0;
background: rgba(0,0,0,0.5);
}
/* Method 1: Transform (most reliable) */
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Method 2: Inset + margin auto (needs width/height) */
.center {
position: absolute;
inset: 0;
margin: auto;
width: 200px;
height: 200px;
}
.flex-container {
display: flex; /* or inline-flex */
/* DIRECTION: which way items flow */
flex-direction: row; /* Left to right (default) */
flex-direction: row-reverse; /* Right to left */
flex-direction: column; /* Top to bottom */
flex-direction: column-reverse; /* Bottom to top */
/* WRAP: do items wrap to new lines? */
flex-wrap: nowrap; /* Single line (default) */
flex-wrap: wrap; /* Multi-line */
flex-wrap: wrap-reverse;
/* SHORTHAND */
flex-flow: row wrap; /* direction + wrap */
/* GAP between items */
gap: 16px; /* Both row and column gap */
row-gap: 20px;
column-gap: 10px;
gap: 20px 10px; /* row-gap column-gap */
}
/*
MAIN AXIS alignment (horizontal in row, vertical in column)
+----------------------------------------------+
| [A] [B] [C] justify-content |
+----------------------------------------------+
*/
justify-content: flex-start; /* |[A][B][C] | (default) */
justify-content: flex-end; /* | [A][B][C]| */
justify-content: center; /* | [A][B][C] | */
justify-content: space-between; /* |[A] [B] [C]| */
justify-content: space-around; /* | [A] [B] [C] | */
justify-content: space-evenly; /* | [A] [B] [C] | */
/*
CROSS AXIS alignment (vertical in row, horizontal in column)
*/
align-items: stretch; /* Fill container height (default) */
align-items: flex-start; /* Align to top */
align-items: flex-end; /* Align to bottom */
align-items: center; /* Align to middle */
align-items: baseline; /* Align text baselines */
/* MULTI-LINE cross axis (only with flex-wrap: wrap) */
align-content: flex-start | flex-end | center |
space-between | space-around | stretch;
/* SHORTHAND for align-items + justify-content */
place-items: center; /* center center */
place-content: center; /* center center */
.flex-item {
/* ORDER: change visual order (default: 0) */
order: 1; /* Higher = later in visual order */
order: -1; /* Negative = before default items */
/* FLEX-GROW: how much item grows to fill space (default: 0) */
flex-grow: 0; /* Don't grow (default) */
flex-grow: 1; /* Take equal share of extra space */
flex-grow: 2; /* Take twice as much extra space */
/* FLEX-SHRINK: how much item shrinks (default: 1) */
flex-shrink: 1; /* Can shrink (default) */
flex-shrink: 0; /* Never shrink */
/* FLEX-BASIS: initial size before grow/shrink */
flex-basis: auto; /* Use width/height (default) */
flex-basis: 200px;
flex-basis: 30%;
flex-basis: 0; /* Ignore content size */
/* FLEX shorthand: grow shrink basis */
flex: 0 1 auto; /* Default */
flex: 1; /* = 1 1 0 (grow equally) */
flex: auto; /* = 1 1 auto */
flex: none; /* = 0 0 auto (rigid) */
flex: 2 1 200px; /* Custom */
/* ALIGN-SELF: override container's align-items */
align-self: auto | flex-start | flex-end | center | stretch | baseline;
}
/* Perfect centering */
.center {
display: flex;
justify-content: center;
align-items: center;
}
/* Navbar: logo left, links right */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
/* Equal-width columns */
.columns { display: flex; }
.columns > * { flex: 1; }
/* Sticky footer */
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main { flex: 1; }
/* Wrap with minimum item width */
.wrap-grid {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.wrap-grid > * {
flex: 1 1 250px; /* Min 250px, grows */
}
/* Media object (image + text) */
.media {
display: flex;
gap: 16px;
}
.media img { flex-shrink: 0; }
.media .body { flex: 1; }
/* Vertical stack with spacing */
.stack {
display: flex;
flex-direction: column;
gap: 12px;
}
/* Push last item to far right */
.item:last-child {
margin-left: auto;
}
.grid {
display: grid; /* or inline-grid */
/* DEFINE COLUMNS */
grid-template-columns: 200px 200px 200px; /* 3 fixed cols */
grid-template-columns: 1fr 2fr 1fr; /* Fractional */
grid-template-columns: repeat(3, 1fr); /* 3 equal cols */
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); /* Responsive! */
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); /* Stretch to fill */
grid-template-columns: 200px 1fr 200px; /* Sidebar layout */
grid-template-columns: fit-content(200px) 1fr; /* Content-based */
/* DEFINE ROWS */
grid-template-rows: 80px 1fr 60px; /* Header, main, footer */
grid-template-rows: repeat(3, 100px);
/* AUTO ROWS/COLS (implicit tracks) */
grid-auto-rows: minmax(100px, auto); /* Min 100px, grow */
grid-auto-columns: 200px;
grid-auto-flow: row; /* Fill by row (default) */
grid-auto-flow: column; /* Fill by column */
grid-auto-flow: dense; /* Fill gaps aggressively */
/* GAP */
gap: 20px; /* Row and column gap */
row-gap: 20px;
column-gap: 16px;
gap: 20px 16px; /* row-gap column-gap */
}
.layout {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: 80px 1fr 60px;
grid-template-areas:
"header header header"
"sidebar main aside"
"footer footer footer";
min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }
/* Use "." for empty cells */
grid-template-areas:
"header header"
"sidebar main"
". footer";
/* CONTAINER ALIGNMENT */
/* Align items within their grid cells */
justify-items: start | end | center | stretch; /* Row axis */
align-items: start | end | center | stretch; /* Column axis */
place-items: center center; /* align-items justify-items */
/* Align the grid itself within the container */
justify-content: start | end | center | stretch | space-between | space-around | space-evenly;
align-content: start | end | center | stretch | space-between | space-around | space-evenly;
place-content: center;
/* ITEM ALIGNMENT (override for individual items) */
.item {
justify-self: start | end | center | stretch;
align-self: start | end | center | stretch;
place-self: center;
}
.item {
/* Span specific columns */
grid-column-start: 1;
grid-column-end: 3;
/* Shorthand */
grid-column: 1 / 3; /* Start at 1, end at 3 */
grid-column: 1 / span 2; /* Start at 1, span 2 */
grid-column: 1 / -1; /* Full width! */
/* Span specific rows */
grid-row: 1 / 3;
grid-row: 2 / span 2;
/* grid-area shorthand */
/* row-start / col-start / row-end / col-end */
grid-area: 1 / 1 / 3 / 3;
}
/* repeat() */
grid-template-columns: repeat(4, 1fr);
grid-template-columns: repeat(3, 100px 50px);
/* minmax() - min and max size */
grid-template-columns: minmax(200px, 1fr) 2fr;
/* auto-fill vs auto-fit */
/* auto-fill: creates empty columns */
repeat(auto-fill, minmax(200px, 1fr))
/* auto-fit: stretches items to fill */
repeat(auto-fit, minmax(200px, 1fr))
/* fit-content() */
grid-template-columns: fit-content(300px) 1fr;
/* Named lines */
grid-template-columns: [start] 1fr [main] 2fr [end];
.item { grid-column: start / main; }
/* Auto-responsive card grid */
.auto-grid {
display: grid;
grid-template-columns:
repeat(auto-fit, minmax(280px, 1fr));
gap: 20px;
}
/* Dashboard layout */
.dashboard {
display: grid;
grid-template-columns:
repeat(auto-fit, minmax(300px, 1fr));
grid-auto-rows: minmax(200px, auto);
gap: 16px;
}
/* Full-bleed layout */
.full-bleed {
display: grid;
grid-template-columns:
1fr min(800px, 90%) 1fr;
}
.full-bleed > * {
grid-column: 2;
}
.full-bleed > .wide {
grid-column: 1 / -1;
}
/* Subgrid: children align to parent grid */
.parent {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.child {
grid-column: span 3;
display: grid;
/* Use parent's column tracks */
grid-template-columns: subgrid;
}
float: left; /* Float element to the left */
float: right; /* Float element to the right */
float: none; /* Default (don't float) */
clear: left; /* Clear left floats */
clear: right; /* Clear right floats */
clear: both; /* Clear all floats */
/* Clearfix hack (prevents parent from collapsing) */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* Modern alternative: flow-root */
.parent {
display: flow-root; /* Contains floats, no hack needed */
}
overflow: visible; /* Content overflows box (default) */
overflow: hidden; /* Content is clipped */
overflow: scroll; /* Always shows scrollbar */
overflow: auto; /* Scrollbar only when needed */
overflow: clip; /* Like hidden but no programmatic scroll */
/* Individual axes */
overflow-x: auto; /* Horizontal */
overflow-y: hidden; /* Vertical */
/* Custom scrollbar (WebKit) */
.box::-webkit-scrollbar { width: 8px; }
.box::-webkit-scrollbar-track { background: #111; }
.box::-webkit-scrollbar-thumb { background: #555; border-radius: 4px; }
/* Standard scrollbar styling */
.box { scrollbar-width: thin; scrollbar-color: #555 #111; }
.text-columns {
column-count: 3; /* Fixed number of columns */
column-width: 250px; /* Ideal column width */
columns: 3 250px; /* Shorthand */
column-gap: 40px;
column-rule: 1px solid #ccc; /* Line between */
}
/* Span across all columns */
h2 { column-span: all; }
/* Prevent breaks inside element */
.card { break-inside: avoid; }
/* Aspect Ratio */
.video { aspect-ratio: 16 / 9; }
.square { aspect-ratio: 1; }
.photo { aspect-ratio: 4 / 3; }
/* Object Fit (for img, video) */
img {
width: 100%;
height: 300px;
object-fit: fill; /* Stretch (default) */
object-fit: contain; /* Fit inside, keep ratio */
object-fit: cover; /* Fill, crop excess */
object-fit: none; /* Original size */
object-fit: scale-down; /* Smaller of none/contain */
object-position: center top; /* Align focal point */
object-position: 50% 50%; /* Center (default) */
}