While CSS3 can't "resize" images directly, you can use media query and responsive design principles to make images look resized in the browser.
The concept is to make the image responsive to the viewport size, ensuring that the image does not exceed the edge. If the viewport height is greater than the width, the maximum width must be defined; if the viewport width is greater than the height, the maximum height must be defined.
The following are some sample code and links to more related articles. Please note that the use of img.ri:empty
is a structural pseudo-class that only matches elements without child elements (the image should not have any child elements). This is a CSS3 selector, so IE8 and below will not be able to resolve the declaration. empty
@media screen and (orientation: portrait) { img.ri { max-width: 90%; } } @media screen and (orientation: landscape) { img.ri { max-height: 90%; } }
properties. background-size
and width
properties. However, to maintain aspect ratio and prevent deformation, only one of the properties should be specified and the other should be set to height
. For example: auto
img { width: 100%; height: auto; }
What is the role of the
attribute in CSS image resizing?object-fit
attribute in the , object-fit
, fill
, contain
, cover
, and none
. The scale-down
value stretches the image to fit the container, which may deform the image if the aspect ratio of the image is different from that of the container. The fill
value resizes the image to fit the container while maintaining its aspect ratio, which may leave some uncovered space in the container. The contain
value resizes the image to cover the entire container while maintaining its aspect ratio, which may crop certain parts of the image. The cover
value does not resize the image at all. The value of none
behaves like scale-down
or contain
, taking the smaller one. none
attribute, whose value is max-width
. This allows the image to shrink when needed, but never to be enlarged beyond its original size. For example: 100%
img { max-width: 100%; height: auto; }
Resize the background image in CSS and use the background-size
attribute. It has several possible values, including auto
, cover
, contain
and specific sizes. The auto
value resizes the background image to its actual size. The cover
value resizes the background image to cover the entire container, even if it has to stretch the image or crop a little from its edge. The contain
value resizes the background image to ensure that the image is fully visible. A specific size can be given to adjust the background image to a certain width and height. For example:
@media screen and (orientation: portrait) { img.ri { max-width: 90%; } } @media screen and (orientation: landscape) { img.ri { max-height: 90%; } }
This code will resize the background image to cover the entire body of the web page.
Adjusting the image to a specific width and height in CSS requires the use of width
and height
properties as well as specific values. However, this may deform the image if the aspect ratio of the image is different from the specified width and height ratio. To prevent deformation, only one of the properties should be specified and the other should be set to auto
. For example:
img { width: 100%; height: auto; }
This code adjusts the image to 500 pixels wide while maintaining its aspect ratio.
Center the image after resizing it in CSS. You can use the margin
attribute, whose value is auto
. This works when the image is displayed as a block-level element and can be specified using the display
attribute and block
value. For example:
img { max-width: 100%; height: auto; }
This code centers the image horizontally within its container.
Resize the image in CSS without stretching it, and its aspect ratio needs to be maintained. This can be done by specifying only one of the width
and height
properties and setting the other property to auto
. For example:
body { background-image: url("image.jpg"); background-size: cover; }
This code resizes the image to fit the width of its container while maintaining its aspect ratio, thus preventing stretching.
Resize the image in CSS and maintain its aspect ratio, you need to specify only one of the width
and height
properties and set the other property to auto
. For example:
img { width: 500px; height: auto; }
This code resizes the image to fit the width of its container while maintaining its aspect ratio.
Resize the image in CSS to fit the screen, use vw
(viewport width) units as the value of the width
attribute, and vh
(viewport height) units as the value of the height
attribute. For example:
img { display: block; margin: auto; }
This code will resize the image to fit the entire screen regardless of the screen size.
Resize the image in CSS to fill its container, you need to use the object-fit
attribute, whose value is fill
. For example:
@media screen and (orientation: portrait) { img.ri { max-width: 90%; } } @media screen and (orientation: landscape) { img.ri { max-height: 90%; } }
This code will stretch the image to fit its container, which may deform the image if the aspect ratio of the image is different from that of the container. To prevent deformation, you can use the object-fit
attribute, whose value is cover
, which will resize the image to cover the entire container while maintaining its aspect ratio, even if it has to crop certain parts of the image.
The above is the detailed content of How to 'Resize' Images with CSS. For more information, please follow other related articles on the PHP Chinese website!