I have a 240*240px image inside a 100*300px div (demo values, actual values vary and are unknown). I use object-fit: contain to make the image completely visible inside the div and also keep it's aspect ratio. The problem is that object-fit isn't modifying the width of the image, resulting in a weird "padding" (so to say).
How can I make the image take only as much width as required, instead of taking the original width?
Demo: http://codepen.io/alexandernst/pen/ONvqzN
.wrapper {
height: 100px;
width: 300px;
border: 1px solid black;
}
.flex {
display: flex;
}
img {
object-fit: contain;
}
<div class="flex wrapper">
<img src="https://unsplash.it/240/240" />
</div>
The object-fit property normally works together with width, height, max-width and max-height. Example:
.wrapper {
height: 100px;
width: 300px;
border: 1px solid black;
}
.flex {
display: flex;
}
img {
object-fit: contain;
height: 100%;
width: auto;
}
<div class="flex wrapper">
<img src="https://unsplash.it/240/240" />
</div>
In fact, it works fine too even without object-fit, see this jsFiddle.
.wrapper {
height: auto;
width: 100%;
border: 1px solid black;
background-image: url(https://unsplash.it/240/240);
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
.flex {
display: flex;
}
img {
object-fit: contain;
}
<div class="flex wrapper">
<img src="https://unsplash.it/240/240" />
</div>
Related
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>
I have a div on which I defined following CSS rules
<style>
.image-preview {
height: 600px;
width: 50%;
border: 1px solid red;
}
.image {
width: auto;
height: 600px;
}
</style>
<div class="image-preview">
<img src="abc.jpg" class="image">
</div>
Suppose if width:50% is 300px and abc.jpg is of 500px it comes out of the div. I want to keep the image in the div as center aligned and with maintaining the aspect ration of it.
I have given the width:auto but this is not working. I have not much experience with CSS so please ignore if this is too basic to ask.
Thanks!!
You could use display flex to center your image with this you make sure that the images always maintains its aspect ratio...
.image-preview {
height: 600px;
width: 50%;
border: 1px solid red;
display:flex;
align-items:center;
justify-content:center;
}
.image {
max-width:100%;
max-height:100%;
}
<div class="image-preview">
<img src="http://via.placeholder.com/500x150" class="image">
</div>
Try to use flexbox, which is probably the best tool to align items in divs. You can find out more about it here: https://www.w3schools.com/csS/css3_flexbox.asp.
.image-preview {
height: 600px;
width: 600px; /*whatever you want*/
border: 1px solid red;
display: flex;
justify-content: center;
align-items: center;
}
.image {
height: 400px; /*Any value would still keep it vertically centered thanks to the align-items property*/
width: auto;
}
<div class="image-preview">
<img src="https://www.ancestry.com/wiki/images/archive/a/a9/20100708215937%21Example.jpg" class="image">
</div>
The justify-content property handles the horizontal alignment, and the align-items property handles the vertical alignment.
So, image is a inline element:
img {
display: block;
margin: 0 auto; //this is center your image
height: auto; // this will make it ration
max-width: 1200px; //or whatever is the size of your image
}
This will make your image full responsive, centered and ration.
.image-preview {
border: 1px solid red;
box-sizing: border-box;
height: 600px;
padding: 10px;
text-align: center;
width: 50%;
}
.image {
width: auto;
height: 100%;
max-width:100%;
}
<div class="image-preview">
<img src="https://placeimg.com/300/600/any" class="image">
</div>
You can do it by setting height 100%; width:auto to image.
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>
I am working on a website and I want to style an image like you style a background with background-position: center. Is there an equivalent to this for a regular image?
Thanks in advance,
Luuk
EDIT:
The image is responsive and contained in a container.
CSS:
.img-container {
max-height: 300px;
overflow: hidden;
}
.img-container img {
max-height: 100%;
max-width: 100%;
}
I would do something like this to position the image centered.
.img-container {
display: flex;
justify-content: center;
align-items: center;
max-height: 300px;
overflow: hidden;
}
.img-container img {
max-height: 100%;
max-width: 100%;
}
You could go for display flex, but the support for IE is quite disastrous. If you care about browser support (which you should) go for the below example instead.
<div class="list">
<div class="item">
<img src="https://unsplash.it/50/40" alt="">
</div>
<div class="item">
<img src="https://unsplash.it/50/60" alt="">
</div>
<div class="item">
<img src="https://unsplash.it/50/30" alt="">
</div>
<div class="item">
<img src="https://unsplash.it/50" alt="">
</div>
</div>
Sass:
.list {
text-align: center;
}
.item {
position: relative;
display: inline-block;
vertical-align: top;
height: 5rem;
width: 5rem;
background: red;
img {
position: relative;
top: 50%;
transform: translateY(-50%);
}
}
Make sure to add the prefixes for the transform attribute as well.
Fiddle: https://jsfiddle.net/k433b6up/
Your desired result isn't exactly clear, but here are some options for two different interpretations:
Changing the display property of the image and setting the text alignment to its container will maintain it's native height and width while centering it in the container:
.img-container {
max-height: 300px;
overflow: hidden;
text-align: center;
}
.img-container img {
display: inline-block;
max-height: 100%;
max-width: 100%;
}
Demo: https://jsfiddle.net/5suo8tbw/
If you're trying to achieve a background fill with an img element you should consider using the object-fit: cover attribute. This will always fill your container's dimensions, maintain the image's aspect ratio, and center it in the container.
.img-container {
max-height: 300px;
overflow: hidden;
}
.img-container img {
object-fit: cover;
height: 100%;
width: 100%;
}
Demo: https://jsfiddle.net/5suo8tbw/1/
Here's a link to the spec: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
For cross browser support, check out the polyfill: https://github.com/anselmh/object-fit
object-fit and object-position properties of an img are equivalent to background-size and background-position properties of a block element with background-image, respectively. In the following snippet, please note that the container size is 300px * 300px while the image size is 500px * 300px.
.imageContainer {
margin: 15px;
width: 300px;
height: 300px;
border: 5px solid lightgreen;
}
.image {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
<div class='imageContainer'>
<img class='image'src='https://picsum.photos/500/300' alt='lorem picsum' />
<div>
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.