Image position equivalent to background position - html

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>

Related

Force image to stay between height & width "boundaries", as well as keep aspect ratio

I wish to keep an image between a minimum and maximum height & width, and at the same time respect the aspect ratio (so if an image needs to be scaled down it would scale down keeping aspect ratio, until both width and height parameters are below the given max-width and max-height)
It tried the following css:
.box {
min-width: 128px;
min-height: 128px;
width: auto;
height: auto;
max-width: 100%;
max-height: 256px;
display: flex;
}
.inner {
background-color: cyan;
width: 100%;
height: 100%;
display: block;
object-fit: contain;
}
<div class="box">
<img class="inner" src="https://i.imgur.com/3t7YAt8.png"/>
</div>
However, when running above it directly shows the max-height is just ignored and the image only respects the max-width constraint. (can be shown by modifying max-width, the image will scale that way).
If I change the css for the image itself to use max-width and max-height instead of width/height attribute the image no longer scale keeping the aspect ratio.
.box {
min-width: 128px;
min-height: 128px;
width: auto;
height: auto;
max-width: 100%;
max-height: 256px;
display: flex;
}
.inner {
background-color: cyan;
max-width: 100%;
max-height: 100%;
display: block;
object-fit: contain;
}
<div class="box">
<img class="inner" src="https://i.imgur.com/3t7YAt8.png"/>
</div>
As a side point: in the example, the image is a square, this isn't always the case.
In order for the image to maintain aspect ratio, use css property "object-fit: contain;" on the image tag
.box {
min-width: 128px;
min-height: 128px;
width: auto;
height: auto;
max-width: 100%;
max-height: 256px;
position: relative
}
.inner {
background-color: cyan;
width: auto;
height: 100%;
display: block;
object-fit: contain;
position:absolute;
}
<div class="box">
<img class="inner" src="https://i.imgur.com/3t7YAt8.png"/>
</div>

Need image to fill height of div

I did do many searched for this but anything I try is not working. I need the image to fill the height of the div, the extra image can get off to the right. But anything I try is not working... What am I missing? I don't want to see any red in this box but yet keep the proportions of the image. Thank You!
https://jsfiddle.net/rhwx23o4/6/
<figure id="main-img"><img src="http://http://www.kimwilddesigns.com/web-lesson/4-4_start/images/hp_main-img_1.jpg"/></figure>
figure#main-img {
width:100%;
min-height: 200px;
background-color: red;
overflow: hidden;
}
figure#main-img img {
width: 100%;
object-fit: contain;
}
You can use height: 100vh;
https://jsfiddle.net/rhwx23o4/65/
figure#main-img {
width:100%;
height: 100vh;
display: block;
background-color: red;
overflow: hidden;
}
figure#main-img img {
height: 100vh;
object-fit: contain;
}
Just a little bit of work around. On the breakpoint i.e here 840px I gave property display:none to your image and gave outer div background of same image.
NOTE: In media query I had to hard-code the max-width. It won't be same for all images.
figure#main-img {
width: 100%;
min-height: 200px;
background-color: red;
overflow: hidden;
}
figure#main-img img {
width: 100%;
}
#media (max-width: 840px) {
div {
background: url('http://www.kimwilddesigns.com/web-lesson/4-4_start/images/hp_main-img_1.jpg');
height: 200px;
background-size: cover;
background-position: center center;
}
img {
display: none;
overflow: auto;
min-height: 200px;
max-height: 200px;
}
}
figure {
margin: 0px;
}
<figure id="main-img">
<div><img src="http://www.kimwilddesigns.com/web-lesson/4-4_start/images/hp_main-img_1.jpg" /></div>
</figure>
Try this CSS. Is this what you want? I gave the image a display property of block and changed the object-fit to cover instead of contain.
figure#main-img {
width:100%;
min-height: 200px;
background-color: red;
overflow: hidden;
}
figure#main-img img {
display: block;
min-height: 200px;
width: 100%;
object-fit: cover;
}
Check this https://jsfiddle.net/rhwx23o4/76/
Added height="100%" and width="100%" to the image tag
You can just use display: flex; in <figure> like this codepen:
figure#main-img {
width:100%;
min-height: 200px;
background-color: red;
overflow: hidden;
display: flex;
}
Flex has magical powers to adjust content within it.
In case you do not want <figure> to take up whole screen width (which it would being a block element), you can change its display to display: inline-flex; and then add a certain width/max-width to it.

