CSS force image resize and keep aspect ratio
img { display: block; max-width:230px; max-height:95px; width: auto; height: auto;}
<p>This image is originally 400x400 pixels, but should get resized by the CSS:</p><img width="400" height="400" src="http://i.stack.imgur.com/aEEkn.png">
Force an image to fit and keep aspect ratio
You can try CSS3 object-fit
, and see browser support tables.
CSS3
object-fit
/object-position
Method of specifying how an object (image or video) should fit inside
its box. object-fit options include "contain" (fit according to aspect
ratio), "fill" (stretches object to fill) and "cover" (overflows box
but maintains ratio), where object-position allows the object to be
repositioned like background-image does.
JSFIDDLE DEMO
.container { width: 200px; /*any size*/ height: 200px; /*any size*/}
.object-fit-cover { width: 100%; height: 100%; object-fit: cover; /*magic*/}
<div class="container"> <img class="object-fit-cover" src="https://i.stack.imgur.com/UJ3pb.jpg"></div>
Force image to maintain dimensions of ::before with padding-bottom aspect ratio hack
The problem is that I was applying styled to the StyledItemWrapper
which wraps the whole card and not just the image, so any padding etc was affecting the layout of the whole card.
The padding and pseudoselector hack for aspect ratio only works if you apply it to a dedicated image container. So, that's what I made:
Item.tsx
const Item: React.FC<Props> = ({ item, handleAddToCart, clickItem }) => (
<StyledItemWrapper
className={item.animation}
onClick={() => {
clickItem(item);
}}
>
<StyledVisualWrapper> // new styled component
<picture>
<img src={item.image} alt={item.title} />
</picture>
</StyledVisualWrapper>
<StyledItemInfo>
<p>{item.title}</p>
<p>{item.description}</p>
<p>${item.price.toFixed(2)}</p>
</StyledItemInfo>
<Button onClick={() => handleAddToCart(item)}>Add To Cart</Button>
</StyledItemWrapper>
);
Item.styles.ts
export const StyledItemWrapper = styled.div`
display: flex;
justify-content: space-between;
flex-direction: column;
border: 1px solid lightblue;
border-radius: 20px;
button {
border-radius: 0 0 20px 20px;
}
`;
export const StyledVisualWrapper = styled.div`
position: relative; // move from StyledItemWrapper to here
max-height: 50%;
&::before {
display: block;
overflow: hidden;
padding-bottom: 100%;
content: "";
}
picture {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
img {
border-radius: 20px 20px 0 0;
width: 100%;
height: 100%;
object-fit: contain;
}
}
`;
How do I fit an image (img) inside a div and keep the aspect ratio?
You will need some JavaScript to prevent cropping if you don't know the dimension of the image at the time you're writing the css.
HTML & JavaScript
<div id="container">
<img src="something.jpg" alt="Sample Image" />
</div>
<script type="text/javascript">
(function() {
var img = document.getElementById('container').firstChild;
img.onload = function() {
if(img.height > img.width) {
img.height = '100%';
img.width = 'auto';
}
};
}());
</script>
CSS
#container {
width: 48px;
height: 48px;
}
#container img {
width: 100%;
}
If you use a JavaScript Library you might want to take advantage of it.
Related Topics
Font-Awesome Icons Not Rendering via the Boostrapcdn
Mailto Links Do Nothing in Chrome But Work in Firefox
Mobile Website "Whatsapp" Button to Send Message to a Specific Number
CSS Margin: 0 Is Not Setting to 0
Can You Style HTML Form Buttons with CSS
Which Browsers Support Data Uris and Since Which Version
CSS Divide Width 100% to 3 Column
Chrome, Safari Ignoring Max-Width in Table
Overflow Behavior After Using CSS3 Transform
Why Do The CSS Width and Height Properties Not Adjust for Padding
Are Nested HTML Comments Possible
What Is The Correct Readonly Attribute Syntax for Input Text Elements
How to Make a Div Tag into a Link
Making an Svg Image Object Clickable with Onclick, Avoiding Absolute Positioning
Twitter Bootstrap: Center Pills