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

Recommend

Vue realizes web online chat function

This article example shares the specific code of ...

mysql creates root users and ordinary users and modify and delete functions

Method 1: Use the SET PASSWORD command mysql -u r...

A brief talk about JavaScript variable promotion

Table of contents Preface 1. What variables are p...

MYSQL transaction tutorial Yii2.0 merchant withdrawal function

Preface I am a PHP programmer who started out as ...

SQL implementation of LeetCode (178. Score ranking)

[LeetCode] 178.Rank Scores Write a SQL query to r...

HTML checkbox Click the description text to select/uncheck the state

In web development, since the checkbox is small an...

Some conclusions on developing mobile websites

The mobile version of the website should at least...

How to deploy DoNetCore to Alibaba Cloud with Nginx

Basic environment configuration Please purchase t...

Steps to set up HTTPS website based on Nginx

Table of contents Preface: Encryption algorithm: ...

Sample code for JS album image shaking and enlarging display effect

The previous article introduced how to achieve a ...

MYSQL slow query and log settings and testing

1. Introduction By enabling the slow query log, M...

jQuery realizes the scrolling effect of table row data

This article example shares the specific code of ...

Notes on Using Textarea

Why mention textarea specifically? Because the tex...

Tutorial on building an FTP server in Ubuntu 16.04

Ubuntu 16.04 builds FTP server Install ftp Instal...