search
HomeWeb Front-endHTML Tutorialbackground系列之你不知道的background-position_html/css_WEB-ITnose

这是一个有趣的话题

其实我并不确切的平时大家是怎么去应用或者玩转一个属性,一个值。我能肯定的是这些东西都有不少的可玩性。

我今天要聊的 background-position 应该已经被大家玩得色彩斑斓了。尤其是 CSS Sprites 流行的这些年, background-position 基本上是被应用最多的属性之一。

重拾旧趣

我们知道 background-position 是用来指定背景图像的偏移值的,能让一张图从特定的位置开始展现。而 CSS Sprites 就是通过将多个小图拼接成一张大图,然后利用 background-position 来指定需要显示的区域,以此达到合并HTTP请求的预期。

一个足够简单的应用

为了回顾 background-position 的应用,接下来我将会用一个最简单的例子来代入,这里有一张由2个 300*100px 垂直拼接而成的图片作为背景图,如 图0 :

我现在需要 图0 在2个并排的div中分别显示不同的部分:

HTML:

<div class="part1"><!-- 显示图0上半部分 --></div><div class="part2"><!-- 显示图0下半部分 --></div>

于是我写了段简单的CSS,如下:

CSS:

div { width: 300px; height: 100px; background: gray url(../test.png) no-repeat;}.part1 { background-position: 0 0;}.part2 { background-position: 0 -100px;}

很显然我可以得到预期,效果如 图1 :

这就是最典型的 CSS Sprites 使用场景。当然,你可以在线查看这个例子 Demo1 最简单的 background-position 应用 。

默认值

由于 background-position 的默认值是 0% 0% ,那么上述的CSS代码其实可以优化成:

.part2 { background-position: 0 -100px;}

因为 .part1 指定的值是 0 0 ,和默认值相同,所以可以省略。你会发现,对一个属性了解得更多,就更能帮助你写出简洁的代码。

百分比

我并不能确定大家是否使用过 background-position 的百分比,这里就权当大家对此并不甚了解。

试着使用百分比去实现上个例子

我相信肯定有童鞋会这样写:

.part2 { /* background-position: 0 -100px; */ background-position: 0 -50%;}

按照一般的思维,上述两行代码应该是等价的,不是么?在开篇的时候我们就说了背景图 图0 的高度是 200px ,那么 -50% 正好是 -100px 。

不用着急,我们会用实际的例子来验证这个结果,来看 Demo2 检验 background-position 的百分比值 。

结果让人有点忧伤,这和我们的设想有点出入,这是为什么呢?

追本溯源

我们都知道一个百分比值,必然会需要有一个参照尺寸。举个例子来讲,假设我定义一个元素的宽度是 50% ,那么这个元素的具体宽度就是: 包含块宽度 * 50% 。

所以,如果你需要使用百分比作为 background-position 的值,必须清楚它的参照尺寸是什么。

w3c 是这样描述 background-position 比分比值的:

原文:refer to size of background positioning area minus size of background image.

翻译:参照指定背景区域的宽度减去背景图片的宽度

这是什么意思呢?白话一点说: background-position 的百分比值参照的是设置背景的区域减去背景图的尺寸。

再出发

按照这个思路,我们将:

.part2 { background-position: 0 -50%;}

换算一下将会得到:

.part2 { background-position: 0 50px;}

换算过程为:(设置背景的区域高度 - 背景图的高度) * -50%,即:(100 - 200) * -50% = 50px

这就解释了 Demo2 为什么会得到 图2 的效果。但这显然并不是我们想要的,我们预期的效果是 图1 。

根据上述的公式,我们可以逆推预期效果的百分比值是多少:

-100 / (100 - 200) = 100%

所以如果你要使用百分比,那么定义应该是这样的:

.part2 { background-position: 0 100%;}

其结果如 Demo3 正确使用 background-position 百分比

这会终于得到我们的预期效果了,请看 图3

了解了百分比的这个特性后,会帮助我们大大简化某些定义,比如我在 Yo 里面对yo-score 的处理,非常巧妙,有兴趣的童鞋可以自己研究一下,这里不细讲。

另外:需要注意的是百分比值会受 background-size 影响,因为 background-size 可以改变背景图像的大小。

多值

在 CSS3 中,对 background-position 属性进行了扩展,允许接受3到4个参数,用于指定背景图的起始方向和具体位置。

原文:If three or four values are given, then each or represents an offset and must be preceded by a keyword, which specifies from which edge the offset is given.

翻译:如果指定了三个或四个值,那么每个 之前必须有一个关键字,用于指定该方向的偏移量。

当指定3到4个参数时,不接受 center 关键字作为偏移量作为边界,只能使用 top, right, bottom, left 这4个关键字。

多值的意义

在此前,我们使用 background-position 只能让背景图从 top, right, bottom, left, center 这5个边界开始显示,但无法指定任意一个边界的偏移量。

举个例子:我想让一个背景图从右下角偏移 20px

你会发现如果没有多值扩展,你很难轻易做到这件事,除非你能确定容器的宽高永远都是显式定义好的,就算如此,其灵活性也一文不值。

多值的应用

如果利用多值特性,这将变得非常轻松,我们仍使用 图0 作为背景图,来做一个演示。

.demo { width: 400px; height: 400px; background: url(../test.png) no-repeat; background-position: right -300px bottom 20px;}

这会终于得到我们的预期效果了,请看 图4

效果可以查看 Demo4 background-position 边界偏移 。实际上,有了多值之后,我们可以让背景图在任意方位上偏移,你可能会发现,这甚至可以让你的结构写得更简单,嵌套变浅。

写在最后

当你深入了解了每个属性的每个定义,你的CSS世界又会变得和以前不一样。enjoy it.

background系列文章:

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 Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

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

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

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.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft