How do you stretch an image to fill a div while keeping the image's aspect-ratio?
If you're able to set the image as a background-image
then you can do something like this, which will crop the image without stretching it:
<div style="background-image: url(...); background-size: cover; width: 100%; height: 100%;"></div>
If you need to stick with an <img>
tag, then as of 2019, you can now use the object-fit
css property that accepts the following values:fill | contain | cover | none | scale-down
See https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
As an example, you could have a container that holds an image:
<div class="container">
<img src="" class="container_img" />
</div>
.container {
height: 50px;
width: 50%;
}
.container_img {
height: 100%;
width: 100%;
object-fit: cover;
}
How to stretch the background image to fill a div
Add
background-size:100% 100%;
to your css underneath background-image.
You can also specify exact dimensions, i.e.:
background-size: 30px 40px;
Here: JSFiddle
Stretch Image to Fit 100% of Div Height and Width
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%;
}
CSS Image size, how to fill, but not stretch?
You can use the css property object-fit
. ("sets how the content of a replaced element, such as an <img>
or <video>
, should be resized to fit its container.")
.cover {
object-fit: cover;
width: 50px;
height: 100px;
}
<img src="http://i.stack.imgur.com/2OrtT.jpg" class="cover" width="242" height="363" />
How do I auto-resize an image to fit a 'div' container?
Do not apply an explicit width or height to the image tag. Instead, give it:
max-width:100%;
max-height:100%;
Also, height: auto;
if you want to specify a width only.
Example: http://jsfiddle.net/xwrvxser/1/
img {
max-width: 100%;
max-height: 100%;
}
.portrait {
height: 80px;
width: 30px;
}
.landscape {
height: 30px;
width: 80px;
}
.square {
height: 75px;
width: 75px;
}
Portrait Div
<div class="portrait">
<img src="http://i.stack.imgur.com/xkF9Q.jpg">
</div>
Landscape Div
<div class="landscape">
<img src="http://i.stack.imgur.com/xkF9Q.jpg">
</div>
Square Div
<div class="square">
<img src="http://i.stack.imgur.com/xkF9Q.jpg">
</div>
Getting image to stretch a div
Add overflow:auto;
to .product1
Related Topics
Century Gothic Font Usablility Question
Checkboxes Not Displaying Chrome - Work in Other Browsers
Validating Check Boxes in HTML
Z-Index Ie8 Bug on Generated Content with: After
How to Use Font Awesome in Own CSS
Webkit Scrollbar CSS, Always a White Box in Corner
How to Use Pseudo Selectors in Material-Ui
CSS 2.1 Spec: 8.3.1 Collapsing Margins: Cannot Properly Interpret Special Case: Clarification Sought
Two Divs Inside Div. How to Auto-Fill The Space of Parent Div by Second Div
How to Install Bootstrap in Laravel 8
Object Oriented CSS: Should It Matter
Align Text Baseline with a Button in CSS
How Is The Caret on Twitter Bootstrap Constructed
Bootstrap 4 Card/Panel with Image Left of Header and Title
CSS Checkboxes & Radio Buttons When Input Is Inside Label
Does The Order of CSS Stylesheet Definitions Matter
Way to Add Custom Class When Using Ngx-Bootstrap Modalservice
Why Can't an <Ul> (With Absolute Position) Inside a <Li> (With Relative Position) Auto Size