CSS Height / Width Get Certified
What are CSS Height and Width?
The CSS height and width properties control the size of HTML elements. You can set fixed sizes (px), relative sizes (%), or use modern units like rem, vw, and vh.
Why Use Height and Width?
These properties give you precise control over layout, help create consistent designs, and are essential for responsive websites when combined with max-width and percentage values.
Step 1: Set Element Width
The width property defines the horizontal size of an element. You can use pixels, percentages, and viewport units.
What this code does: Creates a green box with a fixed width of 400px.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-box{
width:400px;
background:#28a745;
color:white;
padding:20px;
}
</style>
</head>
<body>
<div class="demo-box">
This box has 400px width
</div>
</body>
</html>
Step 2: Set Element Height
The height property defines the vertical size of an element. It controls how much space an element occupies from top to bottom.
What this code does: Creates an orange box with fixed width and height, centered content using Flexbox.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-box{
width:250px;
height:200px;
background:#ff9800;
color:white;
display:flex;
align-items:center;
justify-content:center;
}
</style>
</head>
<body>
<div class="demo-box">
Height Example
</div>
</body>
</html>
Step 3: Using Percentage Width and Height
Percentage values make elements flexible according to their parent container. They are useful for creating responsive layouts.
What this code does: Makes a purple box take 50% of the width and height of its parent container.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-container{
width:80%;
height:200px;
background:#ddd;
}
.demo-box{
width:50%;
height:50%;
background:#6f42c1;
color:white;
}
</style>
</head>
<body>
<div class="demo-container">
<div class="demo-box">
50% Width and Height
</div>
</div>
</body>
</html>
Step 4: Maximum and Minimum Height/Width
CSS provides min-width, max-width, min-height, and max-height properties to control the limits of an element size.
What this code does: Sets a box that can grow up to 400px wide and has a minimum height of 150px.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-box{
width:100%;
max-width:400px;
min-height:150px;
background:#17a2b8;
color:white;
padding:20px;
}
</style>
</head>
<body>
<div class="demo-box">
Maximum Width Example
</div>
</body>
</html>
Responsive Width Example
For responsive websites, developers usually combine percentage widths with max-width so elements can adjust according to the screen size.
What this code does: Makes an image responsive — it scales with the container but never exceeds 500px.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-image-box{
width:100%;
max-width:500px;
}
.demo-image-box img{
width:100%;
height:auto;
}
</style>
</head>
<body>
<div class="demo-image-box">
<img src="https://picsum.photos/500/300"
alt="Responsive Image">
</div>
</body>
</html>
Complete CSS Height and Width Example
What this code does: Creates a centered responsive container with a fixed-height blue box inside.
<!DOCTYPE html>
<html>
<head>
<style>
body{
font-family:Arial;
background:#f5f9ff;
}
.demo-container{
width:90%;
max-width:600px;
height:auto;
margin:auto;
background:white;
padding:30px;
border:2px solid #007BFF;
}
.demo-box{
width:100%;
height:150px;
background:#007BFF;
color:white;
display:flex;
justify-content:center;
align-items:center;
font-size:22px;
}
</style>
</head>
<body>
<div class="demo-container">
<div class="demo-box">
Responsive Box
</div>
</div>
</body>
</html>
Advantages of CSS Height and Width
Common Mistakes
Beginners often forget CSS units, use fixed sizes everywhere, set very large heights, or create layouts that do not adjust on mobile devices.
What this code does: Shows the common mistake of forgetting units, and the correct version with px.
<!DOCTYPE html>
<html>
<head>
<style>
/* Incorrect */
.demo-box{
width:300;
height:200;
}
/* Correct */
.demo-box{
width:300px;
height:200px;
}
</style>
</head>
<body>
<div class="demo-box">Size Example</div>
</body>
</html>
Best Practices
- Use responsive units like %, rem, and vw when possible.
- Use max-width to prevent content from becoming too wide.
- Use height:auto for responsive images.
- Avoid fixed heights for content that can change.
- Use min-width and max-width for flexible layouts.
- Test designs on mobile, tablet, and desktop screens.
- Use CSS Flexbox or Grid with height and width properties.
- Keep element sizes consistent throughout your design.
Web Resources on CSS Height and Width
1. MDN Web Docs : height CSS property
2. MDN Web Docs : width CSS property
3. CSS Tricks.com : Exploring the Complexities of Width and Height in CSS
4. Dev.to : Difference between height, min-height, max-height, width, min-width, max-width
Click “Run Code” to see the result here.
CSS Syntax Example
The CSS syntax consists of a selector and a declaration block.
h1 { color: blue; font-size: 36px;}Selector: h1 – targets all <h1> elements
Property: color and font-size
Value: blue and 36px
Your CSS Applied Below:
This is an H1 heading with your CSS
This is an H2 heading (not affected)
This paragraph is not affected by the h1 selector.
Welcome to CSS
This page is styled using CSS.
The heading and paragraph use your CSS rules.
- List item with your styles
- Another list item
🧪 Test Your CSS Code
Edit the CSS code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.