Week 5: CSS Layout (Flexbox & Grid)
CSS | Flexbox, Grid, Positioning, Display
- التخطيط بـ Flexbox و Grid — شرح عملي — الترويسة بـ Flexbox و sticky، الرئيسية بـ grid-template-areas، القسم الترحيبي بعمودين، بطاقات الورشات بـ Grid تلقائي وشارة position: absolute · 8 دقائق
Al-Aqsa University
CSS Layout
Flexbox, Grid, Position & Display
Week 05
1. The Display Property Element Behavior
Display Values
| Value | Behavior | Examples |
block | Takes full width, starts on new line | div, p, h1-h6, section, header |
inline | Only takes needed width, no line break, ignores top/bottom margin/padding | span, a, strong, em, img |
inline-block | Inline flow but respects width/height/margin/padding | Custom buttons, tags |
none | Removed from layout completely | Hidden elements |
flex | Flex container (children become flex items) | Navbars, card layouts |
grid | Grid container (children placed in grid cells) | Page layouts, galleries |
CSS - Display Property
/* Block: takes full width */
.block-element {
display: block;
width: 100%; /* default for block */
}
/* Inline: flows with text */
.inline-element {
display: inline;
/* width and height are IGNORED */
}
/* Inline-block: best of both worlds */
.tag {
display: inline-block;
padding: 5px 15px; /* respects padding */
width: 100px; /* respects width */
margin: 5px; /* respects margin */
}
/* None: completely hidden */
.hidden {
display: none; /* takes no space */
}
Block vs Inline vs Inline-Block
Block - takes full width
Block - takes full width
Inline
Inline
Inline
Inline-Block
Inline-Block
2. Position Controlling Placement
Position Values
| Value | Behavior | Offset Properties |
static | Default. Normal document flow | top/left/right/bottom ignored |
relative | Offset from its normal position; space is preserved | top, left, right, bottom |
absolute | Removed from flow; positioned relative to nearest positioned ancestor | top, left, right, bottom |
fixed | Removed from flow; positioned relative to viewport (stays on scroll) | top, left, right, bottom |
sticky | Normal flow until scroll threshold, then becomes fixed | top (most common) |
CSS - Position
/* Relative: nudge from normal position */
.nudge-down {
position: relative;
top: 20px; /* moves 20px down from where it would be */
left: 10px; /* moves 10px right */
}
/* Absolute: position inside a relative parent */
.parent {
position: relative; /* creates positioning context */
}
.badge {
position: absolute;
top: 10px;
right: 10px; /* top-right corner of parent */
}
/* Fixed: stays in viewport on scroll */
.navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 1000;
}
/* Sticky: scrolls normally, then sticks */
.sticky-header {
position: sticky;
top: 0; /* sticks when reaching top of viewport */
}
Position: Absolute inside Relative
top:10px left:10px
top:10px right:10px
bottom:10px left:50%
Z-index & Stacking Context
CSS - Z-index
/* z-index controls stacking order (higher = on top) */
/* Only works on positioned elements (not static) */
.background { z-index: 1; }
.content { z-index: 10; }
.modal { z-index: 100; }
.navbar { z-index: 1000; }
.tooltip { z-index: 9999; }
3. Float & Clear Legacy Layout (Brief)
Float (Legacy Approach)
Floats were the primary layout method before Flexbox and Grid. They are still useful for wrapping text around images but should not be used for page layouts.
CSS - Float
/* Float an image to the left, text wraps around it */
img.float-left {
float: left;
margin-right: 15px;
margin-bottom: 10px;
}
/* Clear floats (stop wrapping) */
.clear {
clear: both; /* left | right | both */
}
/* Clearfix hack (apply to parent of floated elements) */
.clearfix::after {
content: "";
display: table;
clear: both;
}
Use Flexbox or Grid Instead
Floats for layout are considered legacy. Use display: flex or display: grid for all layout needs. Floats are still fine for text wrapping around images.
4. Flexbox One-Dimensional Layout
Flex Container Properties
Apply display: flex to a parent element to make it a flex container. Its direct children become flex items.
CSS - Flex Container
.container {
display: flex;
/* Direction: row (default) | row-reverse | column | column-reverse */
flex-direction: row;
/* Main axis alignment */
justify-content: flex-start; /* start (default) */
justify-content: flex-end; /* end */
justify-content: center; /* center */
justify-content: space-between; /* even space between items */
justify-content: space-around; /* even space around items */
justify-content: space-evenly; /* equal gaps */
/* Cross axis alignment */
align-items: stretch; /* fill height (default) */
align-items: flex-start; /* align to top */
align-items: flex-end; /* align to bottom */
align-items: center; /* vertical center */
align-items: baseline; /* text baseline */
/* Wrapping */
flex-wrap: nowrap; /* default: single line */
flex-wrap: wrap; /* allow items to wrap to next line */
/* Gap between items */
gap: 20px; /* row and column gap */
row-gap: 15px;
column-gap: 20px;
/* Align wrapped lines (only with flex-wrap: wrap) */
align-content: center;
}
justify-content Examples
flex-start:
123
center:
123
space-between:
123
space-around:
123
space-evenly:
123
align-items Examples
flex-start:
ABC
center:
ABC
stretch (default):
ABC
flex-direction Examples
row (default):
123
column:
123
Flex Item Properties
CSS - Flex Items
.item {
/* flex-grow: how much extra space this item gets (0 = don't grow) */
flex-grow: 1; /* takes equal share of remaining space */
/* flex-shrink: how much item shrinks (1 = default) */
flex-shrink: 0; /* don't shrink */
/* flex-basis: initial size before grow/shrink */
flex-basis: 200px; /* start at 200px */
flex-basis: auto; /* use content size (default) */
/* Shorthand: flex: grow shrink basis */
flex: 1; /* flex: 1 1 0% - grow equally */
flex: 0 0 200px; /* fixed 200px, no grow, no shrink */
flex: 2; /* takes twice the space as flex:1 */
/* align-self: override align-items for this item */
align-self: flex-end; /* push this item to bottom */
/* order: change visual order (default: 0) */
order: -1; /* move to beginning */
order: 99; /* move to end */
}
flex-grow Example
Item 2 has flex-grow: 2 (gets double space):
flex:1
flex:2
flex:1
Practical Flexbox Examples
CSS - Navbar with Flexbox
/* Classic navbar: logo left, links right */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 30px;
background: #1a1a2e;
}
.nav-links {
display: flex;
gap: 20px;
list-style: none;
}
/* Center anything perfectly */
.center-everything {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Card row that wraps */
.card-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: center;
}
.card {
flex: 0 0 300px; /* fixed width, no grow/shrink */
}
5. CSS Grid Two-Dimensional Layout
Grid Container Properties
CSS - Grid Container
.grid-container {
display: grid;
/* Define columns */
grid-template-columns: 200px 200px 200px; /* 3 fixed columns */
grid-template-columns: 1fr 1fr 1fr; /* 3 equal columns */
grid-template-columns: repeat(3, 1fr); /* same as above */
grid-template-columns: 1fr 2fr 1fr; /* middle is double */
grid-template-columns: 200px 1fr; /* sidebar + flexible */
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); /* responsive! */
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); /* responsive + stretch */
/* Define rows */
grid-template-rows: 100px auto 60px; /* header, content, footer */
grid-auto-rows: minmax(100px, auto); /* auto rows min 100px */
/* Gap */
gap: 20px; /* row and column gap */
row-gap: 15px;
column-gap: 20px;
/* Named areas */
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
}
grid-template-columns: repeat(3, 1fr)
123
456
grid-template-columns: 200px 1fr 1fr
Sidebar
Main
Side
Grid Item Properties
CSS - Grid Items
/* Spanning columns and rows */
.wide-item {
grid-column: span 2; /* spans 2 columns */
grid-column: 1 / 3; /* from line 1 to line 3 */
grid-column: 1 / -1; /* full width */
}
.tall-item {
grid-row: span 2; /* spans 2 rows */
grid-row: 1 / 3; /* from row line 1 to 3 */
}
/* Using named areas */
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
/* Alignment within grid cell */
.item {
justify-self: center; /* horizontal in cell */
align-self: center; /* vertical in cell */
}
Grid with Spanning Items
span 2 cols
span 2 rows
A
B
span 3 (full width)
Grid Template Areas - Page Layout
header
sidebar
main
footer
The fr Unit & Responsive Functions
CSS - fr, repeat, minmax
/* fr = fraction of available space */
.grid {
grid-template-columns: 1fr 2fr 1fr;
/* 1/4, 2/4, 1/4 of total width */
}
/* repeat() - shorthand for repetition */
.grid {
grid-template-columns: repeat(4, 1fr); /* = 1fr 1fr 1fr 1fr */
}
/* minmax() - set min and max size */
.grid {
grid-template-columns: repeat(3, minmax(200px, 1fr));
/* each column: at least 200px, at most equal share */
}
/* auto-fill: create as many columns as fit */
.grid {
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
/* responsive grid without media queries! */
}
/* auto-fit: same as auto-fill but stretches to fill space */
.grid {
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}
auto-fill vs auto-fit
auto-fill creates empty columns if there is extra space. auto-fit collapses empty columns and stretches the existing items to fill the row. For most cases, auto-fit is what you want.
6. CSS Units Sizing & Spacing
Absolute vs Relative Units
| Unit | Type | Relative To | Use Case |
px | Absolute | Screen pixel | Borders, shadows, small fixed values |
% | Relative | Parent element | Widths, responsive sizing |
em | Relative | Parent font-size | Padding, margins relative to text |
rem | Relative | Root (<html>) font-size | Font sizes, consistent spacing |
vw | Relative | 1% of viewport width | Full-width sections, responsive text |
vh | Relative | 1% of viewport height | Full-screen sections (100vh) |
vmin | Relative | 1% of smaller viewport dimension | Responsive sizing |
vmax | Relative | 1% of larger viewport dimension | Responsive sizing |
CSS - Units Examples
/* rem is most recommended for font sizes */
html { font-size: 16px; } /* 1rem = 16px */
h1 { font-size: 2rem; } /* = 32px */
h2 { font-size: 1.5rem; } /* = 24px */
p { font-size: 1rem; } /* = 16px */
/* vh for full-screen sections */
.hero {
min-height: 100vh; /* full viewport height */
}
/* % for responsive widths */
.container {
width: 90%;
max-width: 1200px;
margin: 0 auto;
}
7. Flexbox vs Grid When to Use What
Decision Guide
| Scenario | Use | Why |
| Navbar (logo + links) | Flexbox | One-dimensional row |
| Center something | Flexbox | Simple justify + align |
| Card row | Flexbox | Wrapping items in a row |
| Page layout (header, sidebar, main, footer) | Grid | Two-dimensional control |
| Image gallery | Grid | Rows AND columns matter |
| Dashboard | Grid | Complex 2D placement |
| Equal-height columns | Both work | Flexbox stretch or Grid rows |
Rule of Thumb
Flexbox = one direction at a time (row OR column). Content-first - items determine the layout.
Grid = two directions simultaneously (rows AND columns). Layout-first - you define the grid, items fill it.