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
3 Essential Design Trends, November 2024
Touchable texture, distinct grids, and two-column designs are some of the most trending website design elements of…
20 Best New Websites, October 2024
Something we’re seeing more and more of is the ‘customizable’ site. Most often, this means a button to swap between…
Exciting New Tools for Designers, October 2024
We’ve got goodies for designers, developers, SEO-ers, content managers, and those of you who wear multiple hats. And,…
15 Best New Fonts, September 2024
Welcome to our roundup of the best new fonts we’ve found on the web in the previous four weeks. In this month’s edition…
By Simon Sterne
3 Essential Design Trends, October 2024
This article is brought to you by Constantino, a renowned company offering premium and affordable website design
You…
A Beginner’s Guide to Using BlueSky for Business Success
In today’s fast-paced digital world, businesses are always on the lookout for new ways to connect with their audience.…
By Louise North
The Importance of Title Tags: Tips and Tricks to Optimize for SEO
When it comes to on-page SEO, there’s one element that plays a pivotal role in both search engine rankings and user…
By Simon Sterne
20 Best New Websites, September 2024
We have a mixed bag for you with both minimalist and maximalist designs, and single pagers alongside much bigger, but…
Exciting New Tools for Designers, September 2024
This time around we are aiming to simplify life, with some light and fast analytics, an all-in-one productivity…
3 Essential Design Trends, September 2024
September's web design trends have a fun, fall feeling ... and we love it. See what's trending in website design this…
Crafting Personalized Experiences with AI
Picture this: You open Netflix, and it’s like the platform just knows what you’re in the mood for. Or maybe you’re…
By Simon Sterne
15 Best New Fonts, August 2024
Welcome to August’s roundup of the best fonts we’ve found over the last few weeks. 2024’s trend for flowing curves and…
By Ben Moss