Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 903377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:52:05+00:00 2026-05-15T15:52:05+00:00

A simple (one might think!) question to all CSS gurus: I would like to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:52:06+00:00Added an answer on May 15, 2026 at 3:52 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should hopefully be a simple one. I would like to add an extension
This may be simple one, but 5 mins of Googling didn't give me the
Probably a really simple one this - I'm starting out with C# and need
This is a simple one. I want to replace a sub-string with another sub-string
Hopefully this is a simple one, but can anyone provide some simple c# code
Hopefully a nice simple one. I've got a php3 website that I want to
Im sure this will be a simple one but have a project that started
We have an app that uses simple one way binding with a GridView to
I'm trying to figure out the best way to save a simple one-to-many relationship
One simple method I've used in the past is basically just creating a second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.