#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS Height/Width

The width and height properties set the size of an element's content area, or its border box if you change box-sizing. Together with min- and max- versions, they control how big boxes are and how they react when the screen or the content changes size. After learning padding, this is where you see how size, padding and borders combine.

Values you can use

  • auto: the default. Block elements stretch to fill the available width; height grows to fit the content.
  • Lengths: px, rem, em and so on, for a fixed size.
  • Percentages: relative to the width or height of the containing block.
  • Viewport units: vw, vh, and newer svh, lvh and dvh for the visible screen height on mobile.
  • Keywords: min-content, max-content and fit-content, which size the box from its content.

Why width plus padding can break a layout

By default, box-sizing is content-box. A box with width: 300px, padding: 20px and a 2px border is actually 344px wide on screen. Many developers switch every element to border-box, so width includes padding and border and the box stays at the number you wrote.

*,
*::before,
*::after {
  box-sizing: border-box;
}

.card {
  width: 100%;
  max-width: 420px;   /* never wider than this */
  min-height: 180px;  /* grows if content is longer */
  padding: 1.25rem;
  border: 1px solid #ddd;
  margin-inline: auto; /* centre it */
}

.thumb {
  width: 100%;
  height: auto;       /* keep the image's proportions */
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

min and max make sizes flexible

Fixed widths are fragile on phones. A better habit is width: 100% with a max-width, so the element fills small screens and stops growing on large ones. Similarly, min-height is usually safer than height, because a fixed height cuts off or overflows content when the text is longer than expected, for example after translation or when a user enlarges the font.

When both are set, min-width beats max-width, and max-width beats width.

The percentage height puzzle

A very common surprise is that height: 100% seems to do nothing. A percentage height only works when the parent has a definite height. If the parent's height is auto, the percentage has nothing to measure against and falls back to auto. For full-screen sections, use min-height: 100vh, or 100dvh to account for mobile browser toolbars, instead of chaining height: 100% up through every ancestor.

Also note that width and height have no effect on inline elements such as <span> or <a>. Change them to inline-block or block first.

Sizing images and media

Set max-width: 100% and height: auto on images so they shrink inside narrow containers without stretching. Keep the width and height attributes in the HTML as well; browsers use them to reserve space before the image loads, which stops the page from jumping.

Flexbox and Grid add another twist. Inside a flex container, items can shrink below the width you set, because flex-shrink defaults to 1. If a sidebar keeps getting narrower than its width, add flex-shrink: 0 or use flex: 0 0 240px.

Rules of thumb

  • Use box-sizing: border-box everywhere.
  • Prefer max-width and min-height over fixed sizes.
  • Percentage heights need a parent with a known height.
  • Inline elements ignore width and height.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course