tags:

views:

84

answers:

3

A simple (one might think!) question to all CSS gurus: I would like to shrink a DIV snugly around an IMG. The IMG is 600 x 800 and I needed it much smaller. So I go {height: 100%; width: auto;} and constrain the height via a wrapper DIV. However, to maintain the (unknown to me) AR, I cannot fix the width on the DIV. I tried to set the wrapping DIV to "display: inline-block" or "float: left" or "position: absolute" or ... - no matter: most browsers will stretch that DIV 800px wide - the original width of the full-size IMG - so it looks sthg like this:

[[IMG].............................]

Bizarrely, I can add some text to the DIV (just to test), and that text will actually appear right next to the scaled IMG:

[[IMG]Hello world..................]

Does anyone here know why the original size of the IMG matters (for dimensioning the width, it does not affect the height)? And what I might be able to do to shrink the DIV?

Thanks for looking.

EDIT: To test Pär Wieslander's idea, I wrote a little test bed that should help clarify what I am on about:

<style type="text/css">  
  html, body {  
     height: 100%;  
  }  
  #dialog {  
     background: green;  
     height: 50%;  
     position: relative;  
  }  
  #frame {  
     border: 2px solid black;  
     display: inline-block;  
     height: 100%;  
     position: absolute;  
  }  
  #img {  
     height: 100%;  
     width: auto;  
  }  
</style>  

<body>  
  <div id="dialog">  
     <div id="frame">  
        <img id='img' src='...' />  
     </div>  
  </div>  
</body>  

Just pick any large IMG of your choice. You should find an inexplicably wide frame around and image that has squeezed - height-wise - onto the green carpet.

A: 

So I go height="50%", say, and width="auto" (to maintain AR).

Why not just go width="50%" too as this would resolve it.

Graphain
Thanks for the quick answer. I apologize - it's too early in the morning after a very late-night programming session :-( I corrected the OP. I am trying to make the whole composition flex with the wrapper DIV (which is resizable). In the true styling, the IMG is 100%!! in height of the DIV, and the width of the DIV must give to accommodate the AR - whatever that may be.
Ollie2893
+2  A: 

If you specify the image's width or height as a percentage, that percentage is calculated in proportion to the size of the parent block. So specifying width: 50% on the image doesn't mean 50% of the original image width -- it means 50% of the width of the parent block. The same goes for the height. Thus, there will always be extra space around the image as long as you specify the width or height as a percentage.

The solution is simple -- specify the dimensions in pixels, ems or any other unit other than a percentage:

HTML

<div class="wrapper">
  <img class="small" src="myimage.jpg">
</div>

CSS

img.small {
  width: 150px;    /* or whatever you like */
  display: block;  /* to avoid empty space below the image */
}

div.wrapper {
  display: inline-block;
  border: 1px solid #000;
}

Edit: Based on your comments and updated post, I understand that what you really want to do is to set the width of the surrounding div and make the image fill up that div. Here's an example that does that:

HTML

<div class="wrapper big">
  <img src="myimage.jpg">
</div>

<div class="wrapper small">
  <img src="myimage.jpg">
</div>

CSS

img {
  display: block;
  width: 100%;
}

.wrapper {
  margin-top: 1em;
  border: 1px solid #000;
}

.big {
  width: 600px;
}

.small {
  width: 300px;
}

Edit II: Here's a working example that illustrates the idea, based on the code you posted.

Pär Wieslander
Interesting thought. What I understand you saying is that rather than set the HEIGHT of the wrapper DIV, I should try to dimension its WIDTH. That constrains the WIDTH of the 100%-wide IMG, and I should let it finds its AR by setting { height: auto; } - the DIV will then NOT expand in height to the full height of the original IMG. It will take me a few minutes to test. I appreciate the idea.
Ollie2893
You could easily apply the same principle while setting the height of the div instead. In that case, you would need to set the height of the image to 100% and leave its width untouched, and use floating or `display: inline-block` to make sure the width of the div doesn't stretch beyond the width of the image.
Pär Wieslander
Sorry to be some dumb, Pär, how do you include HTML on this site? I am going mental. I tried your suggestion and it makes no difference. I would like to include my HTML in the original edit to facilitate our communication but I do not succeed to create these grey block quotes. I got 4 spaces, I tried <pre><code> ... none works. How do you do it?
Ollie2893
You need to indent your code **four** spaces -- parts of your code were indented only three spaces; that's why it didn't work. I've fixed the indentation now.
Pär Wieslander
I added a link to an example based on your code, that should work in browsers that support `display: inline-block`. It could easily be adjusted to work with floats instead, if necessary.
Pär Wieslander
I get what you are doing, Pär. I agree, if I COULD core inside-out, then your code works. HTML was designed to display content, and to wrap decorations around it. I yesterday wrote a crop editor (after having wasted days on the inadequacies of Jcrop and imgareaselect). And that editor is given a div to work in (resizable, in my case, for added entertainment) and I have to fill that container with the image. That's also why I chucked all that exciting positioning stuff into my example. My img will always be 100% of one dimension, and auto in the other.
Ollie2893
The positioning shouldn't matter -- I intentionally left it out to simplify the example. However, I think you should be able to work out a solution that works in your particular case based on the ideas I've outlined. For example, is it possible for you to specify the width of the container div instead of the height? Your code sample works fine using this approach, so I really think you should aim for leaving out the height and specifying the width instead.
Pär Wieslander
I agree. I have been playing around with your suggestion and it will indeed appear as though constraining the width does not lead to any spill in height.
Ollie2893
A: 

I think Pär's approach is right: don't do { height: fix; width: auto; } but do instead { height: auto; width: fix; } Works better.

Ollie2893
I'm glad to report that it's worked out beautifully, for the most part. I have Chrome, Opera and FF working. IE's still playing up but that's just par for the course (sigh)...
Ollie2893