CSS-背景图片位置 | background-position

简介:

ckground-position 指定背景图片的初始位置。这个初始位置是相对于以background-origin定义的背景位置图层来说的。

/* Keyword values */
background-position: top;
background-position: bottom;
background-position: left;
background-position: right;
background-position: center;

/* <percentage> values */
background-position: 25% 75%;

/* <length> values */
background-position: 0 0;
background-position: 1cm 2cm;
background-position: 10ch 8em;

/* Multiple images */
background-position: 0 0, center;

/* Edge offsets values */
background-position: bottom 10px right 20px;
background-position: right 3em bottom 10px;
background-position: bottom 10px right;
background-position: top right 10px;

/* Global values */
background-position: inherit;
background-position: initial;
background-position: unset;

初始值

0% 0%

适用元素

all elements. It also applies to ::first-letter and ::first-line.

是否是继承属性

no

Percentages

refer to the size of the background positioning area minus size of background image; size refers to the width for horizontal offsets and to the height for vertical offsets

适用媒体

visual

计算值

a list, each item consisting of two keywords representing the origin and two offsets from that origin, each given as an absolute length (if given a <length>), otherwise as a percentage

Animation type

repeatable list of simple list of length, percentage, or calc

正规顺序

the unique non-ambiguous order defined by the formal grammar

语法

background-position属性被指定为一个或多个<position>值,用逗号分隔。

<position>是一个<position>,一到四个距元素边界2D位置的数值。可以给出相对值或绝对值。注意位置可以设置在盒模型外。

1值语法:值可能是:

  • 关键字值center,它是图像的中心。
  • 关键字值中的一个topleftbottomright。这指定了放置物品的边缘。另一个尺寸被设置为50%,因此该项目被放置在指定边缘的中间。
  • a<length><percentage>。这指定相对于左边缘的X坐标,Y坐标设置为50%。

2值语法:一个值定义X,另一个定义Y.每个值可以是:

  • 关键字值中的一个topleftbottomright。如果left或者right在这里给出,那么这个定义了X,而另一个给定的值定义了Y.如果top或者bottom给定了,那么这个定义了Y而另一个定义了X.
  • <length><percentage>。如果另一个值是leftor right,那么这个值定义Y,相对于上边缘。如果另一个值是topor bottom,那么这个值定义X,相对于左边缘。如果两个值是<length><percentage>值,那么第一个定义X和第二个Y.

请注意:

  • 如果一个值是topbottom,那么另一个值可能不是topbottom
  • 如果一个值是leftright,那么另一个值可能不是leftright

这意味着“ top top"left right”是无效的。

正式语法

<position>#where 
<position> = [[ left | center | right | top | bottom | <length-percentage> ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] | [ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ]]
where 
<length-percentage> = <length> | <percentage>

示例

这三个示例中的每一个都使用CSS background创建一个包含星形图像的黄色矩形元素。在每个例子中,星形图像处于不同的位置。另外,第三个例子说明了如何为一个元素中的两个不同的背景图像指定位置。

HTML

<div class="exampleone">Example One</div>
<div class="exampletwo">Example Two</div>
<div class="examplethree">Example Three</div>

CSS

/* Shared among all DIVS */
div {
  background-color: #FFEE99;
  background-repeat: no-repeat;
  width: 300px;
  height: 80px;
  margin-bottom: 12px;
}

/* background-position examples */

/* These examples use the background shorthand CSS property */
.exampleone {
  background: url("https://mdn.mozillademos.org/files/11987/startransparent.gif") #FFEE99 2.5cm bottom no-repeat;
}
.exampletwo {
  background: url("https://mdn.mozillademos.org/files/11987/startransparent.gif") #FFEE99 3em 50% no-repeat;
}

/*
Multiple background images: Each image is matched with the
corresponding position style, from first specified to last.
*/
.examplethree {
  background-image:    url("https://mdn.mozillademos.org/files/11987/startransparent.gif"), 
                       url("https://mdn.mozillademos.org/files/7693/catfront.png");
  background-position: 0px 0px,
                       center;
}

结果

规范

Specification

Status

Comment

CSS Backgrounds and Borders Module Level 3The definition of 'background-position' in that specification.

Candidate Recommendation

Adds support for multiple backgrounds and the four-value syntax. Modifies the percentage definition to match implementations.

CSS Level 2 (Revision 1)The definition of 'background-position' in that specification.

Recommendation

Allows for keyword values and <length> and <percentage> values to be mixed.

CSS Level 1The definition of 'background-position' in that specification.

Recommendation

Initial definition.

浏览器兼容性

Feature

Chrome

Edge

Firefox

Internet Explorer

Opera

Safari

Basic Support

1.0

12

1.0

4

3.5

1.0

Multiple backgrounds

1.0

12

3.6

9.0

10.5

1.3

Four-value syntax (support for offsets from any edge)

25.0

12

13.0

9.0

10.5

7.0

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Multiple backgrounds

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Four-value syntax (support for offsets from any edge)

(Yes)

(Yes)

(Yes)

13.0

(Yes)

(Yes)

(Yes)

相关文章
|
前端开发
CSS:实现background-image背景图片全屏铺满自适应
CSS:实现background-image背景图片全屏铺满自适应
2072 0
|
5月前
|
前端开发 安全 容器
CSS基本知识之复合选择器、元素显示模式、背景图片位置,精灵图(二)
CSS基本知识之复合选择器、元素显示模式、背景图片位置,精灵图
|
5月前
|
前端开发
CSS基本知识之复合选择器、元素显示模式、背景图片位置,精灵图(一)
CSS基本知识之复合选择器、元素显示模式、背景图片位置,精灵图
|
6月前
|
前端开发
使用css样式淡化body背景图片的实现方法
使用css样式淡化body背景图片的实现方法
|
6月前
|
前端开发
css里面设置body背景图片满屏
css里面设置body背景图片满屏
|
9月前
|
前端开发 容器
3种CSS实现背景图片全屏铺满自适应的方式
3种CSS实现背景图片全屏铺满自适应的方式
809 0
|
10月前
|
前端开发
CSS背景图片自适应大小
CSS背景图片自适应大小
78 0
|
前端开发
零基础CSS入门教程(10)——背景图片平铺不平铺
如果是repeat他会分成四块一样的也是默认平铺,repeat-x平行平铺两个,repeat-y上下平铺两个,而no-repeat他是取消平铺,只显示一个*/如果是repeat他会分成四块一样的也是默认平铺,repeat-x平行平铺两个,repeat-y上下平铺两个,而no-repeat他是取消平铺,只显示一个。什么叫平铺呢,就是图像在窗口上是如何铺设的,是横向一张一张的铺,还是不平铺只展示一张图片,还是纵向的一张一张的铺。我们本小节学习了图片的平铺和不平铺,我们以后对图片设置花样多了许多方法。
零基础CSS入门教程(10)——背景图片平铺不平铺
|
前端开发
零基础CSS入门教程(9)——背景颜色和背景图片
我们在images文件夹下放置一张图片,然后通过background-image来设置背景图。本小结我们学习了背景图片设置,背景颜色的设置,这两个设置再css里面很常用,我们要熟练掌握。如果觉得背景色不够好看,还可以找一张图片作为背景图,就想我们电脑或者手机的壁纸一般。我们前几小节学习了如何设置字体格式,我们这一小节学习一下如何设置背景颜色和图片。
零基础CSS入门教程(9)——背景颜色和背景图片
|
Web App开发 前端开发 Android开发