CSS Height & Width
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;
}
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. |
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. |
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;
}
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. |
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. |
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. |
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;
}
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%;
}
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. |
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-widthto 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 (
vwandvh) carefully. - Test layouts on desktop, tablet, and mobile devices.
- Keep element sizes consistent throughout the website.
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;
}
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 |
14. CSS Height & Width Interview Questions
- What is the purpose of the
widthproperty? - What is the difference between
widthandmax-width? - Why is
height:auto;useful for images? - What are viewport units (
vwandvh)? - What is the difference between
min-widthandmax-width? - Explain
min-heightandmax-height. - How does
box-sizing:border-box;affect width and height? - Why should fixed heights be avoided for large text content?
- How can you make an image responsive using CSS?
- Which CSS properties are commonly used to build responsive layouts?
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.
Quick Quiz
Which CSS property prevents an element from becoming wider than a specified value?