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 themax-width
andmax-height
properties, or use theobject-fit
property.
Detailed answer:
1. Use themax-width
andmax-height
properties
img { max-width: 100%; max-height: 100%; }
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 theobject-fit
attribute
img { object-fit: contain; }
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!