2025-08-01 — By Siddharth Jain · 7 min read
Modern CSS offers you a range of ways to size, position, and lay out your webpage elements. This guide breaks down what every CSS beginner should know about units like px, em, rem, %, the differences between display: block and inline, and how position: absolute works.
Let’s dive in step-by-step!
px
(Pixels)font-size: 16px
, it will always be 16 pixels on every screen..title {
font-size: 24px;
margin: 10px;
}
rem
(Root EM)html { font-size: 16px }
by default, so 1rem
is 16px unless changed.body {
font-size: 1rem; /* 16px by default */
}
h1 {
font-size: 2rem; /* 32px (2 x 16px) */
}
em
(Element Relative).button {
font-size: 1em; /* Same as parent */
padding: 0.5em 2em; /* Padding relative to font size */
}
%
(Percentages)width: 50%
makes the width half of its parent’s width..container {
width: 80%;
}
.child {
width: 50%; /* 50% of .container's width */
}
5%
specifically means 5 percent of the parent’s size.
width: 5%
means 50px.position: absolute
do?position
set (relative
, absolute
, or fixed
).top
, right
, bottom
, left
to place the element..parent {
position: relative;
}
.child {
position: absolute;
top: 20px;
left: 20px;
}
Visual Example:
display: block
,
, ,
.box {
display: block;
width: 70%;
margin: 20px auto;
}
display: inline
,
, ``.highlight {
display: inline;
color: orange;
}
display: inline-block
Absolutely Positioned! This is a highlighted word inside a block paragraph.
Unit/Property | Meaning / Relative to | Best Used For |
---|---|---|
px (pixels) | Exact, absolute (screen) | Icons, borders, fixed elements |
rem (Root EM) | Root html font-size | Typography, site-wide scaling |
em (Element EM) | Parent/element font-size | Buttons, component spacing |
% (Percent) | Parent element's dimension | Responsive layouts, containers |
position: absolute | Nearest positioned ancestor | Overlays, badges, tooltips |
display: block | Starts new line, full width | Layout blocks, divs, sections |
display: inline | No line break, auto width | Text styling within lines |
rem
for font sizes for accessibility.%
for adaptable layouts.em
for responsive spacing inside buttons/components.position: absolute
for overlays, always within a relatively positioned parent.display: block
for main content and inline
for text, or inline-block
for flexible components.