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:
Completa el código:
Interactive Test 2: Fill in the Blanks
Rellena los huecos en cada casilla.
.container { width: ; max-width: ; box-sizing: ; }
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
Property | Description |
---|---|
width | Defines the width of an element. |
height | Defines the height of an element. |
max-width | Sets the maximum width of an element. |
min-width | Sets the minimum width of an element. |
max-height | Sets the maximum height of an element. |
min-height | Sets the minimum height of an element. |
* Write your CSS code and apply to see the results.
Results:
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 */
}
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; }
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;
}
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.