Child image(height, width in px dynamically) fit to parent 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>

Image to fill div proportionally without using background-size: cover or object-fit: cover

I'm trying to fill a div with an image while maintaining its aspect ratio. But i do not want to use a background image with background-size: cover or even use the object-fit: cover property, I want the result of them using the img tag.
As you can see in the code below, without using the object-fit: cover or background-size: cover the image is stretched and this is not the result that i want.
.post-thumbnail {
width: 352px;
height: 240px;
overflow: hidden;
}
.post-thumbnail img {
width: 100%;
height: 100%;
}
<div class="post-thumbnail">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSKh0q0NzNKTmUA9q-uxaJIRx3pNYgbqzEdGW1cXFdIlZ_SlV-M">
</div>
In the code below the image is not stretched, due to object-fit: cover, i want this same result without using this property, since it does not have a good compatibility. Does anyone know how can i do this?
.post-thumbnail {
width: 352px;
height: 240px;
overflow: hidden;
}
.post-thumbnail img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="post-thumbnail">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSKh0q0NzNKTmUA9q-uxaJIRx3pNYgbqzEdGW1cXFdIlZ_SlV-M">
</div>
you could use clip() + position:absolute, or use negative margins along vertical-align and text-align:
.post-thumbnail {
display:inline-block;
width: 352px;
height: 240px;
line-height:240px;
text-align:center;
overflow: hidden;
}
.post-thumbnail.small {
width: 40px;
height: 60px;
line-height: 60px;
}
.post-thumbnail img {
min-width: 100%;
min-height: 100%;
vertical-align:middle;
margin:-500px;
}
/* demo purpose to show what is being hidden;*/
.post-thumbnail {
margin:50px;
overflow: visible;
box-shadow:0 0 0 50px rgba(200,200,200,0.5);
border:solid blue;
}
.post-thumbnail img {
position:relative;
z-index:-1;
}
<div class="post-thumbnail">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSKh0q0NzNKTmUA9q-uxaJIRx3pNYgbqzEdGW1cXFdIlZ_SlV-M">
</div>
<div class="post-thumbnail small">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSKh0q0NzNKTmUA9q-uxaJIRx3pNYgbqzEdGW1cXFdIlZ_SlV-M">
</div>
You are setting the height property and the width property, that way you are stretching you page. If you only set one, the other one scales with it. Depending on what kind of picture you have and how you want it, you need to set the height and width.

Fill a div with a reponsive <img> and position centrally [duplicate]

This question already has answers here:
center image in a div
(3 answers)
Closed 6 years ago.
How can I ensure that the img within the container is centered and scaling correctly from mobile to desktop? Here is a demo on Codepen. Scale to mobile to better understand the problem
HTML
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>
CSS
.image-container{
width: 100%;
height: 500px;
max-height: 500px;
background: red;
overflow: hidden;
text-align: center;
position: relative;
display: block;
}
img{
height: auto;
width: 100%;
max-width: 100%;
position: absolute;
top: -50%;
left: 0;
}
I ask this because the image loses it height on mobile and looks incorrect. I sort of what this to work like `background-size: cover. I'd like the image to completely fill the container
You can add margin:0 auto; to align center horizontally. Remove width:100% as this will stretch width of an image unless you want it to. Keep height:auto to adjust with width.
.image-container{
width: 100%;
height: 500px;
max-height: 500px;
background: red;
overflow: hidden;
text-align: center;
position: relative;
display: block;
}
img{
height: auto;
max-width: 100%;
margin:0 auto;
}
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>
To center image horizontally and vertically inside div you can set top: 50% and left: 50% and then just add transform: translate(-50%, -50%).
To make image responsive you can use max-width: 100% and by default height is auto.
.image-container {
width: 100%;
height: 500px;
background: red;
overflow: hidden;
position: relative;
}
img {
max-width: 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>
Other option is to use Flexbox if you don't want to use relative/absolute position but to keep img responsive you can use object-fit: contain with height: 100%
.image-container {
width: 100%;
height: 500px;
background: red;
display: flex;
align-items: center;
justify-content: center;
}
img {
max-width: 100%;
object-fit: contain;
}
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>
If you want img to behave like background-size: cover one way to that is to use object-fit: cover with height: 100% and width: 100%
body,
html {
margin: 0;
padding: 0;
}
.image-container {
width: 100%;
height: 500px;
background: red;
display: flex;
align-items: center;
justify-content: center;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>