🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Expert Masterclasses.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

CSS Dimensions

Control space and size. Master width, height, and responsive units to build flexible layouts.

style.css
.container {
width: 100%;
max-width: 800px;
height: 100vh;
}
75% Width
styles.css
1 / 8
📏

Tutor:Control the size of elements using width and height. By default, block elements like <div> take up 100% width.


CSS Dimensions Tree

Unlock nodes by learning width, height, and responsive units.

Concept 1: Basic Dimensions

The width and height properties set the size of the content box. Pixels (px) are the most basic unit, representing a fixed dot on the screen.

System Check

Which property sets the vertical size of an element?


Layout Community

Share Layout Patterns

Built a cool responsive grid or flexible card? Share your sizing snippets.

Enjoying this guide?

Codesyllabus is 100% free and open-source. Support our mission, pay for server infrastructure, and fuel new tutorials by buying us a coffee!

CSS Dimensions: Width & Height

Author

Pascual Vila

Frontend Instructor.

Controlling the dimensions of elements is fundamental to CSS layout. The width and height properties determine the size of the content area inside an element's box model.

Units of Measurement

CSS offers various units to control sizing, each with its own behavior:

  • Pixels (px): Absolute units. 200px is always 200 pixels, regardless of screen size. Great for fixed components but rigid.
  • Percentages (%): Relative units. 50% means half the width of the parent container. Essential for fluid layouts.
  • Viewport Units (vh/vw): Relative to the browser window. 100vh is the full height of the visible screen.
  • REM/EM: Relative to font size. Useful for scaling layouts based on text settings.

Constraints: Min and Max

To prevent elements from becoming too small or too large, we use constraint properties:

max-width stops an element from growing beyond a certain point (e.g., ensuring text lines don't get too long to read).
min-height ensures an element is at least a certain height, even if it has no content.

The Box Model

Remember that padding and borders add to the total width of an element unless you use box-sizing: border-box. This is a common pitfall when calculating dimensions.

Dimensions Glossary

width / height
Properties used to set the horizontal and vertical size of an element's content area.
max-width
Defines the maximum width an element can reach. If the viewport is narrower, the element shrinks.
Viewport Units (vw, vh)
Units relative to the browser window size. 1vw = 1% of viewport width.
Pixel (px)
A static unit of measurement. It corresponds to a single point on the screen.