CHAPTER 11

CSS Height and Width

Learn how to control the height and width of HTML elements using CSS.

01. What is CSS Height and Width?

The height property controls the vertical size of an element.
The width property controls the horizontal size of an element.
CSS also provides min-width, max-width, min-height and max-height for responsive layouts.

02. Syntax

selector { width: value; height: value; }

03. Example

CSS / HTML Example AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>

<style>

.box {
    width: 300px;
    height: 150px;

    background: #2563eb;
    color: white;

    padding: 20px;

    box-sizing: border-box;
}

</style>

</head>

<body>

<div class="box">
    AIFA CSS Coding Lab
</div>

</body>
</html>

04. Important Points

✓ width controls horizontal size.
✓ height controls vertical size.
✓ Use max-width for responsive layouts.
✓ Use box-sizing: border-box when you want padding and border included in the element size.

05. Common Mistakes

⚠ Using a fixed width that makes the website overflow on mobile.
⚠ Forgetting that padding and border can increase the final size when box-sizing is not used.
⚠ Using unnecessary fixed heights for content that may change.

06. Real World Use

Width and height are commonly used for cards, images, buttons, containers, banners and website sections.

Ready to Practice? 🚀

Open the AIFA CSS Editor and practice this chapter live.

Try It Yourself →
Practice Exercise

Create a CSS Card

Create a card with width 300px and height 200px. Add a background color, padding and a heading.