With so many devices available in the market today, It goes without saying that designers should think responsiveness. Building sites that can be accessed across devices cannot be stressed enough. It is a good practice to build mobile-first responsive sites then adjust for bigger screens.
Viewport defines the area of the screen that the browser can render content to.To achieve responsiveness, we include meta viewport in the head section of the document
<meta name="viewport" content="width=device-width, initial-scale=1">
Breakdown of the
meta name="viewport"instructs the browser how to controls the width and scaling.
width = device-widthsets the width of the page to follow the screen-width of the device
initial-scale = 1to establish the 1:1 relationship between CSS pixels and device independent pixels.
Media Queries are definitely buddies when talking responsiveness. They enable designers to define breakpoints: The point at which the site adapts according to the best possible layout. Media Queries can be used to check the width and height of the viewport, device width and height, resolution and screen orientation.
Ways to apply media queries
- Linked css
A separate stylesheet is ideal if there is a need to completely separate different styles for different devices such as desktop, laptop.
Linked stylesheet makes many but small http requests.
They are referenced as
- Embedded with an @import tag.
@import rule is expensive and should be avoided for performance reasons.It can prevent stylesheets from being downloaded concurrently negatively impacting performance.However, the rule can be beneficial if a stylesheet depends on another.
This rule should be used sparingly.
import url("mail.css") only screen and (min-width:500px);
- Embeded with @media tag
Many different devices will be available in the market over a period of time so Avoid defining breakpoints based on devices for obvious reasons.Who knows which new device will be in the market tomorrow. It is good practice to find breakpoints using your content as a guide. Furthermore, the content is the important stuff and the design should display the content right?
Width vs device-width
Understanding the difference between media queries based on width and device-width is vital.width is the size of the browser window while device-width is the device actual screen size. In other words, the screen resolution of the device
It is not a good idea to use device-width because:
- They don't always match the layout viewport.C a prevent content from adapting on desktops or other devices that allow the window to be resized and this is because the query is based on the actual device size and not the browser window.
- Some browsers such as Legacy Android browser may report the wrong value
My thoughts on good design Practices.
- Use @import rule sparingly, it can prevent your stylesheets from downloading concurrently
- Device-Width dot always match the layout viewport
- Don't base your breakpoints on device size instead prioritize content
- @media rule makes few big http requests