1. Media queries
Media queries let you design different layouts for different media types like screen, print, TV, handheld devices etc. Depending on the media type, you can configure the style, the font and other elements of the page. Introduced in CSS3, media queries let a designer customize the presentation of the content to suit a specific range of output device types. They consist of a media type declaration and one or more media feature expressions that evaluate to true or false. The range of media types include handheld, screen, tv, print and projection. Media features include, but are not limited to device height, width, aspect ratio, resolution, color index as well as height, width of the browser window. Let’s take a basic example; imagine you want your text size to shrink on smaller devices, like phones. The query for that would look something like:p { font-size:1em; } @media all and (max-width:400px) { p { font-size:0.8em; } }Now, the font size of your paragraphs will be set to 1em, unless the browser width is less than 400px, in which case it will shrink to 0.8em. In my experience, the best way to design a responsive layout is to first create a default master style sheet with the main structural elements. Then you can add in media queries to customize elements based on the layout or other device elements. If it’s more than a handful of rules, it might be better to move them out to a separate child sheet — which you can selectively load later. Here’s how that would work:
<link rel="stylesheet" type="text/css" media="handheld and (orientation:landscape) and (max-device-width: 720px)" href="tablet_layout.css" />This piece of code checks whether the device is a handheld in landscape mode with device width less than 720px, and if so, it loads the tablet_layout.css stylesheet. Note that the features width, height, min/max-width, min/max-height all refer to the browser window’s width and height. The device width and height are controlled by the device-width, device-height, min/max- device-width, min/max- device-height. If you’d like to look up more examples using media queries MDN has a good round up.
2. Fluid grids
“Fluid grid” is a term used to describe a layout that is coded in relative proportions of the various elements, rather than with fixed pixel values. Traditional CSS layouts used fixed width grids to position the elements. The fixed-width approach no longer works today now that we have device screens ranging from 3 inches to a few feet. In a fluid grid design, everything is coded in terms of its relative proportion to the base element i.e. instead of specifying the height and width of each element; it is given a percentage or relative size. Imagine you have four columns. To make this fluid, instead of specifying each as 200 pixel wide, you’d have to specify them 0.2083 % (ie as 200/960). At least, that’s the principle. Creating an entirely fluid grid requires much care. It’s one of those rare cases I’d rather recommend using custom tools. Tiny Fluid Grid, Variable Grid System and Fluid Baseline Grid are some great alternatives to writing your own fluid grid from scratch.3. Flexible images
Images are deemed flexible when the image renders in native size, as long as the HTML container supports it, but when the browser window shrinks, the image scales to fit it. Even if the rest of the design is fluid and responsive, it’s not going to render well, if all components don’t scale. This can get tricky with images and videos. A 700 pixel wide image will look great on a desktop, but get chopped off when viewed on a 320 pixel tablet. Most modern responsive designs use the CSS max-width property to make images scale. (This method was first proposed by Richard Butler.)img { max-width: 100%; }These few lines will ensure that the image doesn’t get chopped off, by setting the max image width to 100% of the container. If the browser window or screen becomes smaller, this piece of CSS code will shrink the image proportionately. The height is automatically adjusted, while keeping the aspect ratio locked. You can instead, lock the image to occupy a certain vertical screen proportion like this
img { max-height: 75%; }(Make sure not to use both max-height and max-width properties together. ) The image size can also be linked to the text size instead of the parent element using em, like this:
img { width: 30em; }If the image contains text, you may want to limit the minimum/maximum size, to ensure that the text is readable. You can specify it like this:
img { width: 30em; max-width: 500px; min-width: 100px; }The same techniques can be used to scale videos, for example:
.video embed, .video object, .video iframe { width: 100%; height: auto; }
Final thoughts
Responsive design is the future of our industry, but with so many different techniques flying around, it’s often hard to keep up. Master these 3 simple techniques, and you’ll be 90% of the way to being a responsive designer. Featured image/thumbnail, elastic image via Shutterstock.Richa Jain
Read Next
LimeWire Developer APIs Herald a New Era of AI Integration
Generative AI is a fascinating technology. Far from the design killer some people feared, it is an empowering and…
By WDD Staff
20 Best New Websites, March 2024
Welcome to our pick of sites for March. This month’s collection tends towards the simple and clean, which goes to show…
Exciting New Tools for Designers, March 2024
The fast-paced world of design never stops turning, and staying ahead of the curve is essential for creatives. As…
Web Tech Trends to Watch in 2024 and Beyond
It hardly seems possible given the radical transformations we’ve seen over the last few decades, but the web design…
By Louise North
6 Best AI Productivity Apps in 2024
There’s no escaping it: if you want to be successful, you need to be productive. The more you work, the more you…
By Simon Sterne
3 Essential Design Trends, February 2024
From atypical typefaces to neutral colors to unusual user patterns, there are plenty of new website design trends to…
Surviving the Leap from College to Real-World Design
So, you’ve finished college and are ready to showcase your design skills to the world. This is a pivotal moment that…
By Simon Sterne
20 Mind-Bending Illusions That Will Make You Question Reality
Mind-bending videos. Divisive Images. Eye-straining visuals. This list of optical illusions has it all. Join us as we…
By Max Walton
15 Best New Fonts, February 2024
Welcome to February’s roundup of the best new fonts for designers. This month’s compilation includes some innovative…
By Ben Moss
The 10 Best WordPress Quiz Plugins in 2024
Whether it’s boosting your organic search visibility or collecting data for targeted email marketing campaigns, a great…
By Robert Reeve
20 Best New Websites, February 2024
It’s almost Valentine’s Day, so this latest collection is a billet-doux celebrating the best of the web this month.
Everything You Need to Know About Image Formats In 2024
Always trying to walk the tightrope between image quality and file size? Looking to branch out from JPGs and PNGs this…
By Robert Reeve