css set background size

王林
Release: 2023-05-21 12:10:07
Original
7192 people have browsed it

CSS is a language used for web design and layout, which can achieve various styles and effects, including the setting of background size. Background size refers to the size and display mode of the background image when the web page is loaded. Setting the background size properly can not only improve the beauty of the web page, but also improve the user experience. Next, we will explain in detail how to use CSS to set the background size.

1. Use the background-size attribute

The background-size attribute is used to set the background size. It can specify the size and display mode of the background image. There are three commonly used values: cover, contain and px. Specifically, they represent respectively:

  1. cover: Make the background image always cover the entire background area, but if the image is smaller than the background area, the image will be automatically stretched or enlarged so that it covers the entire background area. background area.
  2. contain: Make the background image appear centered in the background area as much as possible without stretching, while retaining its original size.
  3. px: Directly specify the size of the background image, which can be a fixed value, a percentage, or the keyword auto, where auto represents the original size.

Below we use several examples to demonstrate how to use the background-size attribute.

  1. cover
<!DOCTYPE html>
<html>
<head>
<style>
body {
  background-image: url('https://picsum.photos/id/100/800/400');
  background-size: cover;
}
</style>
</head>
<body>

<h1>Hello World!</h1>

<p>这是一段测试文本。</p>

</body>
</html>
Copy after login

In the above code, we use a random image provided by Lorem Picsum as the background. In CSS, we spread the background image over the entire background area, and the effect is as follows:

css set background size

As you can see, the height of the image is stretched, but the width is also fully The retention makes the entire background look more beautiful.

  1. contain
<!DOCTYPE html>
<html>
<head>
<style>
body {
  background-image: url('https://picsum.photos/id/100/800/400');
  background-size: contain;
}
</style>
</head>
<body>

<h1>Hello World!</h1>

<p>这是一段测试文本。</p>

</body>
</html>
Copy after login

In the above code, we also use a random picture as the background. In CSS, the contain attribute is used so that the image is not stretched, but is displayed as centered as possible in the background area. The effect is as follows:

css set background size

#As you can see from the picture effect, although the picture cannot completely cover the background area, the entire design feels more balanced.

  1. px
<!DOCTYPE html>
<html>
<head>
<style>
body {
  background-image: url('https://picsum.photos/id/100/800/400');
  background-size: 600px;
}
</style>
</head>
<body>

<h1>Hello World!</h1>

<p>这是一段测试文本。</p>

</body>
</html>
Copy after login

In the above code, we also use a random picture as the background. In CSS, a fixed value setting of 600px size is used, and the effect is as follows:

css set background size

As you can see, since the image size is limited to 600px, the entire background area is left blank. . Therefore, when using the px value to set the background size, you need to adjust it according to the specific situation to ensure that the size of the background image can be coordinated with the entire page.

2. Use the background-position attribute

In addition to the background-size attribute, we can also use the background-position attribute to set the background position. This property allows the background image to be centered horizontally or vertically within the background area, with the corresponding offset. The specific usage is as follows:

<!DOCTYPE html>
<html>
<head>
<style>
body {
  background-image: url('https://picsum.photos/id/100/800/400');
  background-size: cover;
  background-position: center;
}
</style>
</head>
<body>

<h1>Hello World!</h1>

<p>这是一段测试文本。</p>

</body>
</html>
Copy after login

In the above code, we set the centered position, and the effect is as follows:

css set background size

As you can see from the renderings, centered The aligned position coincides with the center of the image, and the visual effect is very good.

3. Summary

The above is about how to use CSS to set the background size. In actual development, focusing on user experience and overall page aesthetics should be the most important. Through good background settings, we can make our web pages more attractive and achieve better design effects.

The above is the detailed content of css set background size. 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