search
HomeWeb Front-endCSS TutorialBackground-size in CSS3 implements several effects with customizable background image sizes (code examples)

Goal of this article:

1. Master several setting methods of background-size background image size

Question:

1. To achieve the following effects, use pure DIV CSS , must use background-size

Background-size in CSS3 implements several effects with customizable background image sizes (code examples)

Additional instructions:

1. The background image size itself is 500*300 size

2.div The width of the container is 600*300. #3 They are all stored here. The materials are

2. Create index.html and write the architecture. How to analyze the architecture?

Idea analysis:

Background-size in CSS3 implements several effects with customizable background image sizes (code examples)1. The target consists of 5 divs. The background image of each div is the same, but the background image is presented in a different way.

2. Each div has a title

Based on the analysis, we came up with the following code

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>背景图片大小background-size演示</title>
</head>
<body>
    <div class="container">
        <h4 id="原样显示背景图片">原样显示背景图片</h4>
        <div class="demo bg1"></div>
        <h4 id="宽和-高显示背景图片">300宽和100高显示背景图片</h4>
        <div class="demo bg2"></div>
        <h4 id="宽和-高显示背景图片">80%宽和60%高显示背景图片</h4>
        <div class="demo bg3"></div>
        <h4 id="背景图片填充整个容器">背景图片填充整个容器</h4>
        <div class="demo bg4"></div>
        <h4 id="背景图片扩充到容器的一边即可">背景图片扩充到容器的一边即可</h4>
        <div class="demo bg5"></div>
    </div>
</body>
</html>

3. Write the style, create a css folder, create a new index.css in it, how to write the style inside, the following is the analysis idea

Idea analysis:

1. .container *

Idea analysis

1. In order to set the common styles of all elements in the container, we can write these common codes .container * style

So add the following code to index.css:

.container *{
    padding:0;
    margin:0;
}

2, .demo

Idea analysis:

1 . According to the requirements, we learned that the width is 600 and the height is 300, so the code is converted into width: 600px; height: 300px; the background image is Aisha, and it is not repeated, so background: url(../images/Background-size in CSS3 implements several effects with customizable background image sizes (code examples)) no-repeat ;With border, so


border: 1px solid #999;To be centered, so margin: 0 auto 10px auto;

So add the following code to index.css:

.demo {
    background: url(../images/Background-size in CSS3 implements several effects with customizable background image sizes (code examples)) no-repeat;
    width: 600px;
    height: 300px;
    border: 1px solid #999;
    margin: 0 auto 10px auto;
}

3. Title h4


Idea analysis:

1. The text is required to be centered, so the converted code is text-align: center;,

so index Add the following code to .css:

h4{
    text-align: center;
}

4. 5 different display methods

Idea analysis:

1. The first way is The default method is to display the background image as big as it is

2. The second method is to customize the fixed width and height display, which requires the background image to be displayed according to the width of 300 and the height of 100

3. The third way is to display it in a custom percentage, which requires 80% width and 60% height of the background image container div (that is, the final width of the background image =600*80%=480, height=300*60%=180)

4. The fourth way is to let the background image fill the entire div. What should be noted here is that if If the width of the background image 5. The fifth way is to scale the background image proportionally until one side fills one side of the container

One thing to note here is that except for the second and third types, the pictures are not expanded in equal proportions. The other ones expand the background images in equal proportions and stop expanding until the conditions are met.

So add the following code to index.css:

.bg1 {
    background-size: auto;
}

.bg2 {
    background-size: 300px 100px;
}

.bg3 {
    background-size: 80% 60%;
}

.bg4 {
    background-size: cover;
}

.bg5 {
    background-size: contain;
}
So far, the entire content of index.css is as follows:

.container *{
    padding:0;
    margin: 0;
}
.demo {
    background: url(../images/Background-size in CSS3 implements several effects with customizable background image sizes (code examples)) no-repeat;
    width: 600px;
    height: 300px;
    border: 1px solid #999;
    margin: 0 auto 10px auto;
}
h4{
    text-align: center;
}
.bg1 {
    background-size: auto;
}

.bg2 {
    background-size: 300px 100px;
}

.bg3 {
    background-size: 80% 60%;
}

.bg4 {
    background-size: cover;
}

.bg5 {
    background-size: contain;
}

Then introduce index.css into index. The running effect of

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>背景图片大小background-size演示</title>
    <link rel="stylesheet" href="css/index.css" />
</head>
<body>
    <div class="container">
        <h4 id="原样显示背景图片">原样显示背景图片</h4>
        <div class="demo bg1"></div>
        <h4 id="宽和-高显示背景图片">300宽和100高显示背景图片</h4>
        <div class="demo bg2"></div>
        <h4 id="宽和-高显示背景图片">80%宽和60%高显示背景图片</h4>
        <div class="demo bg3"></div>
        <h4 id="背景图片填充整个容器">背景图片填充整个容器</h4>
        <div class="demo bg4"></div>
        <h4 id="背景图片扩充到容器的一边即可">背景图片扩充到容器的一边即可</h4>
        <div class="demo bg5"></div>
    </div>
</body>
</html>
in html is as follows:

So far, we have achieved all the requirements

Summary:

Background-size in CSS3 implements several effects with customizable background image sizes (code examples)1. background-size is mainly used to set the size of the background image. The syntax format is as follows:

background-size:auto|fixed width and height|percentage width and height|cover|contain

The above is the detailed content of Background-size in CSS3 implements several effects with customizable background image sizes (code examples). For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Weekly Platform News: Web Apps in Galaxy Store, Tappable Stories, CSS SubgridWeekly Platform News: Web Apps in Galaxy Store, Tappable Stories, CSS SubgridApr 14, 2025 am 11:20 AM

In this week's roundup: Firefox gains locksmith-like powers, Samsung's Galaxy Store starts supporting Progressive Web Apps, CSS Subgrid is shipping in Firefox

Weekly Platform News: Internet Explorer Mode, Speed Report in Search Console, Restricting Notification PromptsWeekly Platform News: Internet Explorer Mode, Speed Report in Search Console, Restricting Notification PromptsApr 14, 2025 am 11:15 AM

In this week's roundup: Internet Explorer finds its way into Edge, Google Search Console touts a new speed report, and Firefox gives Facebook's notification

The Power (and Fun) of Scope with CSS Custom PropertiesThe Power (and Fun) of Scope with CSS Custom PropertiesApr 14, 2025 am 11:11 AM

You’re probably already at least a little familiar with CSS variables. If not, here’s a two-second overview: they are really called custom properties, you set

We Are ProgrammersWe Are ProgrammersApr 14, 2025 am 11:04 AM

Building websites is programming. Writing HTML and CSS is programming. I am a programmer, and if you're here, reading CSS-Tricks, chances are you're a

How Do You Remove Unused CSS From a Site?How Do You Remove Unused CSS From a Site?Apr 14, 2025 am 10:59 AM

Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

An Introduction to the Picture-in-Picture Web APIAn Introduction to the Picture-in-Picture Web APIApr 14, 2025 am 10:57 AM

Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

Ways to Organize and Prepare Images for a Blur-Up Effect Using GatsbyWays to Organize and Prepare Images for a Blur-Up Effect Using GatsbyApr 14, 2025 am 10:56 AM

Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

Oh Hey, Padding Percentage is Based on the Parent Element's WidthOh Hey, Padding Percentage is Based on the Parent Element's WidthApr 14, 2025 am 10:55 AM

I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools