Home > Web Front-end > CSS Tutorial > Guide to CSS image properties: background-size and object-fit

Guide to CSS image properties: background-size and object-fit

PHPz
Release: 2023-10-21 09:09:30
Original
1781 people have browsed it

CSS 图片属性指南:background-size 和 object-fit

CSS Image Property Guide: background-size and object-fit

In front-end development, using images is very common. In order to make images display better on web pages, CSS provides a variety of properties to adjust and control the size and layout of images. Among them, background-size and object-fit are two commonly used properties. They can adjust the size and adaptation of the image as needed. This article explains these two properties in detail and provides specific code examples.

1. background-size attribute

background-size attribute is used to adjust the size of the background image. It can use the following values:

  1. cover: Scale the background image proportionally and completely cover the container. Part of the image may be cropped.
.background {
    background-image: url('img.jpg');
    background-size: cover;
}
Copy after login
  1. contain: Scale the background image proportionally and display it as completely as possible in the container.
.background {
    background-image: url('img.jpg');
    background-size: contain;
}
Copy after login
  1. length: Specify the width and height of the background image.
.background {
    background-image: url('img.jpg');
    background-size: 200px 300px;
}
Copy after login
  1. percentage: Specify the width and height of the background image as a percentage relative to the container.
.background {
    background-image: url('img.jpg');
    background-size: 50% 75%;
}
Copy after login

2. Object-fit attribute

object-fit The attribute is used to adjust the size and adaption of the image in the <img> tag. It can use the following values:

  1. fill: Stretch the image to fill the <img> element, which may cause image distortion.
img {
    object-fit: fill;
}
Copy after login
  1. contain: Scale the image proportionally and display it as completely as possible in the <img> element.
img {
    object-fit: contain;
}
Copy after login
  1. cover: Scale the image proportionally and completely cover the <img> element. Part of the image may be cropped.
img {
    object-fit: cover;
}
Copy after login
  1. none: Do not change the size and adaptation of the image, default value.
img {
    object-fit: none;
}
Copy after login
  1. scale-down: Determine the size and adaptation of the image based on the original size of the image and the size of the container.
img {
    object-fit: scale-down;
}
Copy after login

3. Sample code

In order to better understand and apply the background-size and object-fit properties, the following is Specific code example:

<!DOCTYPE html>
<html>
<head>
    <style>
        .background {
            width: 500px;
            height: 300px;
            background-image: url('image.jpg');
            background-size: cover;
        }
        
        img {
            width: 200px;
            height: 150px;
            object-fit: cover;
        }
    </style>
</head>
<body>
    <div class="background"></div>
    
    <img src="image.jpg" alt="图片">
</body>
</html>
Copy after login

In the above code, the .background class uses the background-size: cover; attribute to scale the background image proportionally and completely cover the container. The <img> tag applies the object-fit: cover; attribute to scale the image proportionally and completely cover the <img> element.

By adjusting the values ​​of related properties, you can customize the size and adaption of the image so that it fits perfectly into your web page layout.

Summary:

By using the background-size and object-fit properties, we can easily adjust and control the size and fit of the image Way. Whether as a background image or as a picture in the <img> element, we can easily achieve customized image display effects. Hopefully this article will help you better understand and apply these two properties.

The above is the detailed content of Guide to CSS image properties: background-size and object-fit. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template