search
HomeWeb Front-endHTML Tutorial[Learn one CSS3 property a day] Part 3: background-size_html/css_WEB-ITnose

Excerpted from MDN. Due to time reasons, I couldn't organize my study properly. I will quote it first and then organize it in detail when I have time.

background-size sets the background image size.

/* 关键字 */background-size: coverbackground-size: contain/* 一个值: 这个值指定图片的宽度,图片的高度隐式的为auto */background-size: 50%background-size: 3embackground-size: 12pxbackground-size: auto/* 两个值: 第一个值指定图片的宽度,第二个值指定图片的高度 */background-size: 50% autobackground-size: 3em 25%background-size: auto 6pxbackground-size: auto auto/* 逗号分隔的多个值:设置多重背景 */background-size: auto, auto     /* 不同于background-size: auto auto */background-size: 50%, 25%, 25%background-size: 6px, auto, containbackground-size: inherit

Values

Values ​​specify the size of the background image and cannot be negative. value specifies the percentage of the background image relative to the background positioning area. The background area is set by background-origin, which defaults to the content area and padding of the box model. It can also be set to only the content area, or also include borders. If attachment is fixed, the background area is the browser's visible area (i.e., the viewport), excluding scroll bars. Cannot be a negative value. auto scales the background image in proportion to the background image. cover Scales the background image to completely cover the background area. Parts of the background image may not be visible. contain scales the background image to fit completely into the background area, which may be partially blank.

Bitmaps must have inherent dimensions and proportions, while vector images may have both or only one. Gradient is treated as an image with only intrinsic dimensions or only intrinsic proportions.

Background image size calculation:

If two values ​​of background-size are specified and it is not auto: the background image is rendered at the specified size. contain or cover: retain the inherent proportions and maximize the inclusion or coverage of the background area. If the image has no inherent proportions, it is sized according to the background area. auto or auto auto: If the image has two lengths, use this size. If there is no inherent size or proportion, the size of the background area is used. If there is no intrinsic size but an intrinsic proportion, the effect is the same as contain. If there is a length and ratio, the size is calculated from this length and ratio. If there is a length but no scale, then the length corresponding to the background area is used. One is auto and the other is not auto: If the image has intrinsic proportions, the specified length uses the specified value, and the unspecified length is calculated from the specified value and the intrinsic proportion. If the image does not have an intrinsic proportion, the specified length uses the specified value, and the unspecified length uses the corresponding intrinsic length of the image. If there is no intrinsic length, the corresponding length of the background area is used.

Note that not all browsers support vector graphics that do not have inherent dimensions or proportions. Pay special attention to testing Firefox 7- vs. Firefox 8 to determine whether the differences are acceptable.

Examples

background-size: cover demo and background-size: contain demo opens in a new window, so you can see when the size of the background area changes, contain and What does cover look like? A series of demonstrations: background-size and its association with the background-* attributes illustrates the use of background-size alone and in conjunction with other attributes.

Notes

If you use a gradient as the background and use background-size on it, it is best not to use only one auto, or only specify a width value (for example, background-size: 50%) . Firefox 8 has changed in these two situations, and the current performance of each browser is inconsistent. Not all browsers fully support the CSS3 background-size specification and the CSS3 Image Values ​​gradient specification.

.bar {       width: 50px; height: 100px;       background-image: gradient(...);       /* 不推荐 */       background-size: 25px;       background-size: 50%;       background-size: auto 50px;       background-size: auto 50%;       /* 可行 */       background-size: 25px 50px;       background-size: 50% 50%;}

It is especially not recommended to use gradient px with auto, because it cannot be re-rendered before Firefox 8, and for browsers that do not implement Firefox 8 rendering features, I don’t know how to specify it. The exact size of the background's elements.

Only supported by IE9 and above.

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
The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Is HTML easy to learn for beginners?Is HTML easy to learn for beginners?Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is an example of a starting tag in HTML?What is an example of a starting tag in HTML?Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS's Flexbox layout to achieve centering alignment of dotted line segmentation effect in menu?How to use CSS's Flexbox layout to achieve centering alignment of dotted line segmentation effect in menu?Apr 05, 2025 pm 01:24 PM

How to design the dotted line segmentation effect in the menu? When designing menus, it is usually not difficult to align left and right between the dish name and price, but how about the dotted line or point in the middle...

What HTML elements does the online code editor use to implement code input?What HTML elements does the online code editor use to implement code input?Apr 05, 2025 pm 01:21 PM

HTML Element Analysis in Web Code Editor Many online code editors allow users to enter HTML, CSS, and JavaScript code. Recently, someone proposed...

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
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.