Home >Web Front-end >CSS Tutorial >How to make images scale proportionally with css
Using max-width and max-height or object-fit attributes, you can scale the image proportionally in CSS and maintain the aspect ratio of the image.
How to use CSS to scale images proportionally
Get straight to the point and answer the question:
To make the image scale proportionally in CSS, you can use the max-width
and max-height
properties, or use the object-fit
property.
Detailed answer:
1. Use the max-width
and max-height
properties
<code class="css">img { max-width: 100%; max-height: 100%; }</code>
Using this method, the image will automatically scale proportionally to fit the size of the container while maintaining the aspect ratio of the image.
2. Use the object-fit
attribute
<code class="css">img { object-fit: contain; }</code>
object-fit
The attribute has the following values:
Note:
object-fit
attribute requires browser support and may not be available in older browsers . When using CSS to scale images, you need to pay attention to the following points:
The above is the detailed content of How to make images scale proportionally with css. For more information, please follow other related articles on the PHP Chinese website!