/    Sign up×
Community /Pin to ProfileBookmark

Image Resizing Media Queries

Hello, I’m just now joining the party for mobilizing websites … I’m late to the party I know!

I’m now familiar with how to apply styles based on the max and min width of the user’s screen:

[CODE]
@media screen and (min-width: 400px) and (max-width: 700px) { … }
[/CODE]

Let’s say I want to thin view something like this:

[CODE]
@media screen and (min-width: 960px){
#wrap {width: 960px; margin: 0 auto;}
}
@media screen and (max-width: 320px) {
#wrap {width: 100%;}
}
[/CODE]

I believe that would give me nice 960 layout for large screens, the it will fit the screen if the max width becomes under 320px. Now I want to get to my question ?. Let’s say I have a large image like this:

[CODE]
<div id=”wrap”>
<img src=”image.jpg” alt=”Image” width=”940″ height=”600″ />
</div>
[/CODE]

In my 960 layout, I wouldn’t need to do anything. In my smaller layout, would I modify the CSS to something like this?

[CODE]
@media screen and (max-width: 320px) {
#wrap {width: 100%;}
img {width: 100%;}
}
[/CODE]

This would make the image 100% of the viewport, which would be perfect (around 320px). Is this the best practice? I was always under the assumption that images should not be resized with CSS, you should create new images instead. If that’s the case, I would need to use javascript right? Something like this in jQuery?

[CODE]
$(document).ready(function(){
var windowWidth = $(window).width();
if(windowWidth <= 320){
$(‘img’).attr(‘src’, ‘image-small.jpg’).removeAttr(‘height’).removeAttr(‘width’);
}
});
[/CODE]

Basically replacing the image url with the path to the small one, and removing the height and width (because we’re using the CSS to set it to 100% wide.

Which is the route to go? Maybe even a different route? I’m looking for the best practice. Here are some examples of sites doing what I want:

[url]http://mediaqueri.es/[/url]

Thanks for any comments!

to post a comment
CSS

8 Comments(s)

Copy linkTweet thisAlerts:
@rockTheSkyApr 05.2011 — Definitely use separate images for smaller screens. I can't see why you'd want the width to be 100pc of the viewport (some gutter/margin/whitesipace at the sides even on small screens makes the design look neater).

You could try writing something in jQuery where it loads the different sized images (with the fixed dimensions) based on the screen width. For istance:

[CODE]if(windowWidth <= 400)
<load 320x480 img>
[/CODE]


and so on... (im not an expert with jQuery, sorry - I hope you understand what I mean.

If they're background images, I don't see why you'd need to manually set the width anyway. Just repeat it?
Copy linkTweet thisAlerts:
@cbVisionauthorApr 05.2011 — Thanks for the feedback. My gut told me that resizing the images isn't the way to go. I guess these are the positives and negatives of each approach:

CSS Media Queries:

- Loading a larger image when you don't need to

- Not compatible with IE

Javascript:

- Maintaining two image files for each blog post

- Javascript becomes required

I've got it working perfectly with media queries and renders great on portrait mode on iPhone and Android. It also renders down how I like it if you resize your Firefox or Chrome window to under 320px wide. You can see what I mean here:

http://www.danandwhitney.com/dan
Copy linkTweet thisAlerts:
@rockTheSkyApr 06.2011 — haha, that's cool man. Whitney is a very pretty young woman.

I noticed you list investing as one of your hobbies & activities. I personally invest in the stock market on a regular basis. I guess this discussion ought to be furthered somewhere else, but what sort of things do you like to invest in?
Copy linkTweet thisAlerts:
@cbVisionauthorApr 06.2011 — Thanks Rock, she's pretty indeed ?

I've been trying to become more active in the stock market since it's been down so low recently. I recently bought into Best Buy (BBY) and Cisco (CSCO) since they are pretty low right now; at least compared to they were in the past. I'm hoping they go back up before August though, because I'll need to sell some off when the wedding comes! Two of my other favorites are Huntington Bancshares (HBAN) and Sirius (SIRI) ... though I don't have anything in them now.

Since I'm young, I'm looking for high risk short term gain investments.

What about you?
Copy linkTweet thisAlerts:
@rebelweb2007Apr 13.2011 — I would make a class for the image and size with height and width property in CSS. This way you can size for each media query. You can do this in the header of the web page instead of doing it in the external stylesheet since this is the only page you will use it on. This is the easiest way to go and the user friendly.
Copy linkTweet thisAlerts:
@SpytimAug 24.2011 — This is an interesting discussion, I'm having the same issue.

The class idea is what I went with instinctively but it doesn't work. You see on my full screen webpage I have images that are various sizes, however on my smallest screen sizes (for phones) i am using tiles at 130 x 130. So long rectangular images can't be squeezed down into small squares... well they can be but it looks ridiculous.

Is there no way to do this with PHP? set the image name as a variable and us IF/THEN to determine screen size and action an appropriate image?
Copy linkTweet thisAlerts:
@SpytimAug 24.2011 — Hello, I'm just now joining the party for mobilizing websites ... I'm late to the party I know!

I'm now familiar with how to apply styles based on the max and min width of the user's screen:

[CODE]
@media screen and (min-width: 400px) and (max-width: 700px) { … }
[/CODE]


Let's say I want to thin view something like this:

[CODE]
@media screen and (min-width: 960px){
#wrap {width: 960px; margin: 0 auto;}
}
@media screen and (max-width: 320px) {
#wrap {width: 100%;}
}
[/CODE]


I believe that would give me nice 960 layout for large screens, the it will fit the screen if the max width becomes under 320px. Now I want to get to my question ?. Let's say I have a large image like this:

[CODE]
<div id="wrap">
<img src="image.jpg" alt="Image" width="940" height="600" />
</div>
[/CODE]


In my 960 layout, I wouldn't need to do anything. In my smaller layout, would I modify the CSS to something like this?

[CODE]
@media screen and (max-width: 320px) {
#wrap {width: 100%;}
img {width: 100%;}
}
[/CODE]


This would make the image 100% of the viewport, which would be perfect (around 320px). Is this the best practice? I was always under the assumption that images should not be resized with CSS, you should create new images instead. If that's the case, I would need to use javascript right? Something like this in jQuery?

[CODE]
$(document).ready(function(){
var windowWidth = $(window).width();
if(windowWidth <= 320){
$('img').attr('src', 'image-small.jpg').removeAttr('height').removeAttr('width');
}
});
[/CODE]


Basically replacing the image url with the path to the small one, and removing the height and width (because we're using the CSS to set it to 100% wide.

Which is the route to go? Maybe even a different route? I'm looking for the best practice. Here are some examples of sites doing what I want:

http://mediaqueri.es/

Thanks for any comments![/QUOTE]



ok that section of javascript is cool, but it changes ALL images on the page. How do you tie it to one instance of an <img src= >??

Thanks
Copy linkTweet thisAlerts:
@cbVisionauthorAug 25.2011 — This line targets all images:

[CODE]$('img').attr('src', 'image-small.jpg').removeAttr('height').removeAttr('width');[/CODE]

If you only want to target a specific images, you can give those images a certain class.

Example:

[CODE]<img src="image1.jpg" alt="Image" class="resize" />
<img src="image2.jpg" alt="Image 2" />

$('.resize').attr('src', 'image-small.jpg').removeAttr('height').removeAttr('width');[/CODE]


In this example, only the image with the class "resize" would be resized.
×

Success!

Help @cbVision spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...