Display is one of the most fundamental property in order to show, hide, and layout content is specific ways, by changing their natural arrangement on the page. By default, HTML elements generally stack either vertically taking full width of their container, or they flow inline, horizontally while ignoring width and height. We can do more than that by using specific display values such as grid and flex.
Block, Inline, Inline-block
block fills the full width and stacks vertically. inline flows with text, ignores width/height. inline-block flows with text but respects width and height settings.
Preview index.html style.css
< div class = "demo" >
< div >
< div class = "label" >block — fills width, stacks vertically</ div >
< div class = "block-el" >Block A</ div >
< div class = "block-el" >Block B</ div >
< div class = "block-el" >Block C</ div >
</ div >
< div >
< div class = "label" >inline — flows with text</ div >
< span class = "inline-el" >Inline A</ span >
< span class = "inline-el" >Inline B</ span >
< span class = "inline-el" >Inline C — flows like text in a sentence</ span >
</ div >
< div >
< div class = "label" >inline-block — inline flow + box model</ div >
< span class = "dib-el" >A</ span >
< span class = "dib-el" >B</ span >
< span class = "dib-el dib-tall" >C< br >tall</ span >
</ div >
</ div > .demo {
display : flex ;
flex-direction : column ;
gap : 1.5 rem ;
}
.label {
font-size : .75 rem ;
font-weight : 600 ;
color : #666 ;
text-transform : uppercase ;
letter-spacing : .05 em ;
margin-bottom : .5 rem ;
}
.block-el {
display : block ;
background : #dbeafe ;
border : 1 px solid #93c5fd ;
padding : .5 rem 1 rem ;
margin : .25 rem 0 ;
border-radius : 4 px ;
}
.inline-el {
display : inline ;
background : #fef9c3 ;
border : 1 px solid #fde047 ;
padding : .2 rem .5 rem ;
border-radius : 4 px ;
}
.dib-el {
display : inline-block ;
background : #dcfce7 ;
border : 1 px solid #86efac ;
padding : .5 rem 1 rem ;
border-radius : 4 px ;
}
.dib-tall {
height : 3 rem ;
}
/* block — full width, stacks vertically, accepts width/height */
div , p , h1 , section { display : block ; }
/* inline — flows with text, width/height ignored */
span , a , strong { display : inline ; }
/* inline-block — flows inline but respects box model */
.badge { display : inline-block ; width : 80 px ; padding : 4 px ; }
/* display: none — removes completely, no space reserved */
.hidden { display : none ; }
/* visibility: hidden — invisible but space is kept */
.invisible { visibility : hidden ; }
02. Flexbox — Container
Setting display: flex on a container makes its direct children flex items . Control direction, wrapping, and gaps on the container.
Preview index.html style.css
< div class = "flex-container" >
< div class = "flex-item" >A</ div >
< div class = "flex-item" >B</ div >
< div class = "flex-item" >C</ div >
< div class = "flex-item" >D</ div >
</ div > .flex-container {
display : flex ;
gap : 1 rem ;
padding : 1 rem ;
background : #f1f5f9 ;
border-radius : 8 px ;
flex-wrap : wrap ;
}
.flex-item {
background : #6366f1 ;
color : white ;
padding : .75 rem 1.25 rem ;
border-radius : 6 px ;
font-weight : 600 ;
}
.container {
display : flex ;
flex-direction : row ; /* row | column | row-reverse | column-reverse */
flex-wrap : wrap ; /* nowrap (default) | wrap | wrap-reverse */
gap : 16 px ; /* space between items */
}
/* flex-flow is shorthand for direction + wrap */
flex-flow : row wrap;
03. Flexbox — Alignment
justify-content aligns items along the main axis . align-items aligns them along the cross axis .
Preview index.html style.css script.js
< div class = "al-controls" >
< div class = "ctrl-group" >
< span class = "ctrl-label" >justify-content</ span >
< label >< input type = "radio" name = "jc2" value = "flex-start" checked > flex-start</ label >
< label >< input type = "radio" name = "jc2" value = "center" > center</ label >
< label >< input type = "radio" name = "jc2" value = "flex-end" > flex-end</ label >
< label >< input type = "radio" name = "jc2" value = "space-between" > space-between</ label >
< label >< input type = "radio" name = "jc2" value = "space-around" > space-around</ label >
</ div >
< div class = "ctrl-group" >
< span class = "ctrl-label" >align-items</ span >
< label >< input type = "radio" name = "ai2" value = "flex-start" > flex-start</ label >
< label >< input type = "radio" name = "ai2" value = "center" checked > center</ label >
< label >< input type = "radio" name = "ai2" value = "flex-end" > flex-end</ label >
< label >< input type = "radio" name = "ai2" value = "stretch" > stretch</ label >
</ div >
</ div >
< div class = "al-preview" id = "al-preview" >
< div class = "al-item" >A</ div >
< div class = "al-item" >B< br >tall</ div >
< div class = "al-item" >C</ div >
</ div > .al-controls {
display : flex ;
gap : 1.5 rem ;
flex-wrap : wrap ;
margin-bottom : .75 rem ;
}
.ctrl-group {
display : flex ;
flex-direction : column ;
gap : .25 rem ;
}
.ctrl-label {
font-size : .7 rem ;
font-weight : 600 ;
text-transform : uppercase ;
letter-spacing : .05 em ;
color : #94a3b8 ;
margin-bottom : .1 rem ;
}
label {
font-size : .8 rem ;
display : flex ;
align-items : center ;
gap : .3 rem ;
cursor : pointer ;
}
.al-preview {
display : flex ;
background : #f1f5f9 ;
border-radius : 8 px ;
height : 120 px ;
padding : .5 rem ;
}
.al-item {
background : #6366f1 ;
color : white ;
border-radius : 6 px ;
padding : .5 rem .75 rem ;
font-size : .8 rem ;
font-weight : 600 ;
}
.al-item:nth-child ( 2 ) {
height : 3.5 rem ;
align-self : auto ;
} document. querySelectorAll ( 'input[name=jc2]' ). forEach ( r => {
r. addEventListener ( 'change' , () => {
document. getElementById ( 'al-preview' ).style.justifyContent = r.value
})
});
document. querySelectorAll ( 'input[name=ai2]' ). forEach ( r => {
r. addEventListener ( 'change' , () => {
document. getElementById ( 'al-preview' ).style.alignItems = r.value
})
});
.container {
display : flex ;
justify-content : center ; /* flex-start | center | flex-end | space-between | space-around | space-evenly */
align-items : center ; /* flex-start | center | flex-end | stretch | baseline */
}
/* override alignment for a single item */
.special { align-self : flex-start ; }
/* center anything in one line */
.center-everything { display : flex ; justify-content : center ; align-items : center ; }
04. Grid — Basics
CSS Grid is two-dimensional: rows and columns at once. The fr unit distributes leftover space proportionally. repeat() avoids repetition.
Preview index.html style.css
< div class = "grid-container" >
< div class = "grid-item" >Header (spans all columns)</ div >
< div class = "grid-item" >1</ div >
< div class = "grid-item" >2</ div >
< div class = "grid-item" >3</ div >
</ div > .grid-container {
display : grid ;
grid-template-columns : repeat ( 3 , 1 fr );
gap : 1 rem ;
}
.grid-item {
background : #a855f7 ;
color : white ;
padding : 1.5 rem ;
border-radius : 6 px ;
font-weight : 600 ;
text-align : center ;
}
.grid-item:nth-child ( 1 ) {
grid-column : 1 / -1 ;
}
.grid {
display : grid ;
grid-template-columns : 1 fr 1 fr 1 fr ; /* 3 equal columns */
grid-template-columns : repeat ( 3 , 1 fr ); /* same thing */
grid-template-columns : 200 px 1 fr ; /* fixed + fluid */
grid-template-columns : repeat ( auto-fit , minmax ( 200 px , 1 fr )); /* responsive */
gap : 16 px ;
}
05. Grid — Placement & Areas
Items can span multiple cells with grid-column and grid-row. Named grid-template-areas create a visual ASCII-art map of your layout.
Preview index.html style.css
< div class = "ga-grid" >
< div class = "ga-area ga-header" >header</ div >
< div class = "ga-area ga-sidebar" >sidebar</ div >
< div class = "ga-area ga-main" >main</ div >
< div class = "ga-area ga-footer" >footer</ div >
</ div > .ga-grid {
display : grid ;
grid-template-columns : 140 px 1 fr ;
grid-template-rows : 48 px 1 fr 40 px ;
grid-template-areas :
"header header"
"sidebar main"
"footer footer" ;
gap : 6 px ; height : 200 px ;
}
.ga-area { border-radius : 6 px ;
display : flex ;
align-items : center ;
justify-content : center ;
font-size : .75 rem ;
font-weight : 600 ;
color : white ;
font-family : monospace ;
}
.ga-header {
grid-area : header;
background : #6366f1 ;
}
.ga-sidebar {
grid-area : sidebar;
background : #818cf8 ;
}
.ga-main {
grid-area : main;
background : #a5b4fc ;
color : #1e1b4b ;
}
.ga-footer {
grid-area : footer;
background : #c7d2fe ;
color : #1e1b4b ;
}
.layout {
display : grid ;
grid-template-columns : 200 px 1 fr ;
grid-template-rows : 60 px 1 fr 60 px ;
grid-template-areas :
"header header"
"sidebar main"
"footer footer" ;
gap : 8 px ;
}
.header { grid-area : header; }
.sidebar { grid-area : sidebar; }
.main { grid-area : main; }
.footer { grid-area : footer; }
/* manual placement */
.hero { grid-column : 1 / 3 ; } /* spans columns 1 and 2 */
.wide { grid-column : 1 / -1 ; } /* spans all columns */
.tall { grid-row : 1 / span 2 ; } /* spans 2 rows */