Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change img src in responsive designs?

Tags:

I'm about to code a responsive layout which will probably contain three different "states".

The quirky part is that much of the text, for example menu items will be images – not my idea and that's nothing i can change i'm afraid.

Since the images will differ slightly, other than size, for each state (for example in the smallest state the menu becomes floating buttons instead of a regular header), i will need to switch images instead of just scaling the same ones.

If it weren't for that i'd probably go with "adaptive-images.com".

So, i need some input on a best practice solution for this.

What i could think of:

  • Loading the images as backgrounds – feels a little bit filthy.

  • Insert both versions and toggle css display property – very filthy!

  • Write a javascript that sets all img links – feels a bit overkill?

Anyone sitting on a good solution? :)

like image 885
jonas Avatar asked May 03 '12 21:05

jonas


People also ask

How can I change responsive image?

To make an image responsive, you need to give a new value to its width property. Then the height of the image will adjust itself automatically. The important thing to know is that you should always use relative units for the width property like percentage, rather than absolute ones like pixels.

How do I make an image fit in a div responsive?

Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.

Which class will you use to create a responsive image that changes its width based on the browser width?

Create responsive images by adding an . img-responsive class to the <img> tag. The image will then scale nicely to the parent element.

How do I make an image text responsive in HTML?

You can make image responsive by using '%' like 100% or etc. But you can't do same with text to make it responsive. You need to use units like 'em' or 'rem' instead of '%' or 'px' to make text responsive. And btw 16px = 1em or 1rem.


1 Answers

Other option. Not need scripts, only CSS.

HTML

<div id="mydiv">     <img src="picture.png" class="image_full">     <img src="picture_mobile.png"  class="image_mobile"> </div> 

CSS

.image_full{    display:block;   }   .image_mobile{   display:none;  }  @media (max-width: 640px) and (min-width: 320px){   .image_full{    display:none;   }    .image_mobile{    display:block;   } } 
like image 184
Maximiliano Catani Avatar answered Nov 01 '22 17:11

Maximiliano Catani