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

  • Home
  • SEARCH
  • 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 might be simple one, since it's a design question mode than development (
I feel like the answer to this question is really simple, but I really
I'm new to Silverlight, so this might be a simple question to answer. I
I would like to create a simple file repository in Ruby on Rails. Users
The question might prompt some people to say a definitive YES or NO almost
The original question: The title of this question might be a bit clumsily phrased,
There might be some very simple answer to this, but i am really stuck
I would like to implement an API for my web based service. I've been
I'm putting together some demo pages, and one of the things I want to
I'm just starting to get into backbone.js. It looks like it's pretty involved and

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.