💡 Tip: Login now to track your course progress and unlock your downloadable certificates instantly!

CSS Width and Height

The CSS Width and Height properties are used to define the size of an HTML element.

  • Width → controls the horizontal size
  • Height → controls the vertical size

These properties help in designing layouts and controlling how elements appear on a webpage.

Syntax
selector {
    width: value;
    height: value;
}
Example
div {
    width: 200px;
    height: 100px;
}

👉 This sets the element width to 200px and height to 100px.

Attributes

PropertyDescriptionExample
widthSets element widthwidth: 300px;
heightSets element heightheight: 150px;
max-widthSets maximum widthmax-width: 100%;
min-widthSets minimum widthmin-width: 200px;
max-heightSets maximum heightmax-height: 400px;

Example

Complete CSS Width and Height Example
<!DOCTYPE html>
<html>
<head>
    <title>CSS Width and Height</title>
    <style>
        div {
            width: 300px;
            height: 150px;
            background-color: lightblue;
            padding: 10px;
            border: 2px solid black;
        }

        img {
            width: 200px;
            height: auto;
        }
    </style>
</head>
<body>

    <div>This box has fixed width and height.</div>

    <img src="image.jpg" alt="Sample Image">

</body>
</html>

Output

Browser Output

The <div> will appear as a fixed-size box (300px × 150px)
The box will have background color, padding, and border
The image will have a fixed width and proportional height

Browser Support

Chrome
Chrome
Firefox
Firefox
Edge
Edge
Safari
Safari
Opera
Opera
IE
IE9+
✅Yes✅Yes✅Yes✅Yes✅Yes✅Yes

Notes

  • Width and height can be set using:
    • px (pixels)
    • % (percentage)
    • auto (default)
  • height: auto maintains aspect ratio for images
  • Use max-width: 100% for responsive design
  • Padding and border can affect total size (box model)
  • Avoid fixed heights for flexible layouts

Conclusion

CSS Width and Height properties are essential for controlling element size and layout. When used properly, they help create responsive and well-structured web designs.

Sign In Form

Use your email and password to sign in

Provide your email and we will send you a password reset link.

Don’t have an account, signup here : 

HTML5 & CSS3

Tools and Tutorials

Registration Form

Signup to track your record and much more.

We have sent you an email with a registration link. Please click the link to verify your email address, or enter the 6-digit OTP provided in the email.

Didn't receive the OTP. Regenerate OTP Resend