Geek Slack

Introduction to CSS
About Lesson



CSS Height, Width, and Max-Width


CSS Height, Width, and Max-Width

In CSS, the height and width properties are used to set the height and width of an element. The max-width property is used to set the maximum width of an element, preventing it from growing beyond the specified value.

Height and Width

The height and width properties can be set using various units like pixels (px), percentage (%), em, rem, etc.

div {
    height: 100px;
    width: 200px;
}

This div has a height of 100px and a width of 200px.

Max-Width

The max-width property sets the maximum width of an element. It prevents the element from growing wider than the specified value, even if the content inside it grows.

div {
    max-width: 100%;
}

Resize the window to see the effect of max-width: 100%;. This div will not grow wider than the window.

Height, Width, and Max-Width Example

Combining height, width, and max-width properties allows for flexible and responsive designs.

div {
    height: 100px;
    width: 50%;
    max-width: 400px;
}

This div has a height of 100px, a width of 50% of its container, and a maximum width of 400px.

Auto Value

The auto value for height and width allows the browser to calculate the dimensions of the element based on its content.

div {
    width: auto;
    height: auto;
}

This div has its width and height set to auto, so it adjusts based on the content.

Conclusion

The height, width, and max-width properties are fundamental in creating responsive and well-structured web layouts. Understanding how to use these properties effectively is crucial for web design and development.

Join the conversation