Understanding media queries

The developer Bruce Lee sums it up perfectly, when likening the effects of media queries to how water acts in different containers:

Empty your mind, be formless, shapeless—like water. Now you put water in a cup, it becomes the cup; you put water into a bottle it becomes the bottle; you put it in a teapot it becomes the teapot. Now water can flow or it can crash. Be water, my friend.

We can use media queries to apply different CSS styles, based on available screen estate or specific device characteristics. These might include, but not be limited to the type of display, screen resolution, or display density. Media queries work on the basis of testing to see if certain conditions are true, using this format:

   @media [not|only] [mediatype] and ([media feature]) {
       // CSS code;
    }

We can use a similar principle to determine if entire style sheets should be loaded, instead of individual queries:

<link rel="stylesheet" media="mediatype and|only|not (media feature)" href="myStyle.css"> 

Seems pretty simple, right? The great thing about media queries is that we don't need to download or install any additional software to use or create them; we can build most of them in the browser directly.

Exploring the available media types

If we take a look at the example query in the previous section, we see that part of it is made up of the type of media in use. This is just part of the query. All media queries come in two parts, with the first part controlling how our CSS will be displayed, with the second part taking care of when it is displayed.

Take a look at this example:

<!-- CSS media query on a link element --> 
<link rel="stylesheet" media="(max-width: 800px)" href="example.css" /> 
 
<!-- CSS media query within a stylesheet --> 
<style> 
@media (max-width: 757px) { 
  .sidebar { 
    display: none; 
  } 
} 
</style> 

This illustrates two ways of referencing media queries: the first example doesn't specify the media type, so we can infer it will be all by default; the second part of it states that it will only apply when the width of our window is at least 800px.

The second media query one doesn't specify the media type either, so again all will be inferred as the default; this time, the .sidebar element will not be displayed if the window width is less than 600px wide.

Let's see what all media types can be used:

Value

Used for

all

All types of media

print

Printers or printing content to media

screen

Screens/displays (such as computers, mobiles, or tablets)

speech

Screen readers

braille

Braille tactile feedback devices

embossed

Paged braille printers

handheld

Handheld devices, except smartphones and tablets, which use screen instead

print

Paged material and for documents viewed on screen in print preview mode

projection

Projecting presentations

screen

Color computer screens and smartphones

speech

Speech synthesizers

tty

Media using a fixed-pitch character grid (such as teletypes, terminals, or portable devices with limited display capabilities

In addition, to this, we have a number of features that we can use to control the trigger for displaying the content; it's worth getting to know them, as they all play an important role, but work in different ways. Let's take a look at the list in more detail and learn what each of them does in turn.

Listing the available media features

Adding a query that specifies the media target is only half the picture; we also need to give it something that controls when to display it! To achieve this, we can provide one or more conditions that must be satisfied in order for content to be styled or displayed.

Let's have a look at some of the options available:

Name of the attribute

Description

aspect-ratio

Viewport's width:height ratio

device-aspect-ratio

Device's width:height ratio

device-[height|width]

Height or width of device's screen

height

Viewport's height

min-device-pixel-ratio

Check device's pixel ratio. Good for high definition and retina devices (where the ratio is greater than 2)

[min|max]-aspect-ratio

Viewport's minimum or maximum width:height ratio

[min|max]-device-aspect-ratio

Device's minimum or maximum width:height ratio

[min|max]-device-height

Device's minimum or maximum height or width

[min|max]-width

Viewport's minimum or maximum width

orientation

Viewport's orientation in portrait or landscape mode

resolution

Device's resolution (in dpi or dpcm)

width

Viewport's width

The key here is selecting the right attribute against which we should run our test; a great example is checking orientation so that we can determine if a tablet is in portrait or landscape mode. We will make good use of some of these query tests later in this chapter.

Okay, time to move on. The theme so far has been what we can achieve by just using a browser and a text editor. This may seem limiting at first, but I maintain that too many people resort to using additional help (such as using jQuery) to create queries when it isn't always necessary to do so. In the main this will work, but there are some points that we should consider:

  • Do you need to support IE8 or below? If so, then we need to provide additional support for this; a decision by Microsoft in early 2016 though means that IE8 is no longer supported, so now is the time to really consider if this browser should feature in your designs.
  • Support for some of the newer media queries, such as max resolution, is only available in the newer browsers; it's worth checking the CanIUse.com site and your server logs to confirm if not supporting older browsers will be an issue.
  • If we need to provide fallback support, then don't automatically assume this means we must use a JavaScript-based solution; it's worth considering what support you need to offer, and whether you really need to use media queries (more on this anon).

Okay, let's move on: it's time to get physical, and stuck into something more practical! We've covered some of the different facets that make up queries and explored some of the considerations we need to allow for, when working with just a browser and text editor. Before we get too practical though, we need to cover one more topic. There are occasions when we need to test for multiple conditions. We can do this using logical operators. Let's take a quick look at some of these in more detail.

Introducing operators in our queries

When composing media queries that require multiple tests, we can make use of logical operators to confirm if one or more CSS rules should be applied to our content. If we use logical operators, we need to encapsulate each attribute that is tested in parentheses; otherwise, it will generate an error in our code.

Let's take a look at some of the more common examples of operators:

  • And: It is used to combine multiple media types or media features into a single complex query. It works normally like arithmetic and operators, that is, requires each condition to be true to execute the query.
  • Not: We can use this to negate a query. It applies on the whole media query and works only if the entire query would otherwise return false (such as min-width: 700px on a 600px display). This operator must be used with a media type.
  • Only: It is used to apply styles only when we need to prevent applying selected styles in old browsers.

Tip

Media queries are case sensitive, and will return false if unknown media types are encountered.

So far, we've talked about the basics of what a media query looks like: let's explore how we can use them to manage content by identifying where our designs break at different screen sizes. These breaks, or breakpoints,are what make media queries work – whilst it is clearly key that we get them right, it is also important to know when we should and should not use them...

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset