CSS height Media Feature

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Media Queries

What You’ll Learn

The height media feature targets the viewport’s vertical size. Adapt layouts for tall desktops, short laptop windows, and landscape phones with min-height and max-height queries.

01

@media

Media query.

02

min-height

Tall screens.

03

max-height

Short screens.

04

Viewport

Window height.

05

vs width

Vertical axis.

06

vs property

Query vs size.

Introduction

Most responsive tutorials focus on width — stacking columns on narrow phones. The height media feature adds another axis: the viewport height, or how tall the browser window is.

A user on a wide but short window (split-screen laptop, landscape phone with keyboard open) may need compact headers and scrollable content. A tall viewport can support full-screen heroes and spacious vertical rhythm.

Definition and Usage

Write @media (min-height: 600px) or @media (max-height: 400px) to apply styles when the viewport meets your height condition. Values use length units such as px, em, rem, or viewport units like vh.

💡
Beginner Tip

Do not confuse this with the CSS height property, which sizes an element. The media feature checks the browser window height, not an individual box.

📝 Syntax

Use min-height, max-height, or an exact height inside @media:

syntax.css
/* Viewport is at least 600px tall */
@media (min-height: 600px) {
  /* Styles for tall viewports */
}

/* Viewport is at most 400px tall */
@media (max-height: 400px) {
  /* Styles for short viewports */
}

/* Exact viewport height (rare) */
@media (height: 768px) {
  /* Matches only at exactly 768px height */
}

Feature Variants

  • min-height — Matches when viewport height is greater than or equal to the value (tall screens).
  • max-height — Matches when viewport height is less than or equal to the value (short screens).
  • height — Matches an exact viewport height; rarely used because few users hit one precise pixel height.

height Media Feature vs height Property

@media (min-height: 600px) Viewport query — “Is the browser window at least 600px tall?”
height: 600px; Element sizing — sets one box to exactly 600 pixels tall.

height vs width Media Features

@media (min-width: 48rem) Horizontal axis — phones vs tablets vs desktops by window width.
@media (min-height: 32rem) Vertical axis — short split windows vs tall full-screen viewports.

Basic Example

height-basic.css
body {
  background-color: #dbeafe;
}

@media (min-height: 600px) {
  body {
    background-color: #dcfce7;
  }
}

@media (max-height: 400px) {
  body {
    background-color: #fee2e2;
  }
}

Default Value

There is no CSS default. You choose the threshold based on your layout needs. Prefer rem or vh for scalable breakpoints.

Syntax Rules

  • Use length values: px, em, rem, vh, dvh.
  • Combine with width: @media (min-width: 48rem) and (min-height: 32rem).
  • max-height overrides min-height when both match — order matters in CSS cascade.
  • Resize the browser vertically to test; DevTools device mode also supports height.
  • Mobile browser chrome (address bar) can change effective height — consider dvh for elements.

Related Topics

@media (min-height: 600px) @media (max-height: 400px) @media (min-height: 100dvh)

⚡ Quick Reference

QuestionAnswer
Feature namesheight, min-height, max-height
What it measuresViewport (browser window) height
Tall screens@media (min-height: 600px)
Short screens@media (max-height: 400px)
Common unitspx, rem, vh, dvh
Combine with widthand (min-width: 48rem)
Browser supportAll modern browsers

When to Use height

Reach for height-based media queries when vertical space drives the design:

  • Full-viewport heroes — Expand hero sections on tall screens with min-height.
  • Compact short windows — Shrink headers and padding when max-height is small.
  • Landscape mobile — Limited vertical space even when width is large.
  • Dashboard panels — Enable internal scrolling only when viewport is too short.
  • Split-screen desktop — Adapt when the window is wide but not tall.

👀 Live Preview

Resize your browser window vertically. This block turns green on tall viewports (min-height: 32rem) and red on short ones (max-height: 20rem):

Viewport Height Demo Default: medium height (blue)

Examples Gallery

Practice the height media feature with background thresholds, full-height heroes, compact headers, and combined width-height queries.

📜 Core Patterns

Adapt styles when the viewport is tall or short.

Example 1 — Background color by viewport height

Change page background based on tall, medium, and short viewport heights:

height-background.css
body {
  background-color: #dbeafe;
}

@media (min-height: 600px) {
  body {
    background-color: #dcfce7;
  }
}

@media (max-height: 400px) {
  body {
    background-color: #fee2e2;
  }
}
Try It Yourself

How It Works

Resize the browser height to see three color zones. This is the classic beginner pattern from the reference tutorial.

Example 2 — Full-height hero on tall screens

Give the hero section full viewport height only when enough vertical space exists:

height-hero.css
.hero {
  padding: 2rem 1.5rem;
  background: #1e293b;
  color: #fff;
  text-align: center;
}

