-
Hajipur, Bihar, 844101
CSS units define how sizes, spacing, and dimensions are measured in a webpage. Every value related to width, height, margin, padding, font size, positioning, and layout depends on CSS units. Without a clear understanding of CSS units, it becomes very difficult to build responsive, consistent, and scalable designs.
In this chapter, you will learn what CSS units are, why they matter, the different types of CSS units, how they behave, and when to use each one in real-world layouts.
CSS units are measurement values used to define the size of elements and text on a webpage. They tell the browser how big or small something should appear.
Example:
p {
font-size: 16px;
}
Here, px is a CSS unit that defines the size of the text.
CSS units can be divided into two main categories:
Absolute units
Relative units
Each category serves a different purpose in layout and design.
Choosing the right CSS unit directly affects how your website looks across different screen sizes and devices.
CSS units help you:
Control spacing and layout precisely
Create responsive designs
Improve accessibility
Maintain consistency across pages
Adapt designs for mobile, tablet, and desktop
Poor unit choices often lead to layouts that break on small screens or look inconsistent on large displays.
CSS provides many units, but they can be grouped logically to make them easier to understand.
The two main types are:
Absolute units
Relative units
Let’s explore each in detail.
Absolute units have fixed values. They do not change based on screen size, parent elements, or user settings.
Common absolute units include:
px
cm
mm
in
pt
pc
Among these, px is the most commonly used in web design.
The px unit represents a pixel on the screen. It is widely used because it provides precise control.
Example:
div {
width: 300px;
height: 150px;
}
This element will always be exactly 300 pixels wide and 150 pixels tall.
Easy to understand
Precise control
Predictable layout
Not responsive by default
Does not scale well with user font settings
Can cause accessibility issues
Because of these limitations, px should be used carefully, especially for font sizes.
Units like cm, mm, in, pt, and pc are based on physical measurements. They are rarely used in web design because screen sizes and resolutions vary greatly.
Example:
p {
font-size: 12pt;
}
These units are more useful for print stylesheets than for screen layouts.
Relative units are flexible. Their values depend on another measurement such as font size, viewport size, or parent element size.
Relative units are preferred for responsive and modern web design.
Common relative units include:
em
rem
%
vw
vh
vmin
vmax
Each of these units behaves differently.
The em unit is relative to the font size of the parent element.
Example:
div {
font-size: 16px;
}
p {
font-size: 1.5em;
}
The paragraph text will be 24px because 1.5em equals 1.5 times the parent font size.
Padding and margin relative to text
Scalable components
Nested elements
em values compound when elements are nested, which can cause unexpected results if not handled carefully.
The rem unit is relative to the root element’s font size, usually the html element.
Example:
html {
font-size: 16px;
}
h1 {
font-size: 2rem;
}
The heading will be 32px regardless of nesting.
Predictable behavior
Easier to manage
Better for accessibility
Ideal for font sizes
Most modern CSS frameworks use rem extensively.
The % unit is relative to the size of the parent element.
Example:
div {
width: 50%;
}
The element will take half the width of its parent.
Fluid layouts
Responsive containers
Flexible widths
Percentages are powerful but depend heavily on the parent element’s size.
Viewport units are based on the size of the browser window.
Common viewport units include:
vw
vh
vmin
vmax
These units help create layouts that adapt directly to screen size.
vw stands for viewport width.
1vw equals 1 percent of the viewport width.
Example:
div {
width: 50vw;
}
The element will be half the width of the browser window.
vh stands for viewport height.
1vh equals 1 percent of the viewport height.
Example:
section {
height: 100vh;
}
This makes the section fill the entire screen height.
vmin uses the smaller value of viewport width or height
vmax uses the larger value
These units are useful for responsive typography and scaling elements proportionally.
Choosing the right unit for font size is critical.
Recommended practices:
Use rem for base typography
Avoid fixed px for body text
Use em for component-level scaling
Example:
body {
font-size: 1rem;
}
h1 {
font-size: 2.5rem;
}
This approach scales well across devices and user preferences.
Different layout situations require different units.
Common patterns:
Use % for flexible containers
Use vw and vh for full-screen sections
Use rem for spacing consistency
Use px for small, fixed elements
Combining units intelligently creates strong layouts.
It is normal and often recommended to mix CSS units.
Example:
.card {
width: 90%;
max-width: 400px;
padding: 1.5rem;
}
This provides flexibility while maintaining limits.
Relative units improve accessibility.
Users who increase font size in browser settings benefit when rem and em are used.
Avoid locking text sizes using fixed px values whenever possible.
Some frequent errors include:
Using only px everywhere
Ignoring parent size when using %
Overusing viewport units for text
Not testing across screen sizes
Understanding how units behave prevents these issues.
Follow these best practices for clean and scalable CSS:
Use rem for font sizes
Use % for flexible widths
Use vw and vh for full-screen layouts
Avoid unnecessary absolute units
Test designs on multiple devices
These habits lead to better responsive design.
CSS units are the foundation of layout, spacing, and typography in web design. Absolute units like px provide precision, while relative units like rem, em, %, and viewport units enable responsive and accessible layouts. Understanding how each unit works and when to use it helps you build flexible, user-friendly, and modern websites. Mastering CSS units is a key step toward writing professional and maintainable CSS.
Q1. Set a div width to 500 pixels.
Q2. Set font size to 2em inside a div.
Q3. Make a section full height of the screen using vh.
Q4. Set padding as 10% of the parent width.
Q5. Make a container 60 characters wide using ch.
Q6. Use rem to define font size of 24px (assuming root is 16px).
Q7. Set image width to 80% of its parent.
Q8. Create a box that is 5cm wide and 2in tall.
Q9. Use vmin to adjust font-size for small screens.
Q10. Create two divs with widths 60vw and 40vw side by side.