Detailed explanation of the new background properties in CSS3

Detailed explanation of the new background properties in CSS3

Previously, we knew several attributes of background in CSS: color, image, repeat, attachment, position. These are commonly used in CSS, so what are the new attributes added in CSS3? Look down:

**

New CSS3 properties: background-clip, background-origin, background-size

1.background-clip

The clipping property of the background is to start drawing from the specified position.

**

**

2.background-origin

The background-origin property specifies the area where the background image is positioned.

** The above two property values ​​are border-box, padding-box, content-box (it is recommended to have a sufficient understanding of the box model, so just look at these 3 property values ​​here and it will be clear), or see the following picture:

insert image description here

Background-clip is equivalent to starting clipping at any of these three positions to achieve the corresponding effect. See the code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    #div1 {
      border: 20px dashed blue;
      padding:40px;
      background: red;
    }

    #div2{
      border: 20px dashed blue;
      padding:40px;
      background: red;
      background-clip: padding-box;
    }

    #div3{
      border: 20px dashed blue;
      padding:40px;
      background: red;
      background-clip: content-box;
    }
  </style>
</head>
<body>

<p>No background clipping (border-box not defined):</p>
<div id="div1">
  <h2>Lorem Ipsum Dolor</h2>
  <p>I just sat there, and I felt so uncomfortable, and I was so tired.</p>
</div>

<p>background-clip: padding-box:</p>
<div id="div2">
  <h2>Lorem Ipsum Dolor</h2>
  <p>I just sat there, and I felt so uncomfortable, and I was so tired.</p>
</div>

<p>background-clip: content-box:</p>
<div id="div3">
  <h2>Lorem Ipsum Dolor</h2>
  <p>I just sat there, and I felt so uncomfortable, and I was so tired.</p>
</div>
</body>
</html>

Effect picture:

insert image description hereinsert image description here

And background-origin is the area where the background is displayed. See the code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    div
    {
      border:1px solid black;
      padding:35px;
      background-image:url('1 (5).jpg');/*If the image cannot be displayed, you need to change it yourself*/
      background-repeat:no-repeat;
      background-position:left;
    }
    #div1 {
      background-origin: border-box;/*Setting padding-box has the same effect, you can try it yourself*/
    }
    #div2 {
      background-origin: content-box;
    }
  </style>
</head>
<body>
<p>Relative position of the background image's bounding box</p>
<div id="div1">
  <h2>Lorem Ipsum Dolor</h2>
  <p>I just sat there, and I felt so uncomfortable, and I was so tired.</p>
</div>
<p>The background image is positioned relative to the content box</p>
<div id="div2">
  <h2>Lorem Ipsum Dolor</h2>
  <p>I just sat there, and I felt so uncomfortable, and I was so tired.</p>
</div>
</body>
</html>

Effect:

insert image description here

**

3.background-size

background-size specifies the size of the background image. Before CSS3, the size of a background image was determined by the actual size of the image. The background-size property in CSS3 allows us to resize background images in different environments. You can specify the size in pixels or percentage. The size you specify is a percentage relative to the width and height of the parent element.

Its attribute values:

①.length sets the height and width of the background image. The first value sets the width, the second the height. If only one value is given, the second is set to auto.

②.lpercentage will calculate the percentage relative to the background positioning area. The first value sets the width, the second the height. If only one value is given, the second is set to "auto"

③.cover will maintain the aspect ratio of the image and scale the image to the minimum size that will completely cover the background positioning area.

④.contain will maintain the aspect ratio of the image and scale the image to the maximum size that will fit within the background positioning area.

Summarize

The above is the new background properties of CSS3 that I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

<<:  Implementation of MYSQL (telephone number, ID card) data desensitization

>>:  Vue3 list interface data display details

Blog    

Recommend

Detailed Linux installation tutorial

(Win7 system) VMware virtual machine installation...

Detailed steps for setting up host Nginx + Docker WordPress Mysql

environment Linux 3.10.0-693.el7.x86_64 Docker ve...

MySQL 5.7 installation and configuration tutorial under CentOS7 64 bit

Installation environment: CentOS7 64-bit MINI ver...

Implementing long shadow of text in less in CSS3

This article mainly introduces how to implement l...

How to change the system language of centos7 to simplified Chinese

illustrate When you install the system yourself, ...

Alibaba Cloud Server Ubuntu Configuration Tutorial

Since Alibaba Cloud's import of custom Ubuntu...

Use of Linux usermod command

1. Command Introduction The usermod (user modify)...

Analysis of MySQL crash recovery based on Redo Log and Undo Log

Table of contents MySQL crash recovery process 1....

Example code for configuring monitoring items and aggregated graphics in Zabbix

1. Install Zabbix Agent to monitor the local mach...

MySQL multi-instance configuration solution

1.1 What is MySQL multi-instance? Simply put, MyS...

Why the disk space is not released after deleting data in MySQL

Table of contents Problem Description Solution Pr...

Detailed explanation of uniapp painless token refresh method

When the front-end requests the interface, it is ...