CSS Display and Layout Modes: Complete Guide to Element Behavior
Professional Guide by Alaa Amer – Expert Web Developer & Designer
Understanding Display Properties is fundamental to mastering page layouts. Learn to control element behavior and create flexible, sophisticated designs.
1️⃣ Basic Display Concept
Main Display Values
Value | Behavior | Use Case |
---|---|---|
block | Takes full width | Headings, paragraphs |
inline | Flows with text | Links, emphasis |
inline-block | Hybrid of both | Buttons, small cards |
flex | Flexible container | Flexible layouts |
grid | Two-dimensional grid | Complex layouts |
none | Completely hidden | Hiding elements |
/* Basic display examples */
.display-examples {
/* Block element - takes full width */
display: block;
width: 100%;
height: 50px;
background: #007bff;
margin-bottom: 10px;
}
.display-inline {
/* Inline element - flows with text */
display: inline;
background: #28a745;
padding: 5px 10px;
/* Note: width and height don't work with inline */
}
.display-inline-block {
/* Hybrid - flows but accepts dimensions */
display: inline-block;
width: 150px;
height: 50px;
background: #dc3545;
margin: 5px;
text-align: center;
line-height: 50px;
}
.display-none {
/* Completely hidden - takes no space */
display: none;
}
.visibility-hidden {
/* Hidden but takes space */
visibility: hidden;
}
2️⃣ Block Elements
Block Element Properties
/* Default block element behavior */
.block-elements {
/* Default block elements */
/* div, p, h1-h6, section, article, header, footer, main, nav */
}
/* Converting element to block */
.make-block {
display: block;
/* Block element properties */
width: 100%; /* Takes full available width */
margin: 10px 0; /* Can apply all margin types */
padding: 20px; /* Can apply all padding types */
border: 2px solid #333; /* Can apply borders */
/* Starts on new line */
/* Next element starts on new line too */
}
/* Practical block element examples */
.card-block {
display: block;
width: 300px;
padding: 20px;
margin: 20px auto;
background: white;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.header-block {
display: block;
width: 100%;
padding: 15px 20px;
background: #007bff;
color: white;
text-align: center;
font-weight: bold;
}
.content-block {
display: block;
max-width: 800px;
margin: 0 auto;
padding: 40px 20px;
line-height: 1.6;
}
Customizing Block Elements
/* Customizing block element widths */
.custom-widths {
display: block;
/* Different widths */
width: 50%; /* half width */
width: 300px; /* fixed width */
width: auto; /* automatic width */
width: fit-content; /* content width */
width: min-content; /* minimum possible width */
width: max-content; /* maximum content width */
}
/* Centering block elements */
.centered-blocks {
display: block;
width: 60%;
margin: 0 auto; /* horizontal centering */
}
.center-container {
display: flex;
justify-content: center;
}
.center-container .centered-child {
display: block;
width: 300px;
}
3️⃣ Inline Elements
Inline Element Properties
/* Default inline elements */
/* span, a, strong, em, img, input, button */
.inline-elements {
display: inline;
/* Inline element properties */
/* width and height don't work */
/* width: 200px; ← won't work */
/* height: 50px; ← won't work */
/* Vertical margins don't work normally */
margin: 0 10px; /* horizontal works, vertical doesn't */
/* Padding works but may overlap */
padding: 5px 10px; /* works but with caution */
/* Borders work */
border: 1px solid #333;
background: #f8f9fa;
color: #333;
}
/* Practical inline element examples */
.inline-link {
display: inline;
color: #007bff;
text-decoration: none;
padding: 2px 4px;
border-radius: 3px;
transition: background-color 0.3s ease;
}
.inline-link:hover {
background-color: rgba(0, 123, 255, 0.1);
}
.inline-badge {
display: inline;
background: #28a745;
color: white;
padding: 3px 8px;
border-radius: 12px;
font-size: 0.75rem;
font-weight: 600;
}
.inline-code {
display: inline;
background: #f8f9fa;
color: #e83e8c;
padding: 2px 6px;
border-radius: 4px;
font-family: "Courier New", monospace;
font-size: 0.9em;
border: 1px solid #e9ecef;
}
Inline Text Formatting
/* Advanced inline element formatting */
.advanced-inline {
display: inline;
/* Letter and word spacing */
letter-spacing: 1px;
word-spacing: 2px;
/* Text transformation */
text-transform: uppercase;
/* Text shadow */
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
/* Background gradient */
background: linear-gradient(135deg, #ff6b6b, #4ecdc4);
color: white;
padding: 4px 8px;
border-radius: 4px;
}
/* Inline elements with icons */
.icon-text {
display: inline;
vertical-align: middle;
}
.icon-text::before {
content: "📧";
margin-right: 5px;
}
/* Interactive inline effects */
.interactive-inline {
display: inline;
position: relative;
padding: 2px 0;
color: #007bff;
text-decoration: none;
transition: color 0.3s ease;
}
.interactive-inline::after {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 0;
height: 2px;
background: #007bff;
transition: width 0.3s ease;
}
.interactive-inline:hover::after {
width: 100%;
}
4️⃣ Inline-Block: Best of Both Worlds
Understanding Inline-Block
/* Inline-block combines advantages of both types */
.inline-block-demo {
display: inline-block;
/* Accepts dimensions like block */
width: 200px;
height: 100px;
/* Flows horizontally like inline */
margin: 10px;
/* Accepts full styling */
padding: 20px;
border: 2px solid #007bff;
border-radius: 8px;
background: #f8f9fa;
text-align: center;
vertical-align: top; /* Control vertical alignment */
}
/* Practical inline-block uses */
/* Simple card grid */
.card-grid {
text-align: center; /* Center cards */
}
.card-item {
display: inline-block;
width: 250px;
margin: 15px;
padding: 20px;
background: white;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
text-align: left; /* Reset text alignment */
vertical-align: top; /* Align from top */
transition: transform 0.3s ease;
}
.card-item:hover {
transform: translateY(-5px);
}
/* Adjacent buttons */
.button-group {
text-align: center;
}
.btn-inline-block {
display: inline-block;
padding: 12px 24px;
margin: 0 5px;
background: #007bff;
color: white;
text-decoration: none;
border-radius: 6px;
border: none;
cursor: pointer;
transition: all 0.3s ease;
}
.btn-inline-block:hover {
background: #0056b3;
transform: translateY(-2px);
}
/* Horizontal navigation list */
.nav-horizontal {
background: #f8f9fa;
padding: 15px;
text-align: center;
border-radius: 8px;
}
.nav-item-inline {
display: inline-block;
margin: 0 10px;
}
.nav-link-inline {
display: inline-block;
padding: 10px 15px;
color: #333;
text-decoration: none;
border-radius: 6px;
transition: all 0.3s ease;
}
.nav-link-inline:hover {
background: #007bff;
color: white;
}
Solving White Space Issues
/* White space problem with inline-block */
.spacing-problem {
/* HTML puts spaces between inline-block elements */
}
/* Different solutions */
/* Solution 1: Remove spaces from HTML */
/* <div class="item">1</div><div class="item">2</div> */
/* Solution 2: font-size on container */
.container-fix {
font-size: 0; /* Remove spaces */
}
.container-fix .item {
display: inline-block;
font-size: 16px; /* Reset font size */
}
/* Solution 3: Use flexbox (best) */
.flex-solution {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.flex-solution .item {
/* No need for inline-block */
flex: 0 1 auto;
}
/* Solution 4: float (old) */
.float-solution .item {
float: left;
margin-right: 10px;
}
.float-solution::after {
content: "";
display: table;
clear: both;
}
5️⃣ Quick Introduction to Flex and Grid
Basic Flexbox
/* Flexbox for flexible layout */
.flex-container {
display: flex;
/* Element direction */
flex-direction: row; /* horizontal (default) */
flex-direction: column; /* vertical */
flex-direction: row-reverse; /* horizontal reversed */
flex-direction: column-reverse; /* vertical reversed */
/* Element wrapping */
flex-wrap: nowrap; /* no wrapping (default) */
flex-wrap: wrap; /* wrapping */
flex-wrap: wrap-reverse; /* reverse wrapping */
/* Horizontal alignment (main axis) */
justify-content: flex-start; /* start */
justify-content: flex-end; /* end */
justify-content: center; /* center */
justify-content: space-between; /* equal spaces */
justify-content: space-around; /* spaces around elements */
justify-content: space-evenly; /* perfectly equal spaces */
/* Vertical alignment (cross axis) */
align-items: stretch; /* stretch (default) */
align-items: flex-start; /* top */
align-items: flex-end; /* bottom */
align-items: center; /* center */
align-items: baseline; /* baseline */
}
/* Flexbox items */
.flex-item {
/* Element growth */
flex-grow: 1; /* grow to fill space */
flex-grow: 0; /* don't grow */
flex-grow: 2; /* grow twice as fast */
/* Element shrinking */
flex-shrink: 1; /* shrink when needed */
flex-shrink: 0; /* don't shrink */
/* Base size */
flex-basis: auto; /* automatic */
flex-basis: 200px; /* fixed size */
flex-basis: 30%; /* percentage */
/* Comprehensive shorthand */
flex: 1; /* flex: 1 1 0% */
flex: 0 1 auto; /* default values */
flex: none; /* flex: 0 0 auto */
}
/* Practical Flexbox layout example */
.layout-flex {
display: flex;
min-height: 100vh;
}
.sidebar {
flex: 0 0 250px; /* fixed width */
background: #f8f9fa;
padding: 20px;
}
.main-content {
flex: 1; /* take remaining space */
padding: 20px;
}
.footer-flex {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
background: #333;
color: white;
}
Basic CSS Grid
/* Grid for grid layout */
.grid-container {
display: grid;
/* Define columns */
grid-template-columns: 200px 1fr 100px; /* fixed, flexible, fixed */
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); /* responsive */
/* Define rows */
grid-template-rows: auto 1fr auto; /* header, content, footer */
grid-template-rows: repeat(4, 100px); /* 4 fixed rows */
/* Gaps between elements */
gap: 20px; /* comprehensive gap */
grid-gap: 10px 20px; /* rows, columns */
row-gap: 10px; /* row gap */
column-gap: 20px; /* column gap */
/* Grid alignment */
justify-content: center; /* horizontal alignment */
align-content: center; /* vertical alignment */
/* Element alignment inside cells */
justify-items: center; /* horizontal alignment of elements */
align-items: center; /* vertical alignment of elements */
}
/* Grid items */
.grid-item {
/* Specify element position */
grid-column: 1 / 3; /* from column 1 to 3 */
grid-row: 2 / 4; /* from row 2 to 4 */
/* Shortcuts */
grid-column: span 2; /* spans 2 columns */
grid-row: span 3; /* spans 3 rows */
/* Area specification */
grid-area: header; /* predefined area */
/* Individual element alignment */
justify-self: start; /* horizontal alignment */
align-self: end; /* vertical alignment */
}
/* Practical Grid layout example */
.layout-grid {
display: grid;
grid-template-areas:
"header header header"
"sidebar main aside"
"footer footer footer";
grid-template-rows: auto 1fr auto;
grid-template-columns: 200px 1fr 150px;
gap: 20px;
min-height: 100vh;
}
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.main {
grid-area: main;
}
.aside {
grid-area: aside;
}
.footer {
grid-area: footer;
}
6️⃣ Practical Project: Multi-Mode Layout System
The HTML, CSS, and JavaScript remain the same as in the Arabic version, with minor adjustments for text direction and language.
Summary
In this lesson we learned:
✅ Display concept and basic display modes
✅ Block elements and their properties
✅ Inline elements and their constraints
✅ Inline-Block as a flexible hybrid solution
✅ Quick introduction to Flexbox and Grid
✅ Interactive project demonstrating all modes
In the next lesson we'll study Flexbox in detail to master flexible layout and advanced alignment.