How can I scale up a image as much as possible without changing the picture ratio. So its either 100% width or 100% height
<div class="container"><img src=""></div>
You can use CSS background which is recommended in this case. You can read more about CSS backgrounds here.
div {
width: 200px;
height: 200px;
border: 1px solid red;
background: url(https://picsum.photos/200) center/cover no-repeat;
}
<div></div>
You can use the image tag (as you did) and set the width & height property to 100%.
div {
width: 200px;
height: 200px;
border: 1px solid red;
}
img {
width: 100%;
height: 100%;
}
<div>
<img src="https://picsum.photos/200/300" />
</div>
P.S.: You can set the height to auto so the image becomes responsive.
img{
object-fit: contain;
}
use this for the image.
You can use object-fit: cover, the image will be sized to maintain its aspect ratio while filling the element’s entire content box
.img {
width: 100%;
height: 100%;
object-fit: cover;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css" rel="stylesheet"/>
<div>
<image
src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/close-up-of-cat-wearing-sunglasses-while-sitting-royalty-free-image-1571755145.jpg"
alt="test"
class="img"
/>
</div>
Just use object-fit: contain; for the image
.image-container {
width: 300px;
height: 300px;
border: 3px solid black;
padding: 15px;
}
.image-container img {
width: 100%;
height: 100%;
object-fit: contain;
}
<div class="image-container" style="height: 500px;">
<img src="https://www.w3schools.com/html/pic_trulli.jpg" alt="">
</div>
<div class="image-container" style="width: 500px;">
<img src="https://www.w3schools.com/html/img_girl.jpg" alt="">
</div>
Related
I have a portrait picture (original size width :2500px height : 4500px) and I want to place this image in a modal div . the modal has width: 100vw and height: 100vh .this is the structure:
<div class="modal">
<div class="image-wrapper>
<img src="..." />
</div>
</div>
my question is how can I make this portrait image responsive in all width and height and also keep the aspect ratio. what should be the CSS like ? what should be the width and the height of image-wrapper and also the img (image) ?
.modal {
width: 200px;
height: 300px;
border: 1px solid red;
}
.modal--horizont {
width: 300px;
height: 200px;
}
.image-wrapper {
width: inherit;
height: inherit;
display: flex;
justify-content: center;
align-items: center;
}
img {
width: 100%;
height: 100%;
display: block;
object-fit: contain;
}
<div class="modal">
<div class="image-wrapper">
<img src="https://picsum.photos/450/250" />
</div>
</div>
<div class="modal modal--horizont">
<div class="image-wrapper">
<img src="https://picsum.photos/450/250" />
</div>
</div>
I have an image with dimensions of height: 751 and width of 244 px and I need to put this image to div with dimensions of 132x132 and I want this picture to have background-size: contain and not to be stretched. I have tried with img-fluid, but nothing happens. What am I doing wrong?
.thumbs-container {
width: 132px;
height: 132px;
}
.item1 {
background-size:contain;
}
<div class="thumbs-container col-2">
<img
class="item1 img-thumbnail"
src="http://via.placeholder.com/244x751"
/>
</div>
.thumbs-container {
width: 132px;
height: 132px;
}
.item1 {
background: url('http://via.placeholder.com/244x751')no-repeat center center;
width: 132px;
height: 132px;
background-size:cover;
}
<div class="thumbs-container col-2">
<div class="item1"></div>
</div>
Set height of .item1 to 100%
.item1 {
height: 100%;
}
background-size:contain only works for images set as background in css.
Just remove the img tag and set your background in css instead like this:
HTML
<div class="thumbs-container col-2"></div>
CSS
.thumbs-container {
width: 132px;
height: 132px;
background: url('http://via.placeholder.com/244x751') no-repeat center/contain;
}
I have an image that is fluid (.img-fluid) and I've set a height of 245px and it's width is always 100%. When I check my page on small devices, the height works just fine, but the image looks squeezed in it's width. My image must always looks correctly, not squeezed.
<div class="product-image">
<img src="https://static.reverb-assets.com/assets/homepage/open-graph-
7c32c7390769b2d0ab9366d1547b0a829b1c44f2eb991a9b0d1b3f59d0e62bf4.jpg"
alt="Foto del producto" class="img-fluid">
</div>
.product-image {
width: 100%;
height: 245px;
}
.product-image .img-fluid {
max-width: 100%;
height: 100%;
}
Looks like object-fit CSS property should fit your needs.
Reference.
img {
width: 100%;
height: 245px;
}
.img--cover {
object-fit: cover;
}
.img--contain {
object-fit: contain;
}
<div>
<img src="https://www.talentz.net/wp-content/uploads/CG10-2.jpg" alt="Foto del producto" class="img--cover">
</div>
<div>
<img src="https://www.talentz.net/wp-content/uploads/CG10-2.jpg" alt="Foto del producto" class="img--contain">
</div>
Use on both sides of the image an auto value
<div class="product-image">
<img src="https://via.placeholder.com/150x245.png" alt="Foto" class="img-fluid">
</div>
.product-image {
width: 100%;
height: 245px;
}
.product-image .img-fluid {
display: block;
width: auto;
max-width: 100%;
height: auto;
max-height: 100%;
}
how to add the black bars on my div with 400x400 div size.
if the picture is portrait, it would add black bars on left and right,
and when the picture is widescreen the black bars will be added on top and bottom.
<div>
<img src="image.png" class="img-responsive" />
</div>
I have no sleep for almost 2 days because of this.
I don't understand on this.
this is the sample picture:
please help me
EDIT1:
hello, can you check my html... it should be perfect align, but the picture have different size the alignment broken... look at this.. http://codepen.io/anon/pen/MaorKq
EDIT2
It's already but when the width higher the image pass through
check here: http://codepen.io/anon/pen/EVXMee
Is this what you're looking for?
I've made a pure CSS solution that should work up to IE8. :)
Also, it should work with any width/height.
.image-background {
background: #000;
display: table;
width: 100%;
height: 100%;
position: relative;
}
.image-container {
display: table-cell;
vertical-align: middle;
text-align: center;
margin: auto;
max-height: 100%;
max-width: 100%;
}
img {
display: block;
max-height: 100%;
max-width: 100%;
margin: auto;
}
http://codepen.io/anon/pen/vNZJwe
Good luck!
Your div is 400*400, but your image is small, When you have used img-responsive that means img max-size : 100 %. But here you want to see min-size:100%.
You can try below code
<img src="image.png" class="img-responsive full-width" /> <!--add class full-width-->
in your css file
.full-width{
width:100%;
}
Use below CSS,JS and HTML markup to achive this:
<style>
.img-container {
width: 400px;
height: 400px;
background-color: black;
position:relative;
}
.img-container img {
position:absolute;
top: 50%;
left: 50%;
}
</style>
<script>
$(document).ready(function() {
debugger;
var img = $('.img-container img');
img.css('margin-top', - ($('.img-container img').height() / 2) + 'px');
img.css('margin-left', - ($('.img-container img').width() / 2) + 'px');
});
</script>
<div class="img-container">
<img src="http://www.gstatic.com/webp/gallery/1.jpg" class="img-responsive" />
</div>
Here is a stripped-down version of what #Himechi90 wrote.
If you only need one image instead of a row, basically all you need is:
.image-full-view {
float: none;
padding: 0;
background: #000;
display: table;
width: 100%;
height: 100%;
}
.image-full-view img {
display: block;
max-height: 80vh;
max-width: 100%;
margin: auto;
}
<div class="image-full-view">
<img src="https://media.usfcvast.org/images/cvast-arches/projects/la_mancha/la_motilla_del_azuer/la_motilla_del_azuer_aerial.jpg" alt="" />
</div>
You can use object-fit: contain;
css:
.image-container {
overflow: hidden;
height: 16rem;
background-color: black;
width: auto;
}
.image-item {
object-fit: contain;
height: 100%;
width: 100%;
}
html:
<div class="image-container">
<img class="image-item" src="images/Screenshot(1).jpg">
</div>
Is this a Chrome bug?
Here's the HTML:
<div><img src="test.png"></div>
Here's the CSS:
* { box-sizing: border-box; }
div { height: 200px; padding: 75px 0 60px; }
img { max-height: 100%; }
Expected result: The img should have a height of 65px.
Result in Chrome (v. 27.0.1453.116) on Mac OS (v. 10.6.8): The img has height of 135px and "bleeds" into the parent div's padding. If I change the padding of the div to 50px 0, oddly it renders properly.
Play with this in a codepen: http://codepen.io/anon/pen/jhbKz
Screenshots:
First block has padding of 50px 0. Second block has padding of 75px 0 60px.
Firefox (correct result)
Chrome (wrong?)
Try adding a container to your Image with width and height of 100%. This will give you the same output on chrome and FF.
<div class="b">
<div style='height:100%;width:100%;'>
<img src="some image url">
</div>
</div>
I cannot explain why this fix works currently, but I myself am trying to reason with it.
Years later, the issue seems to have spread to Firefox.
Pav's workaround did not work for me, maybe because I have "a" not "div". The only way in my case was to display as table:
<div style="display: table;">
<a style="height: 100px; display: table-cell;" href="#">
<img style="max-height: 100%; width: auto;" src="some image url">
</a></div>
An additional benefit of "table" is that vertical-align: middle; can be used to center the image (or other content) vertically.
You can achieve it using position: absolute for your image.
<div class="wrap">
<img class="img" src="https://cdn.photoswipe.com/photoswipe-demo-images/photos/1/img-2500.jpg" alt="">
</div>
body {
height: 100vh;
}
.wrap {
display: inline-block;
position: relative;
max-height: 500px;
height: 100%;
width: 100%;
background: rgba(0,0,0,0.1);
}
.img {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
height: 100%;
}
jsfiddle