I want to fill a parent div with a img, but instead of setting the image as a background property of the div, I want it to be a individual img element inside of the div.
In other words, I want to do this:
div {
background-image: url('someimg.jpg');
background-size: cover;
background-repeat: no-repeat;
}
..but with a img element, like this:
<div>
<!-- Image element should cover the container on dekstops and small screens as well -->
<img src="someimg.jpg">
</div>
I managed to do something like this on the img:
img {
width: 100%;
max-height: 100%;
object-fit: fill;
}
..but on resize, it doesn't fill the parent. Here's a fiddle: https://jsfiddle.net/dy5ub0f5/5/
The img may be filling the parent, but the parent may not be the size you expect.
Change the img styling from max-height: 100% to height: 100%
Adjust the size of the parent div.
Since, you give max-height:100%; in CSS, if your image resolution is less than the div (400Px) then it will stock on with its original height (375px). That's why the image is not filling the parent div on resizing.
div {
height: 400px;
width: 100%;
background-color:#ececec;
}
img {
width: 100%;
height: 100%;
object-fit:fill;
}
<div class="image">
<img src="https://png.pngtree.com/thumb_back/fw800/back_pic/00/06/31/695628f664c006c.jpg">
</div>
The Above Code Will Make the image to fill in its Parent DIV. But Since the picture resolution is less than the div, the image will blur.
To avoid you can use max-width and max-height, so that if the image have less resolution than the div, it will stick with its own size, otherwise it will fill the parent div like the following:
div {
height: 400px;
width: 100%;
background-color:#ececec;
}
img {
max-width: 100%;
max-height: 100%;
object-fit:fill;
}
<div class="image">
<img src="https://png.pngtree.com/thumb_back/fw800/back_pic/00/06/31/695628f664c006c.jpg">
</div>
Related
I've tried everything I can find.
I'm trying to keep an image from overflowing outside the div that it is inside.
I've tried:
div.row #whoPic {
object-fit: contain;
max-width: 100%;
max-height: 100%;
overflow: hidden;
max-width: auto;
max-height: auto;
max-width: 300px;
max-height: 300px;
}
...and many variations of each of those values.
I've also tried targeting all img's, as well as targeting very precisely.
Please help me out. Going nuts here.
In fact this question has been asked before.
Here: How do I auto-resize an image to fit a div container
Here: Make an image width 100% of parent div, but not bigger than its own width
Here: Contain an image within a div?
Here: How do I fit an image (img) inside a div and keep the aspect ratio?
Restrict the image with max-width and max-height both set to 100%.
.theContainer {
width: 300px;
height: 100px;
background-color: lightblue;
}
.theContainer img {
max-width: 100%;
max-height: 100%;
}
<div class="theContainer">
<img src="https://placehold.it/200x200">
</div>
<h3> The original image size is 200x200 </h3>
<img src="https://placehold.it/200x200">
I have 2 div's named first and second and I have set the width and height of them as 100%
.first{
width : 100%;
height : 100%;
}
.second{
width : 100%;
height : 100%;
}
now I would like to add an image in each div. These images should fill in the entire div.
<img src="someimage.png" width="100%" height="100%"/>
My problem is the image should not be stretched it should be filled the entire screen. I have used img img-responsive classes to achieve this. The image is now getting filled without stretching but when resized it is getting resized uniformly and the height of it is also getting decreased hence the image's height is now not getting filled 100%. Is there any way to achieve width and height of an image to cover the entire screen without stretching and decreasing the height?
Check this out, and you should use width: 100% beside min-height: 100% but i recommend you to use background-image with background-size: cover
.first{
width : 100%;
height : 100%;
}
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.first img {
width: 100%;
min-height: 100%;
}
<div class="first">
<img alt="" src="https://placeholdit.imgix.net/~text?txtsize=33&txt=500%C3%97500&w=500&h=500"/>
</div>
jsFiddle
I use imgLiquid this is a jQuery Plugin to resize images to fit in a container.
https://github.com/karacas/imgLiquid
It's super easy to use and light weight.
When I set the background image at I have to give it a static size of div otherwise it will not display the background images. What is problem.....
I have this CSS:
.wrapper {
width: 1170px;
margin: 0 auto;
height: auto !important;
}
.main_div {
float: left;
width: 100%;
height: auto !important;
}
body {
font-family: Verdana, Geneva, sans-serif;
font-size: 14px;
width: 100%;
height: auto !important;
}
.header {
width: 100%;
background-image:url(file:///D|/HJ/ALL%20HTML%20TEMP/bootstrap/bootstrap/img/header_bg.jpg);
float:left;
}
The first comment is correct, I decided to create a little demo to explain this.
So if you take a look at the demo you can see we have the first div using background to place an image. This is fine and valid CSS but without a height and width how can the background be displayed?
Moving onto the second div, here we give the div with the background and height/width. Now the background has appeared. Because we have defined the height and width the background has room to display. A background cannot tell the element to be a certain size without you defining it.
And the last div, this has <img> inside of it. As this is an block element it has a height and width, so it will show the image as the parent has no height or width and therefore will allow the image to expand inside of it.
HTML:
With no height:
<div class="image"></div>With height:
<div class="imageWH"></div>With img:
<div class="imageIMG">
<img src="http://placehold.it/350x150" alt="" />
</div>
CSS:
.image {
background: url(http://placehold.it/350x150) no-repeat;
}
.imageWH {
background: url(http://placehold.it/350x150) no-repeat;
width: 350px;
height: 150px;
}
.imageIMG {
<!-- No need for anything -->
}
DEMO HERE
Why do you float the .main_div with width:100%? Floated elements get out of the flow which means they don't stretch their parent elements, so a background set on .main_div's parent won't show.
You should either remove the float or add some clearing technique to the header (also known as a css clearfix). See demonstration here: http://jsfiddle.net/YLEgY/1/
In the middle of a webpage I want to display an image that stretches all the way from the left to the right side of the browser window.
When the window is resized the image should scale down proportionally but I need the visible height of the image, let's say 200px, should remain the same as there is a div on the image displaying text. I presume the "image window" is preserved by a containing div with a fixed height.
How do one accomplish this and get it working in the following browsers: ie8+, Chrome20+, FF20?
Just set width and leave the height empty, it will auto scale
img {
width: 100%;
}
As you want a set height you should wrap the image in a div with overflow hidden. It will scale as you want etc.
EDITED
DEMO http://jsfiddle.net/vYdBt/136/
img {
width:100%;
}
div {
width: 100%;
height: 200px;
overflow: hidden;
}
The website in your link uses a background image. This could be done like this
DEMO http://jsfiddle.net/kevinPHPkevin/vYdBt/135/
div {
background: url(http://lionssharedigital.com/wp-content/uploads/2013/04/Lion.jpg);
width: 100%;
height: 300px;
background-position: center center;
background-repeat: no-repeat;
background-size:100% auto;
}
This would center the image vertically too
Ah, the ever popular, "how do I do responsive images?" Well, in your cause it's business as usual other than you set a specified height on the image.
http://jsfiddle.net/aMhbz/1/
HTML
<div id="top"></div>
<div id="image">
<img src="http://lorempixel.com/1200/200/city/" />
</div>
<div id="bottom"></div>
CSS
#top, #bottom {
height: 50px;
}
#image img {
max-width: 100%; /* the responsive part */
height: 200px; /* same height as image */
}
My answer does a little extra targeting based on a image container. You could also do some thing like this:
HTML
<img src="your-image.jpg" />
<div>
<img class="set-height" src="your-image.jpg" />
</div>
CSS
/* for all images to be responsive */
img {
max-width: 100%;
}
/* specific images */
.set-height {
height: 200px; /* the height of your image */
}
I have a div with the following CSS
#mydiv{
top: 50px;
left: 50px;
width: 200px;
height: 200px;
}
and my HTML looks like this
<div id = "mydiv">
<img src = "folder/file.jpg" width = "200px" height = "200px">
</div>
I'd like my web image to always be the same size (in a 1:1 aspect ration) no matter what the resolution of the actual image is. If my actual image files are square (with 1:1 ratio) then this isn't a problem. But if the actual image files are not square then the displayed web image do stretch to 100% of both the div's height and width (in this case 200px).
How do I get different image sizes to fit to my DIV?
You're mixing notations. It should be:
<img src="folder/file.jpg" width="200" height="200">
(note, no px). Or:
<img src="folder/file.jpg" style="width: 200px; height: 200px;">
(using the style attribute) The style attribute could be replaced with the following CSS:
#mydiv img {
width: 200px;
height: 200px;
}
or
#mydiv img {
width: 100%;
height: 100%;
}
Instead of setting absolute widths and heights, you can use percentages:
#mydiv img {
height: 100%;
width: 100%;
}
Or you can put in the CSS,
<style>
div#img {
background-image: url(“file.png");
color:yellow (this part doesn't matter;
height:100%;
width:100%;
}
</style>
will the height attribute stretch the image beyond its native resolution? If I have a image with a height of say 420 pixels, I can't get css to stretch the image beyond the native resolution to fill the height of the viewport.
I am getting pretty close results with:
.rightdiv img {
max-width: 25vw;
min-height: 100vh;
}
the 100vh is getting pretty close, with just a few pixels left over at the bottom for some reason.