Do you really use the background property?

introduction

In daily front-end development, background or background image processing is often required. But most front-end ers don't really know the correct way to use the background. After studying this chapter, I believe you can solve 99% of background processing problems.

One: easy to use

  • Background color and background image can appear together

    div {
        width: 300px;
        height: 300px;
        background-color: red;
        background-image: url(./imgs/1.jpg);
        background-repeat: no-repeat; 
       }
    复制代码

page display

36252fa48d59ca199c750a08481dccaf.jpeg

One: background-repeat

background-repeat determines how the background image is tiled

attribute value

background-repeat:repeat (默认值)
background-repeat:no-repeat (不平铺)
background-repeat:repeat-x (水平方向平铺)
background-repeat:repeat-y (垂直方向平铺)

复制代码

1. repeat

By default, if the background image cannot cover the entire box, the system will tile it horizontally and vertically until it covers the entire box

div {
  width: 300px;
  height: 300px;
  background-color: red;
  background-image: url(./imgs/1.jpg);
  background-repeat: repeat; 
 }
复制代码

8acdd1d071acdf08ce174a9b97d06477.jpeg

2.repeat-x

If the background image cannot cover the horizontal direction of the box, it will be tiled in the horizontal direction until the horizontal direction of the box is covered.

div {
  width: 300px;
  height: 300px;
  background-color: red;
  background-image: url(./imgs/1.jpg);
  background-repeat: repeat-x; 
 }
复制代码

7b3711814a4ca8d027f05b380206d813.jpeg

3.repeat-y

If the background image cannot cover the vertical direction of the box, it will be tiled in the vertical direction until the vertical direction of the box is covered.

div {
  width: 300px;
  height: 300px;
  background-color: red;
  background-image: url(./imgs/1.jpg);
  background-repeat: repeat-y; 
 }
复制代码

06e6298a83aba7b5e1f66f10e998ef9e.jpeg

Two: background-position

background-positiont determines the position of the background image in the box area. Its orientation is determined by horizontal and vertical

1.px settings

px determines how much the background image is offset by the specified px in the horizontal and vertical directions of the box.

div {
  width: 300px;
  height: 300px;
  background-color: red;
  background-image: url(./imgs/1.jpg);
  background-repeat: no-repeat;
  background-position: 100px 100px; 
 }
复制代码

1bf7184ee6ebe310f45c289467dfbdd3.jpeg

2. Location words

Horizontal direction: left, center, right

Vertical direction: top, center, bottom

Center the background image

div {
  width: 300px;
  height: 300px;
  background-color: red;
  background-image: url(./imgs/1.jpg);
  background-repeat: no-repeat;
  background-position: center center; 
 }
 
复制代码

564ab6158744223a57b3c460486d91f1.jpeg

3. Proportion

Horizontal direction = |box width - picture width| * scale

vertical direction = |box height - image height| * scale

The proportional offset position in the horizontal and vertical directions is calculated according to the above formula.

The following is the setting method for the centered display of the background image

div {
  width: 300px;
  height: 300px;
  background-color: red;
  background-image: url(./imgs/1.jpg);
  background-repeat: no-repeat;
  background-position: 50% 50%; 
 }
 
复制代码

38b2dd2a12ddb93ba6431990d75b0516.jpeg

Three: background abbreviation

Background has various styles such as background color, background image, tiled or not, etc. In order to simplify the css style, the system provides a shorthand for the background

background:color url repeat postion
复制代码
  • The abbreviation of the background can arbitrarily omit several of these attributes

Three: background-size

background-size determines the specific size of the background image displayed in the box, and the attribute value needs to set the width and height of the background image at the same time.

1. Specific px

  • Directly specify the width and height of the background image

  • There is a background image distortion problem when setting width and height: We all know that each picture has its own original pixels. If we directly specify its width and height every time, the width and height of the picture will be directly compressed to the specified pixels, and the picture will be deformed problem, which greatly affects the user experience.

    div {
        width: 300px;
        height: 300px;
        background-color: red;
        background-image: url(./imgs/1.jpg);
        background-repeat: no-repeat;
        background-size: 200px 50px;
       }
    复制代码

