Creating Responsive Images with CSS - Six Revisions |
Creating Responsive Images with CSS Posted: 17 Dec 2014 02:00 AM PST In this tutorial, you will learn the simplest technique for achieving responsive images.
There are many responsive image techniques, and they vary in complexity and level of browser support. An example of a complicated way of implementing responsive images is using the In contrast, the method covered in this tutorial is simple and relies only on the CSS You’ll get to see several variations of the responsive image technique discussed in this tutorial, but the foundational concept is the same: Using CSS, give images a percentage-length unit (or any relative-length unit, e.g. Basic Responsive ImageLet’s start with a basic example. We have a HTML The container has a The CSS div.container { width: 96%; max-width: 960px; margin: 0 auto; /* to center the container */ } img { width: 100%; height: auto; } Note that the Responsive Images in ColumnsSometimes we want images to display side-by-side in columns. For example, image galleries often display thumbnails on a grid. To achieve responsive images in columns, the only change we have to do is lower the CSS Let’s discuss a couple of layout schemes: A two-column image layout and a three-column image layout. Two-column Responsive Image LayoutFor a two-column responsive image layout, we can set the CSS HTML CSS Three-column Responsive Image LayoutFor a three-column responsive image layout, the concept is the same, we just need to set the HTML CSS Images with Conditional BreakpointsIt’s a good idea to have conditional breakpoints for responsive images that are displayed in columns so that when the images get too small, the columns will collapse. We’ll need media queries to make this happen. In the following example, the images will display in one column on smartphones, two columns on tablets, and four columns on larger screens. HTML CSS Note: It’s a good idea to set the appropriate media queries for each design you work on. In the demo page, the media query parameters that were chosen worked well for the layout, but they might not work so well in other designs. Full-width Responsive ImageTo have responsive images that are always 100% of the viewport’s size, we just need to remove the container’s CaveatEven though this responsive image technique is easy to use and has good browser support, its downside is that it will always serve the full size of the images. This means large, high-res images will still be served to the user even if he/she is on a small mobile device that can’t take advantage of the full dimension and resolution of the images. If you’d like to conditionally serve different versions of the same image (e.g. smaller sizes for mobile devices and larger versions for desktops) to improve mobile web performance, check out the srcset attribute and/or the picture element. If you would like a polyfill to be able to support today’s browsers, see Scott Jehl’s Picturefill. Related Content
About the AuthorThe post Creating Responsive Images with CSS appeared first on Six Revisions. |
You are subscribed to email updates from Six Revisions To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 1600 Amphitheatre Parkway, Mountain View, CA 94043, United States |
No comments:
Post a Comment