This question already has answers here:
Scale div to fit background image
(4 answers)
Closed 5 years ago.
Can I stretch a div to fit the background image's width and height using only CSS? And if yes, how?
Div has width and height unset.
I am NOT asking how to scale the image into a div.
I am NOT asking how to scale the image into a div with specific width and height.
ie.
<div class="banner"
styles="width: auto; height: auto; background-image: url(unknown-size-img.jpg)">
</div>
I have a suggestion, but may not be a good method.
Put the same image as a tag inside the DIV. So the container div will take the same size of the . Then make the zero opacity and may be pointer-events 'none' like below
<div class="banner" styles="width: auto; height: auto; background-image: url(unknown-size-img.jpg)">
<img src="unknown-size-img.jpg" alt="image" title="image" style="opacity:0 ; pointer-events: none" />
</div>
Related
This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 1 year ago.
I have this div that has a width of 100% and a height of 90px. Inside the div I have a img that is 90px in height, but I let the width set itself. So how do I set the img withing the div, without knowing it's width?
There is some useful discussion on the centering of img elements at
https://stackoverflow.com/questions/34247337/object-fit-not-affecting-images
Here's a snippet which centers an img based on the recent answer in that link from #MohammadImami
You can try altering the dimensions of the img (the last two in the url are width and height respectively) to see what happens. All is fine unless the img is so wide compared to its height of 90px that it wont fit, in which case this code shrinks the height to ensure the whole img is included in the container. It is centered vertically as well as horizontally. I don't know whether this is a situation you will encounter. It seems like the most sensible thing to do - so none of the img is cropped out. But I don't know your requirement in this extreme situation.
.container {
width: 100%;
height: 90px;
}
.container img {
height: inherit;
width: inherit;
object-fit: contain;
object-position: center;
}
<div class="container">
<img src="https://picsum.photos/id/1016/1024/768">
</div>
If you want to center the image inside the div, you can try
img {
margin: 0 auto;
}
It will let the top and bottom margin as 0px and right and left as auto (centered).
If you want the image to cover the entire width, use the background-image property
div {
background-image: url('image.jpg');
}
I don't know if this is what you wanted to do.
This question already has answers here:
Image inside div has extra space below the image
(10 answers)
Closed 2 years ago.
I feel like such a newb asking this, but I can't figure out why any div container seems to always be taller than the content that it contains. See https://codepen.io/grayayer/pen/XWjBJWZ for working example.
.container {
background-color:red;
height: fit-content;
width: fit-content;
padding: 0;
}
<div class="container">
<img src="https://via.placeholder.com/350" height="350" width="350">
</div>
It's like there's always 5px of padding on the bottom, even though I've specifically said not to.
Images are rendered inline by default, so that space at the bottom is accommodating text decenders. You can set display: block; on the image to remove the space.
More information in this answer.
This question already has answers here:
Fit div size to background image
(3 answers)
Size the DIV to the size of the background image
(3 answers)
Closed 4 years ago.
I have a div with some content and want to have a background image. However, I want to be able to see the full height of the image. I could add a load of padding to the top and bottom of the content but I want this to be dynamic for all screen sizes.
div{
background-image:url(https://via.placeholder.com/350x150);
background-position: center;
background-repeat: no-repeat;
background-size: cover;
/* height 100%; */
text-align:center;
}
<div>
Some content
</div>
I know I could also just add an <img> tag to the <div> but there is a lot of content in this that would then have to be floated around to overlay the image.
I don't mind using JS/jQuery to resolve this.
To make something fit the full height of the screen instead of using 100% as you would for width, using vh, so in your case:
height: 100vh;
This will work dynamically depending on the users screen dimensions, you can read more about this as well as see examples here.
I was searching for solution for hours but can't find it.
I have div with fixed height and 50% width. And I want to display a picture inside it with 100% width and default aspect ratio but vertically centered
Thanks ;)
<div class="wrap">
<img class="img">
</div>
Add overflow:hidden to your div and then adjust the margin of the image into the negative. How much depends on the div's fixed height and the image height.
EDIT
Consider using CSS and background images if you don't know the image heights. Instead of outputting an image tag, output an inline style on the div.
<div class="css-is-good" style="background-image: url(image.jpg);"></div>
CSS
div.css-is-good {
background-position: 50% 50%;
/* if you need to stretch it, use background-size: */
background-size: 100% auto;
}
Updated fiddle: http://jsfiddle.net/willthemoor/Cu3G5/
Actually, I'm having very big width image. Width is 3000px and height is 100px. I need to display image's center part in my browser.
If I put my image, It's showing left part of the image. But first I should display center part. If Display screen is big, then side can display.
and it's inline image only.
<div class="my_img">
<img src="img.png" >
</div>
What can I do?
Use margin:0 auto; display:block;. This will make it to the center.
contain scales the image to the largest size such that both its width and its height can fit inside the content area.
Try:
img{background-size:contain;}
Not the cleanest way I'm sure, but it works:
http://jsfiddle.net/7eA3W/
<div>
<img src="http://placehold.it/3000x100" alt="" />
</div>
div {
overflow: hidden;
width: 300px;
}
img {
margin-left: -1350px; // minus half of the image width, minus half of the container width
}
If you want to show the center of the image, what you have to do is using a div instead of an img.
In that div's style you set tthe background to be the image you want to show, and then, you center it with css background-position: 50% 0px
<div class="my_img">
<div
style="background-image:url(img.png);
height:YOUR_IMAGE_HEIGHT;
background-position: 50% 0px;">
</div>
</div>
with this, if the screen is small, the imagen wiil be cropped and show the center. If the screen is large, youll see the whole image, also centered