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.
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 types of media |
|
Printers or printing content to media |
|
Screens/displays (such as computers, mobiles, or tablets) |
|
Screen readers |
|
Braille tactile feedback devices |
|
Paged braille printers |
|
Handheld devices, except smartphones and tablets, which use |
|
Paged material and for documents viewed on screen in print preview mode |
|
Projecting presentations |
|
Color computer screens and smartphones |
|
Speech synthesizers |
|
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.
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 |
|
Viewport's width:height ratio |
|
Device's width:height ratio |
|
Height or width of device's screen |
|
Viewport's height |
|
Check device's pixel ratio. Good for high definition and retina devices (where the ratio is greater than 2) |
|
Viewport's minimum or maximum width:height ratio |
|
Device's minimum or maximum width:height ratio |
|
Device's minimum or maximum height or width |
|
Viewport's minimum or maximum width |
|
Viewport's orientation in portrait or landscape mode |
|
Device's resolution (in dpi or dpcm) |
|
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:
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.
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:
min-width: 700px
on a 600px
display). This operator must be used with a media type.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...