Click here to Skip to main content
14,382,347 members

Conserving Ratio and Serving Optimal Fluid Images in Responsive Design

Rate this:
5.00 (2 votes)
Please Sign up or sign in to vote.
5.00 (2 votes)
27 Jun 2013CPOL
How to conserve ratio and serve optimal fluid images in responsive design


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


  • 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.

	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){
		background-image: url('../img/retina.jpg');
/* Desktop */
@media screen and (min-width: 980px) and (max-width: 1024px){
		background-image: url('../img/desktop.jpg');
/* Tablet */
@media screen and (min-width: 760px) and (max-width: 980px){
		background-image: url('../img/tablet.jpg');
/* Mobile HD */
@media screen and (min-width: 350px) and (max-width: 760px){
		background-image: url('../img/mobile-hd.jpg');
/* Mobile LD */
@media screen and (max-width: 350px){
		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.


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


About the Author

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

Comments and Discussions

GeneralMy vote of 5 Pin
Prasad Khandekar27-Jun-13 21:51
professionalPrasad Khandekar27-Jun-13 21:51 
GeneralRe: My vote of 5 Pin
Yassin Souabni28-Jun-13 4:40
memberYassin Souabni28-Jun-13 4:40 
Questionnot an article. Pin
Dave Kreskowiak27-Jun-13 18:35
mveDave Kreskowiak27-Jun-13 18:35 
AnswerRe: not an article. Pin
Yassin Souabni27-Jun-13 19:01
memberYassin Souabni27-Jun-13 19:01 
GeneralMy vote of 5 Pin
Brian A Stephens29-May-13 5:19
professionalBrian A Stephens29-May-13 5:19 
GeneralRe: My vote of 5 Pin
Yassin Souabni29-May-13 8:45
memberYassin Souabni29-May-13 8: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.

Posted 25 May 2013


6 bookmarked