CSS Height & Width

CSS Height and Width properties are used to control the size of HTML elements. These properties help create responsive, attractive, and well-structured web pages.

1. Introduction to CSS Height & Width

Every HTML element has a height and width. CSS allows you to control these dimensions according to your design needs.

Main Properties
  • width
  • height
  • max-width
  • min-width
  • max-height
  • min-height

.box{

    width:300px;

    height:200px;

}
Height and width control the overall size of HTML elements.

2. The Width Property

The width property specifies the horizontal size of an element.


.box{

    width:400px;

}
Example

.container{

    width:80%;

}
Value Description
300px Fixed width.
80% Relative to parent element.
auto Default width.
Percentage widths are commonly used for responsive layouts.

3. The Height Property

The height property specifies the vertical size of an element.


.box{

    height:250px;

}
Example

.banner{

    height:60vh;

}
Value Description
250px Fixed height.
60vh 60% of the viewport height.
auto Height adjusts automatically.
Avoid fixed heights for large text content because it may overflow.

4. Width & Height Units

CSS supports various units for specifying width and height.

Unit Description
px Fixed pixels.
% Relative to the parent element.
vw Viewport width.
vh Viewport height.
auto Automatic size.

.box{

    width:50vw;

    height:40vh;

}
vw and vh are useful for creating responsive layouts.

5. max-width & min-width

The max-width property limits the maximum width of an element, while min-width sets the minimum width.


.container{

    width:100%;

    max-width:1200px;

}
Minimum Width Example

.box{

    min-width:250px;

}
Property Purpose
max-width Prevents an element from becoming too wide.
min-width Prevents an element from becoming too narrow.
max-width is widely used to create responsive containers that look good on both large and small screens.

6. max-height & min-height

The max-height property limits the maximum height of an element, while min-height specifies the minimum height.


.box{

    min-height:150px;

    max-height:400px;

}
Example

.content{

    min-height:300px;

    max-height:600px;

    overflow:auto;

}
Property Description
min-height Prevents an element from becoming too short.
max-height Prevents an element from becoming too tall.
Use overflow:auto; when content may exceed the maximum height.

7. Responsive Width & Height

Responsive layouts automatically adjust element sizes on different devices.


.container{

    width:90%;

    max-width:1200px;

    margin:auto;

}
Media Query Example

@media(max-width:768px){

    .box{

        width:100%;

        height:auto;

    }

}
Technique Benefit
Percentage Width Adapts to screen size.
max-width Prevents oversized layouts.
height:auto Maintains proper proportions.
Responsive sizing improves the user experience on desktops, tablets, and mobile phones.

8. Width, Height & the CSS Box Model

Width and height define the content area. Padding, border, and margin increase the total space occupied by an element.

Box Model Part Description
Content Specified width and height.
Padding Space inside the border.
Border Surrounds the padding.
Margin Outer spacing.

.box{

    width:300px;

    height:200px;

    padding:20px;

    border:2px solid black;

    margin:20px;

}
Remember that padding and borders increase the total element size unless box-sizing:border-box; is used.

9. Practical Height & Width Examples

Example 1: Image

img{

    width:100%;

    height:auto;

}

Example 2: Banner

.banner{

    width:100%;

    height:300px;

}

Example 3: Card

.card{

    width:350px;

    min-height:250px;

}

Example 4: Profile Image

.profile{

    width:150px;

    height:150px;

    border-radius:50%;

}
Width and height properties are widely used in cards, images, banners, buttons, and containers.

10. box-sizing with Width & Height

The box-sizing property controls how width and height are calculated.


.box{

    width:300px;

    padding:20px;

    border:5px solid blue;

    box-sizing:border-box;

}
Value Meaning
content-box Default. Width and height apply only to the content.
border-box Width and height include padding and border.
Modern websites commonly use box-sizing:border-box; because it makes layouts easier to manage.

11. CSS Height & Width Best Practices

Choosing appropriate height and width values helps create responsive, attractive, and user-friendly web pages.

  • Use percentage (%) widths for responsive layouts.
  • Use max-width to prevent oversized containers.
  • Avoid fixed heights for text-heavy content.
  • Use height:auto; for images to maintain aspect ratio.
  • Use box-sizing:border-box; for easier layout calculations.
  • Use viewport units (vw and vh) carefully.
  • Test layouts on desktop, tablet, and mobile devices.
  • Keep element sizes consistent throughout the website.
Responsive sizing improves usability and provides a better user experience.

12. Common Mistakes

Mistake Correct Practice
Using fixed widths everywhere Use responsive widths whenever possible.
Giving fixed heights to text containers Allow height to grow automatically.
Ignoring max-width Limit large containers using max-width.
Stretching images Use height:auto;.
Ignoring box-sizing Use border-box for predictable sizing.

❌ Incorrect

img{

    width:300px;

    height:300px;

}

✔ Better

img{

    width:300px;

    height:auto;

}
Incorrect height and width values can break responsive layouts and distort images.

13. Browser Support

CSS height and width properties are supported by all modern browsers.

Browser Support
Google Chrome ✔ Supported
Mozilla Firefox ✔ Supported
Microsoft Edge ✔ Supported
Safari ✔ Supported
Opera ✔ Supported
You can safely use these properties in all modern web browsers.

14. CSS Height & Width Interview Questions

  1. What is the purpose of the width property?
  2. What is the difference between width and max-width?
  3. Why is height:auto; useful for images?
  4. What are viewport units (vw and vh)?
  5. What is the difference between min-width and max-width?
  6. Explain min-height and max-height.
  7. How does box-sizing:border-box; affect width and height?
  8. Why should fixed heights be avoided for large text content?
  9. How can you make an image responsive using CSS?
  10. Which CSS properties are commonly used to build responsive layouts?
These questions are frequently asked in HTML & CSS interviews and practical examinations.

15. Lesson Summary

In this lesson, you learned:
  • ✔ Introduction to CSS Height & Width.
  • ✔ Width Property.
  • ✔ Height Property.
  • ✔ Width & Height Units.
  • ✔ max-width & min-width.
  • ✔ max-height & min-height.
  • ✔ Responsive Width & Height.
  • ✔ CSS Box Model.
  • ✔ Practical Examples.
  • ✔ box-sizing with Width & Height.
  • ✔ Best Practices and Common Mistakes.
Congratulations! You have successfully completed the CSS Height & Width lesson. You can now create responsive layouts by controlling the size of elements using height, width, minimum and maximum dimensions, and the CSS Box Model.

Quick Quiz

Which CSS property prevents an element from becoming wider than a specified value?