What is a Media Query?

With the introduction of tablets and phones accessing the internet, there is a wide variety of screen sizes and resolutions to account for when developing a web site. For many years, organizations created multiple versions of their web sites (one for desktops and one for mobile). Unfortunately, this required that changes be made in two places, which is not the most efficient way to update a web page. More recently, the concept of responsive design has taken over and web developers can now query the media device to get information that will help them cater the styling/layout of a page or site to match the screen size of the device accessing it.

Step one in responsive design is to control the viewport and map the pixels of a site to the mobile device. This can be done in the following manner:
<meta name='viewport' content='width=device-width, initial-scale=1'>

In addition, sizing/placing elements relatively will avoid placements and widths that are too large for a smaller screen.

To control specific elements within a site or page, we can use media queries to control which styles are applied to an element. Media queries come in two flavors: Media Types and Media Features. There are four options for media types: all, print, screen, speech. Most pages are developed for screen and we must use the Features to customize the output for the screen size. There are many more options in terms of features including: width, resolution, orientation, and many others.

Media types are written simply as the word. In other words, if we wanted to specify a set of styles for print, we would use
media='print'
. In the case of setting styles for printing, typically the background is set to white, the foreground to black, and pictures are often hidden (display:none). This allows users to print the content of a web site without wasting all of their ink AND without the developer having to make a second version of the page with desired printing styles. The default media type is all and need not be written.

Media features are tests that check the device that the user is accessing the page from. A media feature is a name:value pair, similar to a style, that is tested against the viewing device and a value of true or false is returned. Media features are written inside of parenthesis. For example, to use a certain set of styles when the device screen is less than 480px, we would write
media='(max-width:480px)'
. Below is a list of common media features:



Compound Media Queries

Often, programmers want to set a series of restrictions on their media queries. For example, if we develop a stylesheet for tablets, we want to look at a range of widths from just bigger than a smartphone to just smaller than a laptop. Other times, we may want to have a set of styles that occur if one of several conditions are true. In these cases, we can use logical operators to build compound conditions.



Applying Media Queries

Media queries can be applied to load an entire stylesheet
<link rel='stylesheet' media='print' href='stylesheet.css' />

or to control individual styles within a style sheet.
@media (min-width:480px) and (max-width: 620px){
	//css style pairs
}



A Discussion on Responsive Design

Developers want their site to look great everywhere - on phones, tablets, laptops and computers. However, since each device has unique measurements (even the iphone 4, 5 and 6 have different dimensions), this can become time consuming. Over time a list of 'generic' breakpoints have been created and can be seen below.


Unfortunately, as technology progresses, these marks will have to change. As this type of development has progressed, the newer thinking is to build effectively by building up. In other words, start with the smallest version of the site - the mobile version. Instead of using pre-determined breakpoints, add break points where your site 'breaks'. This means that your design will look as you desire on any device as the layout will be forced to change whenever the layout starts to look bad.

A more recent movement is to work in relative sizes (em) instead of absolute sizes (px). This movement focuses on content as a change in em means a change in text size. This idea fixes the zooming issue. The standard view (1em) is typically 16px. But if a user zooms in, then 1em could be equal to 20px. By controlling the display text size based on relative sizes, we keep the focus on the content. To calculate, if your design 'breaks' at 400px, divide that value by 16 to get the corresponding em value (25 in this case) and set that as the max-width. In that style set, be sure to set an appropriate text size. As the breaks move up, continue to increase the text size appropriately.