background-position-CSS
Contents
background-position
Experimente
A propriedade
CSS
background-position
define a posição inicial, relativa a posição de fundo na camada definido por
background-origin
, para cada background image definido.
A fonte deste exemplo interativo está alocado em um repositório GitHub. Se você pudesse contribuir com os exemplos de projetos interativos , por favor “clone”
https://github.com/mdn/interactive-examples
e envie-nos um pull request.
Sintaxe
/* Valores chave */ background-position: top; background-position: bottom; background-position: left; background-position: right; background-position: center; /* Valores <percentuais> */ background-position: 25% 75%; /* valores <espessura> */ background-position: 0 0; background-position: 1cm 2cm; background-position: 10ch 8em; /* Multiplas imagens */ background-position: 0 0, center; /* Valores de cantos relativos */ background-position: bottom 10px right 20px; background-position: right 3em bottom 10px; background-position: bottom 10px right; background-position: top right 10px; /* Valores globais */ background-position: inherit; background-position: initial; background-position: unset;
A propriedade background-position
é especificada como um determinado numero de valores de
<position>
, separados por espaços.
You are reading: background-position-CSS
Values
-
<position>
- A
<position>
(en-US). A position defines an x/y coordinate, to place an item relative to the edges of an element’s box. It can be defined using one or two values. If two values are used, the first value represents the horizontal position and the second represents the vertical position. If only one value is specified, the second value is assumed to be
center
. -
1-value syntax: the value may be:
- The keyword value
center
, which centers the image. - One of the keyword values
top
,left
,bottom
,right
. This specifies an edge against which to place the item. The other dimension is then set to 50%, so the item is placed in the middle of the edge specified. - A
<length>
(en-US)or
<percentage>
(en-US). This specifies the X coordinate relative to the left edge, with the Y coordinate set to 50%.
2-value syntax: one value defines X and the other defines Y. Each value may be:
- One of the keyword values
top
,left
,bottom
,right
. Ifleft
orright
are given here, then this defines X and the other given value defines Y. Iftop
orbottom
are given, then this defines Y and the other value defines X. - A
<length>
(en-US)or
<percentage>
(en-US). If the other value is
left
orright
, then this value defines Y, relative to the top edge. If the other value istop
orbottom
, then this value defines X, relative to the left edge. If both values are<length>
or<percentage>
values, then the first defines X and the second Y.
Note that:
- If one value is
top
orbottom
, then the other value may not betop
orbottom
. - If one value is
left
orright
, then the other value may not beleft
orright
.
This means, e.g., that
top top
andleft right
are not valid. - The keyword value
Formal syntax
<bg-position>
# (en-US)
where
<bg-position> =
[ (en-US)
[ (en-US)
left| (en-US)
center| (en-US)
right| (en-US)
top| (en-US)
bottom| (en-US)
<length-percentage>
] (en-US)
| (en-US)
[ (en-US)
left| (en-US)
center| (en-US)
right| (en-US)
<length-percentage>
] (en-US)
[ (en-US)
top| (en-US)
center| (en-US)
bottom| (en-US)
<length-percentage>
] (en-US)
| (en-US)
[ (en-US)
center| (en-US)
[ (en-US)
left| (en-US)
right] (en-US)
<length-percentage>
? (en-US)
] (en-US)
&& (en-US)
[ (en-US)
center| (en-US)
[ (en-US)
top| (en-US)
bottom] (en-US)
<length-percentage>
? (en-US)
] (en-US)
] (en-US)
where
<length-percentage> =
<length> (en-US)
| (en-US)
<percentage> (en-US)
Examples
Each of these three examples uses the
background
property to create a yellow, rectangular element containing a star image. In each example, the star is in a different position. The third example illustrates how to specify positions for two different background images within one element.
HTML
<div class="exampleone">Example One</div> <div class="exampletwo">Example Two</div> <div class="examplethree">Example Three</div>
See more: Screen Mirroring Explained: How Screen Mirroring Works
CSS
/* Shared among all <div>s */ div { background-color: #FFEE99; background-repeat: no-repeat; width: 300px; height: 80px; margin-bottom: 12px; } /* These examples use the `background` shorthand 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, 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; }
Result
Specifications
Specification | Status | Comment |
---|---|---|
CSS Backgrounds and Borders Module Level 3 |
Candidata a Recomendação | Adds support for multiple backgrounds and the four-value syntax. Modifies the percentage definition to match implementations. |
CSS Level 2 (Revision 1) |
Recomendação | Allows for keyword values and
and
values to be mixed. |
CSS Level 1 |
Recomendação | Initial definition. |
Initial value |
0% 0% |
---|---|
Aplica-se a | all elements. It also applies to
and
. |
Inherited |
não |
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 |
Computed value |
as each of the properties of the shorthand:
|
Animation type | repeatable list of simple list of length, percentage, or calc |
BCD tables only load in the browser
with JavaScript enabled. Enable JavaScript to view data.
See more: Classroom Winter Crafts That We Want to Try Right Now-WeAreTeachers
Quantum CSS notes
- Gecko has a bug meaning that
background-position
can’t betransitioned
between two values containing different numbers of
<position>
(en-US)values, for example
background-position: 10px 10px;
andbackground-position: 20px 20px, 30px 30px;
(seebug 1390446
). Firefox’s new parallel CSS engine (also known as
Quantum CSS
or
Stylo
, planned for release in Firefox 57) fixes this.
See also
-
background-position-x
(en-US) -
background-position-y
(en-US) -
background-position-inline
-
background-position-block
-
Using multiple backgrounds
-
transform-origin
(en-US)
Chuyên mục: App