Display, Position & Overflow
display controls how an element behaves in layout. block elements take full width and start on a new line. inline elements flow with text. none hides the element completely.
CSS Display
display controls how an element behaves in layout. block elements take full width and start on a new line. inline elements flow with text. none hides the element completely.
Real-life example: block is like a full-width bus stop. inline is like a word in a sentence. none is like removing a poster from the wall.
.block-box {
display: block;
background: #ecfeff;
margin-bottom: 1rem;
}
.hidden-mobile {
display: none; /* use carefully — still in DOM */
}CSS Position
position sets how an element is placed. static is default. relative moves from its normal spot. absolute is placed inside a positioned parent. fixed sticks to the viewport. sticky blends both.
Real-life example: fixed is like a sticker on your phone screen that stays while you scroll. absolute is like placing a sticker inside one photo frame.
.relative-badge {
position: relative;
top: 4px;
}
.parent {
position: relative;
}
.badge {
position: absolute;
top: 8px;
right: 8px;
background: #ef4444;
color: white;
padding: 0.15rem 0.5rem;
border-radius: 999px;
font-size: 0.75rem;
}
.site-header {
position: sticky;
top: 0;
z-index: 100;
background: white;
}Position Offsets & z-index
top, right, bottom, and left move positioned elements. z-index controls stacking order — higher numbers appear on top.
Real-life example: z-index is like stack order of plates — the plate with the higher number sits on top.
.modal-backdrop {
position: fixed;
inset: 0;
background: rgba(15, 23, 42, 0.5);
z-index: 1000;
}
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 1001;
background: white;
padding: 1.5rem;
border-radius: 12px;
}Overflow
overflow controls what happens when content is bigger than its box. hidden cuts off extra content. auto adds scrollbars when needed. scroll always shows scrollbars.
Real-life example: overflow: hidden is like a window too small for a long banner — you only see the part that fits.
.scroll-box {
max-height: 200px;
overflow-y: auto;
border: 1px solid #e2e8f0;
padding: 1rem;
}
.avatar-crop {
width: 80px;
height: 80px;
overflow: hidden;
border-radius: 50%;
}Float (legacy) & inline-block
float was used for text wrapping around images and old layouts. Today prefer Flexbox and Grid. inline-block lets elements sit side by side but still accept width and height.
Real-life example: float is like an old boat layout — still floating in some old websites. inline-block is like small boxes that can sit in a row but still have fixed size.
/* Legacy — know it when reading old code */
.img-float {
float: left;
margin-right: 1rem;
}
/* Modern alternative for small rows */
.tag {
display: inline-block;
padding: 0.25rem 0.75rem;
background: #ecfeff;
border-radius: 999px;
margin: 0.25rem;
}Align & Centering
text-align centers inline content. margin: auto centers block elements with a set width. For modern layouts, Flexbox and Grid centering are easier and more powerful.
Real-life example: margin: auto is like equal empty space on left and right of a car in a parking slot.
.narrow {
max-width: 600px;
margin-inline: auto;
text-align: center;
}
.flex-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 200px;
}