992d616aed0be175811fac30b2ad320c.jpeg

2. Percentage

  • Percentages are relative to the width and height of the box

  • The percentage also has the problem of background image deformation

    div {
        width: 300px;
        height: 300px;
        background-color: red;
        background-image: url(./imgs/1.jpg);
        background-repeat: no-repeat;
        background-size: 50% 10%;
       }
    复制代码

a1f21eeeb9a28b1ba6a8b371d5184e4b.jpeg

3.auto

  1. If the width is a specific value and the height is set to auto, the height of the background image will be proportionally stretched according to the width value

  2. If the height is a specific value and the width is set to auto, the width of the background image will be proportionally stretched according to the height value

  3. If both width and height are set to auto, use the width and height of the original background image directly

As shown in the figure below, the height of the image is stretched proportionally with the width, and there is no image deformation problem.

div {
      width: 300px;
      height: 300px;
      background-color: red;
      background-image: url(./imgs/1.jpg);
      background-repeat: no-repeat;
      background-size: 200px auto;
     }
     
     
复制代码

ed7be8af23114c187cb4ab1f0426ebb3.jpeg

4.cover

cover means covering in English, so its meaning is to require the background image to cover the entire box.

rule

  1. Select the smaller width and height of the background image

  2. Select the smaller side of the background image as a reference, and zoom in or out the background image until the smaller side of the background image just fills the box, and the larger side of the background image will also fill the box.

features

  • The ratio of width and height is stretched or reduced to fill the entire box, and the width and height must fill the box at the same time

  • The picture is not deformed

    div {
        width: 300px;
        height: 300px;
        background-color: red;
        background-image: url(./imgs/1.jpg);
        background-repeat: no-repeat;
        background-size:cover;
       }
    复制代码

a120e179b717a2e54a82cc030bf45a5d.jpeg

5.contain

contain, the width and height of the background image must meet one of the covering boxes. When the width and height of the image are smaller than the box, the image will be stretched proportionally. If the width or height of the image is greater than or equal to the width or height of the box, the stretching will stop.

rule

  1. Select the larger width and height of the background image

  2. Select the larger side of the background image as a reference, and zoom in or out of the background image until the larger side of the background image just fills the box. Ignore whether the smaller side of the background image is filled.

features

  • The ratio of width and height is stretched or shrunk, and the width or height should be the same as the width and height of the box.

  • The picture is not deformed

    div {
        width: 300px;
        height: 300px;
        background-color: red;
        background-image: url(./imgs/1.jpg);
        background-repeat: no-repeat;
        background-size: contain;
       }
       
    复制代码

四:background-origin

background-origin determines where the background image starts to render from the box

1. background-origin: padding-box (default value)

Starting from the padding position of the box

.box{
  margin: 20px auto;
  width: 300px;
  height: 300px;
  padding: 50px;
  border: 50px solid gold;
  background-color: red;
  background-image: url(./imgs/1.jpg);
  background-repeat: no-repeat;
  background-origin: padding-box;
}


复制代码

fec2f68420e868b987ab4a93632bc9b5.jpeg

2.background-origin: content-box

Start at the content area position of the box

.box{
  margin: 20px auto;
  width: 300px;
  height: 300px;
  padding: 50px;
  border: 50px solid gold;
  background-color: red;
  background-image: url(./imgs/1.jpg);
  background-repeat: no-repeat;
  background-origin: content-box;
}


复制代码

3f0171da3fc46d6b7a9f6558cd87f347.jpeg

3.background-origin: border-box

Start at the border area position of the box

Five: background-clip

background-clip determines where the background color starts to render from the box

  1. background-clip: border-box (from box margin)

  2. background-clip: content-box (starting from box content)

  3. background-clip: padding-box (default, start from box padding)

Guess you like

Origin blog.csdn.net/weixin_42981560/article/details/130355788