53

I want to have a website where I can upload images of different sizes to be displayed in a jquery slider. I can't seem to fit (scaling down) the image in a containing div. Here's how I'm intending to do it

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title> 
<link rel="stylesheet" type="text/css" href="Imtest.css"/>
</head>

<body>

<div id="wrapper"> 

<div id="im"><img src="Images/Scarpa2_1.jpg" /></div>

</div>  
</body>
</html>

And the CSS

#wrapper {
    width: 400px;
    height: 400px;
    border: 2px black solid;
    margin: auto;
}

#im {
    max-width: 100%;
}

I've tried to set the max-width of my image to 100% in CSS. But that doens't work.

Volker E.
  • 5,911
  • 11
  • 47
  • 64
Muteking
  • 1,465
  • 5
  • 18
  • 31
  • try #im img{ display:block; height:100%; width:100%;} but small images may looks bad..! – Vivek Vikranth Sep 04 '13 at 06:14
  • 1
    Strangely that you say that setting `max-width` of the image to 100% doesn't help. [It works perfectly](http://jsfiddle.net/h6732/). – user Mar 19 '14 at 10:22
  • 1
    possible duplicate of [Image auto resize to fit div container](http://stackoverflow.com/questions/3029422/image-auto-resize-to-fit-div-container) – user Mar 19 '14 at 10:25

11 Answers11

62

You can use a background image to accomplish this;

From MDN - Background Size: Contain:

This keyword specifies that the background image should be scaled to be as large as possible while ensuring both its dimensions are less than or equal to the corresponding dimensions of the background positioning area.

Demo

CSS:

#im {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: url("path/to/img");
    background-repeat: no-repeat;
    background-size: contain;
}

HTML:

<div id="wrapper">
    <div id="im">
    </div>
</div>
dc5
  • 12,341
  • 2
  • 35
  • 47
19

This is an old question I know, but this is in the top five for several related Google searches. Here's the CSS-only solution without changing the images to background images:

width: auto;
height: auto;
max-width: MaxSize;
max-height: MaxSize;

"MaxSize" is a placeholder for whatever max-width and max-height you want to use, in pixels or percentage. auto will increase (or decrease) the width and height to occupy the space you specify with MaxSize. It will override any defaults for images you or the viewer's browser might have for images. I've found it's especially important on Android's Firefox. Pixels or percentages work for max size. With both the height and width set to auto, the aspect ratio of the original image will be retained.

If you want to fill the space entirely and don't mind the image being larger than its original size, change the two max-widths to min-width: 100% - this will make them completely occupy their space and maintain aspect ratio. You can see an example of this with a Twitter profile's background image.

CarenRose
  • 1,266
  • 1
  • 12
  • 24
Woody Payne
  • 595
  • 4
  • 15
8

if you want both width and the height you can try

 background-size: cover !important;

but this wont distort the image but fill the div.

Chathuranga
  • 1,008
  • 1
  • 15
  • 26
5

I believe this is the best way of doing it, I've tried it and it works very well.

#img {
  object-fit: cover;
}

This is where I found it. Good luck!

Josh Werner
  • 51
  • 1
  • 2
3

It's very simple. Just Set width of img to 100%

2

Hope this will answer the age old problem (Without using CSS background property)

Html

<div class="card-cont">
 <img src="demo.png" />
</div>

Css

.card-cont{
  width:100%;
  height:150px;
}

.card-cont img{
  max-width: 100%;
  min-width: 100%;
  min-height: 150px;
}
Moonis Abidi
  • 663
  • 10
  • 15
1

I am using this, both smaller and large images:

.product p.image {
text-align: center;
width: 220px;
height: 160px;
overflow: hidden;
}
.product p.image img{
max-width: 100%;
max-height: 100%;
}
Flavio
  • 41
  • 4
1

I have met the same issue and fixed using the following CSS. Hope this would help you.

max-width: 100%;
max-height: 100%;
width: max-content;
height: max-content;

.parent {
  border: 1px solid black;
  width: 200px;
  height: 100px;
}
.image {
  max-width: 100%;
  max-height: 100%;
  width: max-content;
  height: max-content;
  border: 1px solid black;
}
<div class='parent'>
  <img class='image' alt='' src="https://avatars.githubusercontent.com/u/95250818?v=4"></img>
</div>
Bikas Lin
  • 689
  • 5
  • 16
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ethan Jun 20 '22 at 23:49
0

I use:

object-fit: cover;
-o-object-fit: cover;

to place images in a container with a fixed height and width, this also works great for my sliders. It will however cut of parts of the image depending on it's.

0

In a webpage where I wanted a in image to scale with browser size change and remain at the top, next to a fixed div, all I had to do was use a single CSS line: overflow:hidden; and it did the trick. The image scales perfectly.

What is especially nice is that this is pure css and will work even if Javascript is turned off.

CSS:

#ImageContainerDiv {
  overflow: hidden;
}

HTML:

<div id="ImageContainerDiv">
  <a href="URL goes here" target="_blank">
    <img src="MapName.png" alt="Click to load map" />
  </a>
</div>
Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
0

Several of these things did not work for me... however, this did. Might help someone else in the future. Here is the CSS:

    .img-area {
     display: block;
     padding: 0px 0 0 0px;
     text-indent: 0;
     width: 100%;
     background-size: 100% 95%;
     background-repeat: no-repeat;
     background-image: url("https://yourimage.png");

    }
Blake
  • 375
  • 2
  • 6
  • 19