12/1/2020 Responsive Design Example
Responsive design is an approach to web design that makes your web content adapt to the different screen and window sizes of a variety of devices. For example, your content might be separated into different columns on desktop screens, because they are wide enough to accommodate that design. Responsive Navigation. Navigation patterns in responsive web design can be tricky to tackle - especially while keeping a consistent look, feel, and experience on your site. Your website's navigation is how users get around on your website and discover all that you have to offer.
Chrome Dev Summit 2020 is back & going virtual on December 9-10. Learn more.
Responsive web design basics
The use of mobile devices to surf the web continues to grow at an astronomical pace, and these devices are often constrained by display size and require a different approach to how content is laid out on the screen.
Responsive web design, originally defined by Ethan Marcotte in A List Apart, responds to the needs of the users and the devices they're using. The layout changes based on the size and capabilities of the device. For example, on a phone users would see content shown in a single column view; a tablet might show the same content in two columns.
A multitude of different screen sizes exist across phones, 'phablets,' tablets, desktops, game consoles, TVs, and even wearables. Screen sizes are always changing, so it's important that your site can adapt to any screen size, today or in the future. In addition, devices have different features with which we interact with them. For example some of your visitors will be using a touchscreen. Modern responsive design considers all of these things to optimize the experience for everyone.
Set the viewport #
Pages optimized for a variety of devices must include a meta viewport tag in the head of the document.A meta viewport tag gives the browser instructions on how to control the page's dimensions and scaling.
To attempt to provide the best experience, mobile browsers renderthe page at a desktop screen width (usually about
980px , though this variesacross devices), and then try to make the content look better by increasingfont sizes and scaling the content to fit the screen.This means that font sizes may appear inconsistent to users,who may have to double-tap or pinch-to-zoomin order to see and interact with the content.
Using the meta viewport value
width=device-width instructs the page to matchthe screen's width in device-independent pixels. A device (or density) independent pixel being a representation of a single pixel, which may on a high density screen consist of many physical pixels. This allows the page to reflowcontent to match different screen sizes, whether rendered on a small mobilephone or a large desktop monitor.
Some browserskeep the page's width constant when rotating to landscapemode, and zoom rather than reflow to fill the screen. Adding the value
initial-scale=1 instructs browsers to establish a 1:1 relationship between CSSpixels and device-independent pixels regardless of device orientation, andallows the page to take advantage of the full landscape width.
Caution:To ensure that older browsers can properly parse the attributes,use a comma to separate attributes.
The Does not have a
<meta name='viewport'> tag with width or initial-scale Lighthouse audit can help you automate the process of making sure that your HTML documents are using the viewport meta tag correctly.
Ensure an accessible viewport #
In addition to setting an
initial-scale ,you can also set the following attributes on the viewport:
When set, these can disable the user's ability to zoom the viewport,potentially causing accessibility issues.Therefore we would not recommend using these attributes.
Size content to the viewport #
On both desktop and mobile devices,users are used to scrolling websites vertically but not horizontally;forcing the user to scroll horizontally or to zoom outin order to see the whole page results in a poor user experience.
When developing a mobile site with a meta viewport tag,it's easy to accidentally create page content that doesn't quite fit within the specified viewport.For example, an image that is displayed at a width wider than the viewportcan cause the viewport to scroll horizontally.You should adjust this content to fit within the width of the viewport,so that the user does not need to scroll horizontally.
The Content is not sized correctly for the viewportLighthouse audit can help you automate the process of detecting overflowing content.
Images #
An image has fixed dimensions and if it is larger than the viewport will cause a scrollbar.A common way to deal with this problem is to give all images a
max-width of 100% .This will cause the image to shrink to fit the space it has,should the viewport size be smaller than the image.However because the max-width , rather than the width is 100% ,the image will not stretch larger than its natural size.It is generally safe to add the following to your stylesheetso that you will never have a problem with images causing a scrollbar.
Add the dimensions of the image to the img element #
When using
max-width: 100% you are overriding the natural dimensions of the image,however you should still use the width and height attributes on your <img> tag.This is because modern browsers will use this information to reserve space for the imagebefore it loads in,this will help to avoid layout shifts as content loads.
Layout #
Since screen dimensions and width in CSS pixels vary widely between devices(for example, between phones and tablets, and even between different phones),content should not rely on a particular viewport width to render well.
In the past, this required setting elements used to create layout in percentages.In the example below, you can see a two-column layout with floated elements, sized using pixels.Once the viewport becomes smaller than the total width of the columns, we have to scroll horizontallyto see the content.
By using percentages for the widths, the columns always remain a certain percentage of the container.This means that the columns become narrower, rather than creating a scrollbar.
Modern CSS layout techniques such as Flexbox, Grid Layout, and Multicolmake the creation of these flexible grids much easier.
Flexbox #
This layout method is ideal when you have a set of items of different sizesand you would like them to fit comfortably in a row or rows,with smaller items taking less space and larger ones getting more space.
In a responsive design, you can use Flexbox to display items as a single row,or wrapped onto multiple rows as the available space decreases.
Read more about Flexbox.
CSS Grid Layout #
CSS Grid Layout allows for the straightforward creation of flexible grids.If we consider the earlier floated example,rather than creating our columns with percentages,we could use grid layout and the
fr unit,which represents a portion of the available space in the container.
Grid can also be used to create regular grid layouts,with as many items as will fit.The number of available tracks will be reduced as the screen size shrinks.In the below demo, we have as many cards as will fit on each row,with a minimum size of
200px .
Multiple-column layout #
For some types of layout you can use Multiple-column Layout (Multicol),which can create responsive numbers of columns with the
column-width property.In the demo below, you can see that columns are added if there is room for another 200px column.
Use CSS media queries for responsiveness #
Sometimes you will need to make more extensive changes to your layoutto support a certain screen size than the techniques shown above will allow.This is where media queries become useful.
Media queries are simple filters that can be applied to CSS styles.They make it easy to change styles based on the types of device rendering the content,or the features of that device, for example width, height, orientation, ability to hover,and whether the device is being used as a touchscreen.
To provide different styles for printing,you need to target a type of output so you could include a stylesheet with print styles as follows:
Responsive Website Design Examples
Alternatively, you could include print styles within your main stylesheet using a media query:
It is also possible to include separate stylesheets in your main CSS file using the
@import syntax,@import url(print.css) print; , however this use is not recommended for performance reasons.See Avoid CSS imports for more details.
For responsive web design, we are typically querying the features of the devicein order to provide a different layout for smaller screens,or when we detect that our visitor is using a touchscreen. Lets golf 2 1 0 5.
Media queries based on viewport size #
Media queries enable us to create a responsive experiencewhere specific styles are applied to small screens, large screens, and anywhere in between.The feature we are detecting here is therefore screen size,and we can test for the following things.
All of these features have excellent browser support,for more details including browser support information seewidth,height,orientation, andaspect-ratio on MDN.
The specification did include tests for
device-width and device-height . These have been deprecated and should be avoided.device-width and device-height tested for the actual size of the device window which was not useful in practice becausethis may be different from the viewport the user is looking at,for example if they have resized the browser window.
Media queries based on device capability #
Given the range of devices available, we cannot make the assumption that every large device is a regular desktopor laptop computer, or that people are only using a touchscreen on a small device.With some newer additions to the media queries specificationwe can test for features such as the type of pointer used to interact with the deviceand whether the user can hover over elements.
Try viewing this demo on different devices,such as a regular desktop computer and a phone or tablet.
These newer features have good support in all modern browsers. Find out more on the MDN pages forhover,any-hover,pointer,any-pointer.
Using
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |