What the heck is a vertical media query?

We’ve all used media queries that look something like this:

@media screen and (max-width: 25em) {}

A vertical media query targets height instead of width:

@media screen and (max-height: 15em) {}

Why would you do that?

One reason is to keep above the fold content above the fold, even when the browser or device is very small. For example, if you have a call to action, or some other element that absolutely needs to be above the fold at all times.

Example

Let’s look at an example: http://cdpn.io/eAhFq (make the browser shorter/taller to see the media queries take effect)

I am using the vertical media queries to decrease padding and font-size, and even hide and reposition elements as the window gets shorter. This allows the call to action buttons to remain in view, even at a very short window height.

The Code

The first media query is at 20em (why ems?), and I’m decreasing the padding to keep all the important stuff in view:

@media screen and (max-height: 20em) {
  .hero {
    padding: .7em;
  }
}

After that, I’m progressively adding small tweaks:

@media screen and (max-height: 15em) {
  .hero {
    font-size: smaller;
  }
}

@media screen and (max-height: 13em) {
  .hero p, .hero .fake-image {
    display: none;
  }
}

@media screen and (max-height: 8em) {
  .hero h1 {
    float: left;
    margin-right: 2em;
  }
}

Here’s the full demo code:

See the Pen Vertical Media Queries by Scott Bolinger (@scottb) on CodePen

This is just one example, I know you smart guys and gals will come up with cool ways to use vertical media queries. If you do, feel free to leave them in the comments!

Further Reading:

http://trentwalton.com/2012/01/11/vertical-media-queries-wide-sites/
http://css-tricks.com/responsive-web-above-the-fold/

One thought on “Vertical Media Queries for Above the Fold Content

  1. The fold is a myth. Users are comfortable with scrolling and have been since the dawn of the www. The idea of “keeping everything important above the fold” is archaic imho.

Leave a Reply

Your email address will not be published. Required fields are marked *