Sizing Elements: Width and Height Properties in CSS

Define the dimensions of your web elements with precision using the foundational width and height properties.

Welcome! Today, we'll learn to control the size of elements with the `width` and `height` properties.

/* Let's begin! */

The 'width' Property

The width property defines the horizontal dimension of an element. You can use absolute units like pixels (px), or relative units like percentages (%) which size the element in relation to its parent container. This is crucial for creating responsive designs.

The 'height' Property

The height property sets the vertical dimension. Similar to width, it accepts various units. For example, height: 50vh; would make an element's height equal to 50% of the viewport's (browser window's) height.

Setting Boundaries with min/max

Use min-width and max-width to create flexible layouts. For instance, width: 80%; max-width: 800px; ensures an element is responsive but never gets too wide on large screens. min-height is great for ensuring footers stay at the bottom of the page.

The 'box-sizing' Property

The box-sizing property changes how width and height are calculated. By default (content-box), padding and borders are added *outside* the width. With box-sizing: border-box;, they are included *inside*, which makes layout math much more intuitive.

Practice Zone


Interactive Test 1: Drag & Drop

Arrastra en el orden correspondiente.


Arrastra las opciones:

height
width

Completa el código:

.card { ______: 300px;
______: 400px; }
Unlock with Premium

Interactive Test 2: Fill in the Blanks

Rellena los huecos en cada casilla.

.container {
  width: ;
  max-width: ;
  box-sizing: ;
}
Unlock with Premium

Practice Example: Code Editor

Create a CSS rule for a `div` element that gives it a width of `300px` and a height of `150px`.

Width and Height Properties

PropertyDescription
widthDefines the width of an element.
heightDefines the height of an element.
max-widthSets the maximum width of an element.
min-widthSets the minimum width of an element.
max-heightSets the maximum height of an element.
min-heightSets the minimum height of an element.

* Write your CSS code and apply to see the results.

Results:

Unlock with Premium

Knowledge Check

Which 'box-sizing' value includes padding and border in the element's total width and height?


Unlock with Premium

A Practical Guide to Mastering Element Sizing

Sizing isn't just about setting a width and height. It's about building robust, flexible components that adapt to different screen sizes and content. Let's explore some advanced, real-world techniques.


1. Fluid Layouts with `max-width`

To make a layout responsive, instead of a fixed width, use width: 100%; combined with max-width. This allows the element to shrink on smaller screens but stops it from becoming too wide on larger ones, improving readability.

.container {
  width: 100%;
  max-width: 800px;
  margin: 0 auto; /* Center it */
}
I'm responsive!

2. The `box-sizing: border-box` Advantage

By default, an element's `width` only applies to its content. Padding and borders are added on top, which can break your layout. Using box-sizing: border-box; makes the `width` property define the *total* width, including padding and border. It's so useful, many developers apply it to all elements.

/* Default: 100px + 40px padding = 140px wide */
.box-1 { width: 100px; padding: 20px; }

/* border-box: Total width is exactly 100px */
.box-2 { width: 100px; padding: 20px;
         box-sizing: border-box; }
140px wide
100px wide

3. Full-Screen Sections with Viewport Units

Viewport units (`vh` for height, `vw` for width) are relative to the browser window size. height: 100vh; will make an element exactly as tall as the visible screen, perfect for hero sections or full-page slides.

.hero-section {
  width: 100%;
  height: 50vh; /* Half screen height */
  background: lightblue;
}
I'm 50vh tall

Practical Takeaway: Master sizing by thinking flexibly. Use percentages and `max-width` for fluidity, and always use `box-sizing: border-box` for predictable layouts. Viewport units give you powerful control relative to the screen itself.