@media (min-height: 32rem) {
  .hero {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
Try It Yourself

How It Works

Short viewports keep a compact hero; tall ones get a cinematic full-screen welcome section centered vertically.

Example 3 — Compact header on short viewports

Reduce header padding and font size when vertical space is limited:

height-compact.css
.site-header {
  padding: 1.25rem 1.5rem;
  background: #fff;
  border-bottom: 1px solid #e2e8f0;
  font-size: 1.125rem;
  font-weight: 600;
}

@media (max-height: 500px) {
  .site-header {
    padding: 0.5rem 1rem;
    font-size: 0.9375rem;
  }
}
Try It Yourself

How It Works

Split-screen and landscape layouts often have limited height. A slimmer header preserves content area.

Example 4 — Combined width and height query

Apply a spacious two-column layout only on large viewports that are both wide and tall:

height-combined.css
.layout {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (min-width: 48rem) and (min-height: 32rem) {
  .layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
  }
}
Try It Yourself

How It Works

A wide but short window (e.g. half-height laptop) stays single-column; both dimensions must be large for the two-column upgrade.

💬 Usage Tips

  • Test vertically — Drag the browser window shorter and taller, not just narrower.
  • Use rem breakpointsmin-height: 32rem scales with user font settings.
  • Pair with width — Avoid two-column layouts on wide-but-short viewports.
  • Consider dvh — Use 100dvh for full-height sections on mobile browsers.
  • Mobile landscapemax-height catches phones rotated sideways.

⚠️ Common Pitfalls

  • Confusing with height property — Media query checks viewport; property sizes elements.
  • Only using width queries — Short viewports need their own rules.
  • Exact height matches(height: 768px) rarely matches real users.
  • Ignoring cascade order — Later rules win when min and max both match.
  • Fixed vh on mobile — Address bar show/hide affects 100vh; prefer dvh.

♿ Accessibility

  • Zoom and text size — Large text increases effective viewport needs; test with zoom.
  • Scrollable content — Never hide essential content on short screens; allow scrolling.
  • Focus visibility — Compact headers must still show visible focus outlines.
  • Reduced motion — Height-based layout shifts should respect prefers-reduced-motion.
  • Keyboard users — Full-height heroes should not trap focus or hide skip links.

🧠 How height Works

1

Browser measures viewport

The user agent reads the visible window height in CSS pixels.

Measure
2

Media queries evaluate

min-height and max-height conditions are tested against that value.

Evaluate
3

Matching rules apply

Styles inside true @media blocks override defaults.

Apply
=

Height-aware layout

UI adapts when users resize vertically or use short viewports.

🖥 Browser Compatibility

The height, min-height, and max-height media features are supported in all modern browsers.

Baseline · Universal support

Viewport height queries

Works in Chrome, Firefox, Safari, Edge, and Opera.

99% Global support
height media features 99% supported

Bottom line: Height media queries are safe for production. Combine with width queries for complete responsive coverage.

🎉 Conclusion

The height media feature lets you respond to viewport height with min-height, max-height, and exact height queries. It complements width-based breakpoints for truly flexible layouts.

Use it for full-viewport heroes on tall screens, compact UI on short windows, and combined width-and-height rules for large desktops. Always distinguish viewport height queries from the CSS height property on elements.

💡 Best Practices

✅ Do

  • Test vertical resize
  • Use min- and max-height
  • Combine with min-width
  • Use rem or dvh units
  • Keep content scrollable

❌ Don’t

  • Confuse with height property
  • Rely on exact height only
  • Hide content on short screens
  • Ignore mobile landscape
  • Use width queries alone

Key Takeaways

Knowledge Unlocked

Five things to remember about height

Use these points when writing height-based responsive CSS.

5
Core concepts
02

max-height

Short screens.

Query
VP 03

Viewport

Window height.

Target
+W 04

and width

Both axes.

Combine
🌐 05

99% support

Universal.

Compat

❓ Frequently Asked Questions

The height media feature matches styles based on the viewport height — the visible height of the browser window. Use min-height, max-height, or an exact height value inside @media rules to adapt layouts when the screen is tall or short.
min-height applies styles when the viewport is at least the given height (tall screens). max-height applies styles when the viewport is at most the given height (short screens). Combine both to target specific height ranges.
The media feature queries the viewport height for conditional rules. The height property sets the height of an individual element. @media (min-height: 600px) checks the window; height: 600px sizes one box.
Use them for full-viewport heroes on tall screens, compact headers on short laptop windows, scrollable panels in landscape mobile, and layouts where vertical space matters more than width.
Yes. min-height, max-height, and height media features are supported in all modern browsers including Chrome, Firefox, Safari, Edge, and Opera.

Practice in the Live Editor

Open the HTML editor and experiment with @media (min-height: 600px) and short-screen layouts.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful