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 */
}
Related
I want to make a gif image responsive to resize it to cover the width of every mobile device. This is because for some reason I can get a video to autoplay in certain IOS devices, so I though of converting the video into a gif. Now, how can I make it responsive? I don't really care about the height, is more of a width problem.
this is my oode so far:
.box {
width: 500px !important;
}
<div class="box">
<img src="examplegif" alt="Example gif" style="width:340px;height:170px;">
</div>
you have to set the maximum width to the image or video in this case of the gif.
<style>
.box {
display: flex;
justify-content: center;
}
img {
max-width: 100%;
height: auto;
}
</style>
<html>
<body>
<div class="box">
<img src="https://media.giphy.com/media/VRhsYYBw8AE36/giphy.gif" alt="Example gif">
</div>
</body>
</html>
The first step would be to remove width: 500px from the parent container .box. This explicit width restricts the image from being able to fill the entire viewport since it's parent containers width is only 500px. Next, there is an 8px default margin on the <body> from the user agent stylesheet. Using margin: 0 within the body styling will ensure there isn't any extra space around your content.
To make the GIF responsive and fill the entire viewport width, you can add some styling to the nested <img> elements in .box. I gave the images a width and max-width of 100% to ensure they take up all the available space. For the image height, use height: 100% to make sure it's 100% of the containing blocks height or height: auto to let the browser calculate and select a height for the specified element. Try this out.
body {
margin: 0;
padding: 0;
}
.box{
width: auto;
}
.box img {
max-width: 100%;
width: 100%;
height: auto; /* vary this to your needs ie auto, 100%, etc */
margin: 0 auto;
}
<div class="box">
<img src="https://media.giphy.com/media/ZqlvCTNHpqrio/giphy.gif" alt="Example gif">
</div>
You can make your image responsive by applying the following class to your image.
CSS:
.responsive {
max-width: 100%;
width: auto;
height: auto;
}
and HTML:
<img src="ypurgif" class="responsiveImage" />
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>
I have a div profile_pic which has the following CSS:
#profile_pic{
position:absolute;
border:1px solid #E1E3E4;
left:25px;
top: 25px;
width: 200px;
height: 200px;
}
Since profile picture for my application can be any image (of any size), the div or image, should be flexible to adapt to one another. I have tested a profile picture with the dimensions of 300px width and 300px height and the image renders perfectly in the the div. However, when I upload a picture with say, 550px width and 400px width the image is appearing "squashed" which is understandable.
There are two options, 1. resizing the image so that the whole image appears in the div and 2. cropping the image so that the image adapts to the div size. I do not mind adopting either of these approaches but I am unable to implement how these approaches in code.
I have tried to set:
#profile_pic {width: 50%}
#profile_pic img {width:100%}
But it just does not work. How can I get the div (or image) to always fit in the div's size without the image losing it's quality?
You could just add background-size:contain; to the div that has the image (assuming you are setting the background image the image you want.
losing quality is another thing, scaling say a 50x50px image to 100x100 is going to lose quality, so it would probably be best to set a minimum size the profile picture can be.
You may set max-width and max-height in order to resize image to fit inside the box without overflow, add line-height and text align to center image in case it has not the same box ratio.
#profile_pic,
.profile_pic2 {
position: absolute;
border: 1px solid #E1E3E4;
left: 25px;
top: 25px;
width: 200px;
height: 200px;
line-height: 197px;
/* since image is the one and single child */
text-align: center;
border: solid;
/*demo purpose */
}
.profile_pic2 {
left: 250px;
}
.profile_pic2 +.profile_pic2 {
left: 450px;
}
#profile_pic img, .profile_pic2 img {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
/* set on middle baseline setted at 200px */
}
<div id="profile_pic">
<img src="//lorempixel.com/640/480">
</div>
<div class="profile_pic2">
<img src="//lorempixel.com/480/640">
</div>
<div class="profile_pic2">
<img src="//lorempixel.com/480/480">
</div>
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/
I have a gallery slider, with random images from the forum. So, the size is pretty random but the gallery(container frame) itself is fix sized. So, we decided to set the image height to a fixed size but the width is set to auto. This way, the image will not be squeezed inside the container if its ratio different is too much from the container ratio.
Then, I set the container's text-align to center in order to center the image. But, this only works for images smaller than the container. If the image is still bigger than the container (after resize), the image is aligned to the left instead.
The jsffidle example.
NOTE: Using background-image is not a solution because resizing background image currently is still not supported by many browsers (especially IE and some Chinese browsers).
Hope there is enough information here. So, how do I center the image in this situation?
I have found another solution
<style type="text/css">
.container {
width:600px; //set how much you want
overflow: hidden;
position: relative;
}
.containerSecond{
position: absolute;
top:0px;
left:-100%;
width:300%;
}
.image{
width: 800px; //your image size
}
</style>
and in body
<div class="container">
<div class="containerSecond">
<image src="..." class="" />
</div>
</div>
This will center your image whenever your container is bigger or smaller. In this case your image should be bigger than 300% of container to not be centered, but in that case you can make with of containerSecond bigger, and it will work
You would use max sizes:
img {
max-width: 200px;
max-height: 200px;
}
Take a look: http://jsfiddle.net/fabianhjr/zW6eh/
Edit: still having centring problems, I will get back to you on that.
I had similar problem, but the solution was about to crop right and left margin, while the image should be centered. Smaller images are stretched.
My solution is also in this JS Fiddle: http://jsfiddle.net/david_binda/9tTRQ/
HTML
<div class="thumb-wrapper">
<a href="" title="" class="img">
<img src="http://upload.wikimedia.org/wikipedia/commons/4/40/Tectonic_plate_boundaries.png" alt="" />
</a>
</div>
CSS
.thumb-wrapper{
width: 200px; // desired thumbnail width
height: 150px; // desired thumbnail height
overflow: hidden;
position: relative;
margin: 0 auto;
}
.thumb-wrapper .img{
display: block;
position: absolute;
width: 300px; // should be wider than final thumbnail
height: 150px; // desired thumbnail height
left: 50%;
margin-left: -150px; // half of above defined width eg. 300/2 = 150
}
.thumb-wrapper .img img{
width: auto !important;
max-width: 300px !important; // should be wider than final thumbnail
min-width: 200px !important; // desired width of thumbnail
height: 150px !important; // desired thumbnail height
margin: 0 auto;
display: block;
}
The solution that I've found is:
img{
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);//You have to add all the prefixes
//of transform
}
div.container{
position:relative;
overflow:hidden;
}
Okay, I think this is your best solution.
You set your wrapper around each image to display: table; and then one more wrapper inside that with a display: table-row; and set your img's to display: table-cell
This way you can resize anyway you like while keeping the ratio.
http://jsfiddle.net/zW6eh/17/
You can also simply set your height: to 200px; This will keep your width auto by default.