I have a user uploaded image inside of a div with a fixed width and height. The image inside is aligned to the center vertically. Works fine.
The problem is that sometimes, the height of the image is smaller than the height of the div.
See this jsfiddle for example.
How do I make the image always fit the height of the div and maintain its aspect ratio?
HTML:
<div class="container">
<div class="b_feat_img">
<img src="http://i.imgur.com/iEJWyXN.jpg">
</div>
<div class="b_feat_img">
<img src="http://i.imgur.com/qRkEJni.jpg">
</div>
</div>
CSS:
.container {
width:120px;
}
.b_feat_img {
border: 1px solid green;
background:red;
float: left;
height: 96px;
margin-bottom: 10px;
overflow: hidden;
width: 100%;
}
.b_feat_img img {
height: auto;
position: relative;
top: 50%;
transform: translate(0px, -50%);
width: 100%;
}
In supported browsers*. You can use object-fit with value of cover, it works similarly to background-size:cover, but for inline images.
.b_feat_img img {
object-fit: cover;
height: 100%;
width: 100%;
}
.container {
width: 120px;
}
.b_feat_img {
border: 1px solid green;
background: red;
float: left;
height: 96px;
margin-bottom: 10px;
overflow: hidden;
width: 100%;
}
.b_feat_img img {
object-fit: cover;
height: 100%;
width: 100%;
}
<div class="container">
<div class="b_feat_img">
<img src="http://i.imgur.com/iEJWyXN.jpg">
</div>
<div class="b_feat_img">
<img src="http://i.imgur.com/qRkEJni.jpg">
</div>
</div>
* Note, at the time of writing, IE doesn't support it, be sure to see the support tables.
Related
Building an image slideshow, I have a container div of arbitrary size and aspect ratio into which I have to best fit an image, centred, with a caption overlayed at the bottom of the image and fitting its width. My best solution to date is to contain the image and the caption in a child element of the container but I'm having trouble centring it. This should be such a simple thing that I can't believe it's not staring me in the face but I can't see it. The code below uses a portrait format image but I need it to handle landscape also. I'm using React so jQuery is out.
.container {
position: relative;
width: 80%;
height: 48vw;
/* 4:3 */
margin: 0 auto;
display: flex;
justify-content: center;
}
.img-wrap {
background-color: #efe;
}
img {
position: absolute;
max-width: 100%;
max-height: 100%;
}
.caption {
position: absolute;
bottom: 0;
color: #fff;
background-color: rgba(153, 153, 153, 0.541)
}
<div class="container">
<div class="img-wrap">
<img src="https://png.pngtree.com/thumb_back/fw800/background/20190223/ourmid/pngtree-full-aesthetic-aurora-night-sky-background-skystarry-skystarnight-viewbackgroundstar-image_87582.jpg" height="1600px">
<div class="caption">Caption Content</div>
</div>
</div>
Update your code like below:
.container {
width: 80%;
height: 48vw;
/* 4:3 */
margin: 5px auto;
text-align: center;
}
.img-wrap {
background-color: #efe;
height: 100%;
display: inline-block;
position: relative;
}
img {
max-width: 100%;
height: 100%;
display: block;
object-fit: contain; /*or cover if you want to cover all the area*/
object-position: bottom;
}
.caption {
position: absolute;
left: 0;
right: 0;
bottom: 0;
color: #fff;
background-color: rgba(153, 153, 153, 0.541)
}
<div class="container">
<div class="img-wrap">
<img src="https://i.picsum.photos/id/10/400/600.jpg">
<div class="caption">Caption Content</div>
</div>
</div>
<div class="container">
<div class="img-wrap">
<img src="https://i.picsum.photos/id/10/600/300.jpg">
<div class="caption">Caption Content</div>
</div>
</div>
don't position absolute the image,
also if the caption is the sibling of the image,
set the size of the parent and set the image as 100% if the parent's width and height
then you can simply use the text-align: center on the caption to center it.
edit :
keep the existing style of a caption for positioning
fiddle : https://jsfiddle.net/hellooutlook/6ep4Lofz/4/
I have an image inside a flexible div and I want the image to stay the same size no matter if the div is being resized, the image should just be hidden (centering it would also be a great add-on). How to achieve this?
Below is my current code, this causes the image to shrink when the div is being resized. Thank you!
.banner-img {
max-width: 30%;
float: left;
overflow: hidden;
border: 1px solid red;
}
.banner-img img {
width: 250px;
height: 100px;
}
<div class="banner-img"><img src="http://via.placeholder.com/250x100" alt="" /></div>
Flex is a good solution to center your image
.banner-img {
max-width: 30%;
height: auto;
overflow: hidden;
display: flex;
justify-content: center;
border: 1px solid red;
}
.banner-img img {
width: 250px;
height: auto;
}
<div class="banner-img"><img src="http://via.placeholder.com/250x100" alt="" /></div>
You can achive this with making an image as background and align the background position to center:
.banner-img {
width: 250px;
max-width: 30%;
height: 100px;
background: url("http://via.placeholder.com/250x100") center no-repeat;
}
<div class="banner-img"></div>
In .banner-img img, you can add object-fit: cover and change the width: 250px from pixels to percentage, like this:
.banner-img {
max-width:30%;
float:left;
overflow:hidden;
}
.banner-img img {
width: 100%;
height: 100px;
object-fit: cover;
}
<div class="banner-img"><img src="http://via.placeholder.com/250x100" alt="" /></div>
My issue is that the child image element must fit to the parent div, but the image's height and width are dynamically set.
.people-image-right-parent { height: 150px; width: 150px;background: #EAEAEA; overflow: auto; }
.people-image-right{ width: 220px;
object-fit: contain;
height: 220px;
background: url(http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png) -491px -235px;}
<div class="people-image-right-parent">
<img class="people-image-right" />
</div>
The image's height and width are dynamic, not fixed, and it should be in pixels (px). I don't want the image to be scrollable or hidden. It must fit to parent element or must be contained in the parent element.
Please guide me on how to fit the image to its parent div.
Thanks in advance.
try this one
.people-image-right-parent {
height: 150px;
width: 150px;
background: #EAEAEA;
overflow: hidden;
}
.people-image-right {
width: 100%;
object-fit: contain;
height: 100%;
background: url(http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png) -491px -235px;
}
<div class="people-image-right-parent">
<img class="people-image-right" class="" />
</div>
Is this what you were looking for?
.people-image-right-parent {
height: 150px;
width: 150px;
background: #EAEAEA;
overflow: hidden; /* If needed you can change this
value to "scroll" or "visible". */
}
.people-image-right{
height: inherit; /* If needed you can change these */
width: inherit; /* values to pixel values. */
object-fit: cover;
margin: 0;
}
<div class="people-image-right-parent">
<img class="people-image-right" src="http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png" />
</div>
Here try with this
If your image is lower(in PX) than your div,you have to use min-width:100% & min-height:100%;
If your image is higher(in PX) than your div,you have to use max-width:100% & max-height:100%;
Here is the example, Your image is higher or lower than the parent div it should fit the parent div.
.people-image-right-parent {
height: 150px;
width: 150px;
background: #EAEAEA;
}
.people-image-right{
height: 350px;
width: 345px;
max-width:100%;
max-height:100%;
min-width:100%;
min-height:100%;
margin: 0;
}
<div class="people-image-right-parent">
<img class="people-image-right" src="http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png" />
</div>
I don't know if modifying the html is an option but...
You are much better of using an img tag inside the parent div
In the example below, the images are completely dynamic and will adjust to fit the div no matter what the aspect ratio is. it will also never stretch the image.
.people-image-right-parent {
display: inline-block;
width: 150px;
height: 150px;
background: darkred;
line-height: 150px; /* should match your div height */
text-align: center;
font-size: 0;
}
img.people-image-right {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
}
<div class="people-image-right-parent">
<img class="people-image-right" src="https://unsplash.it/300/300">
</div>
<div class="people-image-right-parent">
<img class="people-image-right" src="https://unsplash.it/100/300">
</div>
<div class="people-image-right-parent">
<img class="people-image-right" src="https://unsplash.it/300/100">
</div>
Edit:
If you can set the height and width of the img inline - even if it's generated dynamically - but cannot control anything else, see the example below. This will work as long as the width and height are declared in px in the img tag whether it's done manually or dynamically.
.people-image-right-parent {
display: inline-block;
width: 150px;
height: 150px;
background: darkred;
line-height: 150px; /* should match your div height */
text-align: center;
font-size: 0;
}
/* demonstration backgrounds */
.bg1 {background: url(https://unsplash.it/500/400);}
.bg2 {background: url(https://unsplash.it/200/600);}
.bg3 {background: url(https://unsplash.it/900/300);}
.people-image-right {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
background-size: contain;
background-position: center;
background-repeat:no-repeat;
}
<div class="people-image-right-parent">
<img class="people-image-right bg1" style="width:300px ;height:300px">
</div>
<div class="people-image-right-parent">
<img class="people-image-right bg2" style="width:100px ;height:300px">
</div>
<div class="people-image-right-parent">
<img class="people-image-right bg3" style="width:300px ;height:100px">
</div>
Im sure this is really easy but i have been looking at this issue for a little while and my brain has gone blank. I have a div that then has an image inside of it. The image seems to just overflow the div border and its driving me mad. I have an image below to show you what is happening along with the css.
#avatar {
float: left;
width: 50%;
height: 300px;
border: 1px solid black;
}
#avatar img {
width: 100%;
height: auto;
}
<div id="avatar">
<img src="http://i.imgur.com/dkzoNCc.png"></div>
I have a border on the main div #avatar just so i can see the whole size of the div. All i want is for the image to scale to the size of the div. If i set the height to 100% it goes into the div just fine but when resizing it it starts to overflow the div. I want the image to resize on the width not height.
Am i missing something really simple here? I dont want to use overflow hidden on the image as that will just cut some of it off i believe.
Thanks everyone
Try below css for img.
use height: 100%; for maximum height
display: block;margin: auto; for centering
max-width: 100%; to fit large images
Please check the example with large and small images.
#avatar {
float: left;
width: 50%;
height: 300px;
border: 1px solid black;
}
#avatar img {
height: 100%;
max-width: 100%;
display: block;
margin: auto;
}
<div id="avatar">
<img src="http://www.baraodasfestas.com.br/Assets/Produtos/SuperZoom/0431_MICKEY_635703672330071491.jpg" alt="">
</div>
<div id="avatar">
<img src="https://upload.wikimedia.org/wikipedia/en/d/d4/Mickey_Mouse.png" alt="">
</div>
Just add:
#avatar img {
max-width: 100%;
}
#avatar {
float: left;
width: 50%;
height: 300px;
border: 1px solid black;
}
#avatar img {
/*width: 100%;*/
height: auto;
max-width: 100%;
height:100%;
}
<div id="avatar">
<img src="http://i.imgur.com/dkzoNCc.png"></div>
It's because of your height:auto for the <img>
Just use :
#avatar img
{
width: 100%;
height: 100%;
}
But this will stretch you image. So if you want full size image inside your container you need to stretch your container instead. Like
#avatar
{
float: left;
display: inline-block;
width: 50%;
height: auto;
border: 1px solid black;
}
#avatar img
{
width: 100%;
height: 100%;
}
Is it possible to get a similar effect like background-size: contain with an img tag?
I want to display product pictures inside a fixed width/height div container, that have various dimensions (square, portrait, landscape). The pictures should
Always display the whole image, nothing should be cropped
Be aligned horizontally and vertically centered
This is, what background-size: contain does. Unfortunately I have to use an img tag (vor various reasons, going from the framework I use to SEO stuff).
In this plnkr you can see the problems and how it should look like (using background-size) http://plnkr.co/edit/k9Nv4ELoZgYCaQfVuSDQ?p=preview
Looks good
Should be centered vertically
Is cropped, but should display 100% of its height
CSS:
.product {
background-color: green;
border: 1px solid blue;
width: 200px;
height: 200px;
overflow: hidden;
margin-bottom: 20px;
}
.product img {
width: 100%;
}
Note: Somehow I can't insert HTML tags, it displays the images not the
source code. Please have a look into the plnkr.
EDIT:
This should work with CSS only, no JS.
Support for all modern browsers, including IE 10
You can do it like this by setting position: relative to the .product and position: absolute with other formatting to the .product img. Please check the fiddle below.
.product-css {
background-color: red;
border: 1px solid blue;
width: 200px;
height: 200px;
background-repeat: no-repeat;
background-size: contain;
background-position: center;
margin-bottom: 20px;
}
.product {
background-color: green;
border: 1px solid blue;
width: 200px;
height: 200px;
overflow: hidden;
margin-bottom: 20px;
position: relative;
}
.product img {
max-width: 100%;
max-height: 100%;
position: absolute;
top: -100%;
bottom: -100%;
left: -100%;
right: -100%;
margin: auto;
}
<h2>With img tag</h2> 1.
<div class="product">
<img src="http://keentype.com/post-images/wineBottles/vine-bottles-post.jpg">
</div>
2.
<div class="product">
<img src="https://0.s3.envato.com/files/149175458/wine_bottle_mockup_05.jpg">
</div>
3.
<div class="product">
<img src="http://www.designer-daily.com/wp-content/uploads/2009/02/boxhead-wine.jpg">
</div>
<hr>
<h2>Desired behaviour (with background-image)</h2>
<div class="product-css" style="background-image: url(http://keentype.com/post-images/wineBottles/vine-bottles-post.jpg)">
</div>
<div class="product-css" style="background-image: url(https://0.s3.envato.com/files/149175458/wine_bottle_mockup_05.jpg)">
</div>
<div class="product-css" style="background-image: url(http://www.designer-daily.com/wp-content/uploads/2009/02/boxhead-wine.jpg)">
</div>
Try to add this css:
.product img {
max-width: 200px;
max-height:200px;
width:auto;
height:auto;
margin: 0 auto;
display: block;
position: relative;
top: 50%;
transform: translateY(-50%);
}