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.
Related
I need to center-align my main div image which I have working on wider screens. But when I reduce the width of the screen, the image always starts from the left and is cropped off at the right.
I need the image to be centered within screen size, even if it is too wide.
body {
background-color: grey;
overflow: hidden;
margin: 0px;
}
.vid {
height: 100vh;
display: block;
margin: auto;
}
<img class="vid" src="http://wizzfree.com/pix/testbg.jpg">
Instead of specifying the height, you should specify the width. Here is an example:
.vid {
width:90%;
display: block;
margin: auto;
}
body {
background-color: grey;
margin:0;
}
.vid {
width: 100vw;
margin-top:10vw;
}
<img class="vid" src="http://wizzfree.com/pix/testbg.jpg">
You may try to apply these styles to the img's parent:
body {
display: flex;
justify-content: center;
align-items: center;
max-width: 100%;
max-height: 100vh;
}
img {
width: 100%;
height: 100%;
}
You can use object-fit lets you specify how the image should fit into the space:
.vid {
object-fit: cover; /* fill the whole container centring image if it doesn't fit */
height: 100vh;
width: 100%; /* object-fit needs to have a width set */
}
Using object-fit:cover will make the image fill (or "cover") the whole space, cropping off the left and right if it is too wide for the space so that it is centred.
Working Example - full cover
body {
background-color: grey;
overflow: hidden;
margin: 0px;
}
.vid {
object-fit:cover;
height: 100vh;
width: 100%;
}
<img class="vid" src="http://wizzfree.com/pix/testbg.jpg">
Working Example Setting a max height & width - you can also set a min/max height or width on the image too if you need to - these can be set to any specific value (e.g. 800px) or % (e.g. max-width:100%):
body {
background-color: grey;
overflow: hidden;
margin: 0px;
}
.vid {
object-fit:cover;
/* e.g. to limit the size of the image to its actual size */
max-width: 100%;
min-height: 500px; /* for example, but not required */
max-height: 100vh;
margin: auto;
display:block;
}
<img class="vid" src="http://wizzfree.com/pix/testbg.jpg">
Its better to use background image when you want to fill a div with a image(most of the time).I hope this this will help you resolve your issue.
body {
overflow: hidden;
margin: 0px;
}
.vid {
height: 100vh;
background-image: url('http://wizzfree.com/pix/testbg.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
<div class="vid" > </div>
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'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.
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've been trying to figure out if there is a pure CSS solution to ensure the image within my banner doesn't go below the height of the parent but keep ratio of the image.
You can see a demo here: http://jsfiddle.net/LkxYU/1/
html:
<div class="banner_holder">
<img src="http://placekitten.com/g/800/600"/>
</div>
css:
.banner_holder{
width: 100%;
height: 300px;
min-height: 200px;
position: relative;
outline:1px dotted red;
}
.banner_holder img{
width: 100%;
}
My aim is to have the image always 100%, but never below 300px in height. This would mean image cutoff, but thats fine, i just want to know if there is a pure CSS solution, or if i need to use jQuery
Instead of using an < img > tag, I made that image the background-image for a div and gave it the following styles:
.banner_holderImage{
height: 100%;
position:relative;
background: url("http://placekitten.com/g/800/600")no-repeat;
background-size: cover;
background-position: center;
}
here's the fiddle I was using: http://jsfiddle.net/MathiasaurusRex/LkxYU/4/
Here's the complete HTML and CSS:
<div class="banner_holder">
<div class="banner_holderImage"></div>
</div>
--
.banner_holder{
width: 100%;
height: 300px;
min-height: 200px;
position: relative;
outline:1px dotted red;
}
.banner_holderImage{
height: 100%;
position:relative;
background: url("http://placekitten.com/g/800/600")no-repeat;
background-size: cover;
background-position: center;
}
Your image will inevitably be out of ratio depending on the screen size, why not try a background image:
.banner_holder{
width: 100%;
height: 300px;
min-height: 200px;
position: relative;
outline:1px dotted red;
background: url('http://placekitten.com/g/800/600') center no-repeat;
background-size: cover;
}
or you could just add a max height to your image tag:
.banner_holder img{
width: 100%;
max-height: 300px;
}
try:
.banner_holder img{
height: 100%;
/* OR */
height: inherit;
}
Use max-height and max-width to be sure that image will always fit the parent div:
.banner_holder{
width: 200px;
height: 300px;
position: relative;
outline:1px dotted red;
}
.banner_holder img{
max-width: 100%;
max-height: 100%;
}
EDIT: Sorry, I miss the part where you wrote about 300px cut-off stuff :) MathiasaurusRex's answer is correct.