Click here to Skip to main content
13,095,921 members (54,191 online)
Click here to Skip to main content
Add your own
alternative version

Stats

12.4K views
6 bookmarked
Posted 25 May 2013

Conserving Ratio and Serving Optimal Fluid Images in Responsive Design

, 27 Jun 2013
Rate this:
Please Sign up or sign in to vote.
How to conserve ratio and serve optimal fluid images in responsive design

Introduction

Maybe you have been faced with the problem of conserving ratio when trying to serve different images for different view-ports, using <div> tag you cannot conserve ratio, but I have found a solution that will make your life easier.

How Does It Work

Ingredients

  • Transparent PNG image with the desired ratio (transparent-ratio-conserver.png)
  • <IMG> tag
  • Different images for different view-ports (retina.jpg, desktop.jpg, tablet.jpg...)
The idea is to open an <img> tag and to assign to it a transparent image (with our desired ratio). We also add class="responsive-image" that's all in HTML.
<img src="img/transparent-ratio-conserver.png" class="responsive-image">

In the CSS, we set background-size to fit the <img> and we choose the width of our image.

.responsive-image{
	width: 100%;
	background-size: 100% 100%;
} 

and finally, we serve for every view-port the right image:

/* Retina display */
@media screen and (min-width: 1024px){
	.responsive-image{
		background-image: url('../img/retina.jpg');
	}
}
/* Desktop */
@media screen and (min-width: 980px) and (max-width: 1024px){
	.responsive-image{
		background-image: url('../img/desktop.jpg');
	}
}
/* Tablet */
@media screen and (min-width: 760px) and (max-width: 980px){
	.responsive-image{
		background-image: url('../img/tablet.jpg');
	}
}
/* Mobile HD */
@media screen and (min-width: 350px) and (max-width: 760px){
	.responsive-image{
		background-image: url('../img/mobile-hd.jpg');
	}
}
/* Mobile LD */
@media screen and (max-width: 350px){
	.responsive-image{
		background-image: url('../img/mobile-ld.jpg');
	}
} 

You can download the demo from here.

Points of Interest

Before that solution, we have to pass with complicated calculation to make <div> tag conserve certain ratio as described in the article: responsive-background-images-with-fixed-or-fluid-aspect-ratios.

Thank you for sending your feedback.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Yassin Souabni
Software Developer (Junior) Netzleiter
Germany Germany
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
Prasad Khandekar27-Jun-13 20:51
professionalPrasad Khandekar27-Jun-13 20:51 
GeneralRe: My vote of 5 Pin
Yassin Souabni28-Jun-13 3:40
memberYassin Souabni28-Jun-13 3:40 
Questionnot an article. Pin
Dave Kreskowiak27-Jun-13 17:35
mvpDave Kreskowiak27-Jun-13 17:35 
AnswerRe: not an article. Pin
Yassin Souabni27-Jun-13 18:01
memberYassin Souabni27-Jun-13 18:01 
GeneralMy vote of 5 Pin
Brian A Stephens29-May-13 4:19
professionalBrian A Stephens29-May-13 4:19 
GeneralRe: My vote of 5 Pin
Yassin Souabni29-May-13 7:45
memberYassin Souabni29-May-13 7:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 28 Jun 2013
Article Copyright 2013 by Yassin Souabni
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid