A detailed look at CSS3 backgrounds including the three key boxes - content-box, padding-box,border-box. The presentation also explores new values - such as space, round, contain and cover. Also covered are three new properties - background-origin, background-clip and background-size. Finally, the presentation explores shorthand CSS3 backgrounds and multiple background images. Not for the faint-hearted!
8. First letâs start with a simple
container with some content
inside. Although we cannot see it,
there is an invisible box around
the content called the
content-box.
19. We can change this default
position using the background-
position property.
p { background-position: 10px 10px; }
20. In CSS2.1, we can use
two values to determine the
position of the background image
in relation to the element.
1 2
p { background-position: 10px 10px; }
21. The first value represents
the horizontal position.
Horizontal axis
p { background-position: 10% 10%; }
22. The second value represents
the vertical axis.
Vertical axis
p { background-position: 10% 10%; }
23. In CSS3, we can specify up to
four values for background-
position.
1 2 3 4
p { background-position:
left 10px top 15px; }
24. The first two values represent
the horizontal axis.
Horizontal axis
p { background-position:
left 10px top 15px; }
25. The second two values
represent the vertical axis.
Vertical axis
p { background-position:
left 10px top 15px; }
26. This is a powerful addition,
as it means we can position
images using length values in
relation to any of the four
corners of elements, not just
the top left corner.
36. By default, images will repeat
along both the x and y axis -
starting from the top left
corner of the padding-box.
37. Even though the background
images start in the top left
corner of the padding-box,
they will repeat outwards in all
directions, including into the
border-box area.
38.
39. In CSS2.1, we could change
the repeat behaviour using
four different keywords.
⢠repeat
⢠repeat-x
⢠repeat-y
⢠no-repeat
45. In CSS3, we can now define
background-repeat using two
values instead of one.
div { background-repeat:
repeat repeat; }
46. The first of these two values
represents the horizontal axis.
Horizontal axis
div { background-repeat:
repeat repeat; }
47. The second value represents the
vertical axis.
Vertical axis
div { background-repeat:
repeat repeat; }
48. If we use one value only, the
browsers will interpret this as a
double value. This allows the
background-repeat value to be
backwards compatible.
Browser doubles value
div { background-repeat:
repeat [repeat]; }
50. CSS3 also allows us to use
two new values with the
background-repeat property:
⢠space
⢠round
51. Browser space round
Firefox 3.6, 4 No No
Safari 4, 5 No No
Chrome 10 No No
IE 6, 7, 8 No No
IE 9 No No
Opera 10, 11 Yes Yes
52. The space value sets the image
to repeat as often as will fit within
the background area and then the
images are spaced out to fill the
area. The first and last images
touch the edges of the area.
p { background-repeat: space; }
54. The round value sets the image
to repeat as often as will fit within
the background area. If it doesn't
fit a whole number of times, it is
rescaled so that it does.
p { background-repeat: round; }
69. The background-clip
property is used to determine
where and if background images
are clipped (or cut off) inside the
background area.
div { background-clip: padding-box; }
70. Browser background-clip
Firefox 3.6 (See notes next slide)
Firefox 4 Yes
Safari 4, 5 -webkit-background-clip
Chrome 10 Yes
IE 6, 7, 8 No
IE 9 Yes
Opera 10, 11 Yes
71. Firefox versions 1.0 to 3.6
support an older syntax:
⢠border (instead of border-box)
⢠padding (instead of padding-box)
Note: they do not support content or the
newer content-box values.
Thanks to Louis Lazaris - impressivewebs.com for picking up this FireFox support issue
72. We can clip our background
images via the background-
clip property using three
possible values:
⢠content-box
⢠padding-box
⢠border-box
82. The length value sets the height
and width of the background
image. The first value sets the
width, the second value sets
the height.
Width Height
div { background-size: 10px 20px; }
83. If only one length value
is given, the second value is
set to the âinitial valueâ of auto.
Initial value of auto used
div { background-size: 10px [auto]; }
85. The percentage value sets the
height and width to a percent
of the parent element. The first
value sets the width, the second
value sets the height.
Width Height
div { background-size: 20% 40%; }
86. Like length values, if only one
percentage value is given,
the second is set to the âinitial
valueâ of auto.
Initial value
div { background-size: 10% [auto]; }
88. The contain value will scale the
image (while keeping its aspect
ratio) so that the entire image can
fit inside the background area.
div { background-size: contain; }
91. The cover value will scale the
image (while keeping itâs aspect
ratio) to the smallest size that
will completely covered the
background area.
div { background-size: cover; }
99. CSS3 allows us to place multiple,
comma-separated values into
any background property.
p { Comma after each value
background-image:
url(01.gif),
url(02.gif),
url(03.gif);
}
100. An example of three background
images inside one element -
written in longhand
background-image 1
p
{
background-image: url(01.gif), url(02.gif), url(03.gif) ;
background-position: left top, 50% 30%, 10px 100px ;
background-size: auto, 10% auto, auto ;
background-repeat: no-repeat, repeat, repeat-y ;
background-attachment: scroll, scroll, scroll ;
background-origin: padding-box, padding-box, border-box ;
background-clip: border-box, padding-box, border-box ;
}
101. An example of three background
images inside one element -
written in longhand
background-image 2
p
{
background-image: url(01.gif), url(02.gif), url(03.gif) ;
background-position: left top, 50% 30%, 10px 100px ;
background-size: auto, 10% auto, auto ;
background-repeat: no-repeat, repeat, repeat-y ;
background-attachment: scroll, scroll, scroll ;
background-origin: padding-box, padding-box, border-box ;
background-clip: border-box, padding-box, border-box ;
}
102. An example of three background
images inside one element -
written in longhand
background-image 3
p
{
background-image: url(01.gif), url(02.gif), url(03.gif) ;
background-position: left top, 50% 30%, 10px 100px ;
background-size: auto, 10% auto, auto ;
background-repeat: no-repeat, repeat, repeat-y ;
background-attachment: scroll, scroll, scroll ;
background-origin: padding-box, padding-box, border-box ;
background-clip: border-box, padding-box, border-box ;
}
103. Each of the images is sized,
positioned, and tiled according
to the corresponding value in
the other background
properties.
104. If a property doesn't have enough
comma-separated values to
match the number of layers, the
browser must calculate its used
value by repeating the list of
values until there are enough.
105. Listed values repeated when not
defined by author.
p
{
background-image: url(1.gif), url(2.gif), url(3.gif), url(4.gif) ;
background-size: auto, 10% auto, auto, auto ;
background-repeat: no-repeat, repeat, [no-repeat], [repeat] ;
}
107. The background images are then
displayed in layers - one on top of
each other. The first image in
the list is the layer closest to
the user, the next one is painted
behind the first, and so on.
121. If you are going to use
shorthand properties, you need
to understand initial values,
as these values can affect which
rules will be applied and those
that may not!
122. If we use a shorthand
background property, we
donât need to define all the
background properties in order
for the rule to work. For example:
Only one background-property defined
p { background: yellow; }
123. However, browsers will add
initial values for any value that
we donât define.
Initial values added by browser
p { background: [none] [0% 0%] / [auto auto]
[repeat repeat] [scroll] [padding-box]
[border-box] yellow; }
125. Why does all this matter?
You may try to specify two
background rules for the same
element and then wonder why
one of the rules doesnât work.
Same element
p { background: url(a.gif); }
P { background: yellow; }
126. p {
background: url(a.gif) [0% 0%] /
[auto auto] [repeat repeat]
[scroll] [padding-box]
[border-box] [transparent];
}
The first rule has a background-image specified.
All other initial values are added by the browser
p {
background: [none] [0% 0%] /
[auto auto] [repeat repeat]
[scroll] [padding-box]
[border-box] yellow;
}
127. p {
background: url(a.gif) [0% 0%] /
[auto auto] [repeat repeat]
[scroll] [padding-box]
[border-box] [transparent];
}
As the second rule has no background-image defined,
the browser has set the value to an initial value of none
p {
background: [none] [0% 0%] /
[auto auto] [repeat repeat]
[scroll] [padding-box]
[border-box] yellow;
}
128. p {
background: url(a.gif) [0% 0%] /
[auto auto] [repeat repeat]
[scroll] [padding-box]
[border-box] [transparent];
}
The second rule wins, as it is written last.
So, no background image is applied
p {
background: [none] [0% 0%] /
[auto auto] [repeat repeat]
[scroll] [padding-box]
[border-box] yellow;
}
130. All eight background properties
can be combined into a single
shorthand background
property.
131. Multiple shorthand backgrounds
are written in the same way as
single shorthand backgrounds -
with commaâs separating each
background value.
132. A simple example of three
background images inside one
element - written in shorthand.
p background-image 1
{
background:
url(01.gif) no-repeat,
url(02.gif) repeat left bottom,
url(03.gif) repeat-y 10px 5px;
}
133. A simple example of three
background images inside one
element - written in shorthand.
p
background-image 2
{
background:
url(01.gif) no-repeat,
url(02.gif) repeat left bottom,
url(03.gif) repeat-y 10px 5px;
}
134. A simple example of three
background images inside one
element - written in shorthand.
p
{
background: background-image 3
url(01.gif) no-repeat,
url(02.gif) repeat left bottom,
url(03.gif) repeat-y 10px 5px;
}
135. Just as with the longhand
version, each of the background
images is displayed as a layer -
one on top of each other.
142. If a background-color value is
assigned to any other layer apart
from the final layer, the entire
rule will not be displayed.
143. A background-color assigned
to the final layer only.
p Final layer with background-color assigned
{
background:
url(01.gif) no-repeat,
url(02.gif) repeat left bottom,
url(03.gif) repeat-y 10px 5px yellow;
}
144. It may be safer to add the
background-color as a
separate declaration using the
background-color
property.
145. A background-color
declared separately.
p background-color
{
background:
url(01.gif) no-repeat,
url(02.gif) repeat left bottom,
url(03.gif) repeat-y 10px 5px;
background-color: yellow;
}
147. If you are writing more complex
shorthand rules, you have to
be aware of some browser
quirks and catches.
148. Theoretically, the correct syntax
for all background properties is:
p
{
background:
[background-image] background-image
[background-position]
[/ background-size]
[background-repeat]
[background-attachment]
[background-origin]
[background-clip]
[background-color];
149. Theoretically, the correct syntax
for all background properties is:
p
{
background:
[background-image]
[background-position] Background-position
[/ background-size]
[background-repeat]
[background-attachment]
[background-origin]
[background-clip]
[background-color];
150. Theoretically, the correct syntax
for all background properties is:
p
{
background:
[background-image]
[background-position]
[/ background-size] Background-size
[background-repeat]
[background-attachment]
[background-origin]
[background-clip]
[background-color];
151. Theoretically, the correct syntax
for all background properties is:
p
{
background:
[background-image]
[background-position]
[/ background-size]
[background-repeat] Background-repeat
[background-attachment]
[background-origin]
[background-clip]
[background-color];
152. Theoretically, the correct syntax
for all background properties is:
p
{
background:
[background-image]
[background-position]
[/ background-size]
[background-repeat]
[background-attachment] Background-attachment
[background-origin]
[background-clip]
[background-color];
153. Theoretically, the correct syntax
for all background properties is:
p
{
background:
[background-image]
[background-position]
[/ background-size]
[background-repeat]
[background-attachment]
[background-origin] Background-origin
[background-clip]
[background-color];
154. Theoretically, the correct syntax
for all background properties is:
p
{
background:
[background-image]
[background-position]
[/ background-size]
[background-repeat]
[background-attachment]
[background-origin]
[background-clip] Background-clip
[background-color];
155. Theoretically, the correct syntax
for all background properties is:
p
{
background:
[background-image]
[background-position]
[/ background-size]
[background-repeat]
[background-attachment]
[background-origin]
[background-clip]
[background-color]; Background-color
156. You may have noticed that there
is a forward slash between
background-position and
background-size.
157. A forward slash separating
position and size.
p
{
background:
[background-image]
[background-position] Slash
[/ background-size]
[background-repeat]
[background-attachment]
[background-origin]
[background-clip]
[background-color];
158. Safari 5, firefox 4, and Chrome 10
all have two problems with rules
written out in full like this.
159. Problem 1:
These browsers ignore the entire
declaration when a forward slash
or background-size values are
included.
160. Problem 2:
These browsers ignore the entire
declaration when two box values
(background-clip and
background-origin) are
included. Only one value can be
included.
161. So, at this time, shorthand
rules are fine for less complex
declarations, but if you want to
include all seven properties, it
may be better to use longhand
for the present.
163. Keep in mind that gradients are a
type of generated image. They
can be used in place of the
url() value.
164. This means you can include
gradients within multiple
backgrounds.
Linear gradient
p
{
background:
url(demo.jpg) no-repeat 0 0,
linear-gradient(left, blue, green);
}
169. The downside of this is that
we cannot add multiple vendor-
specific properties into one
declaration as all browsers would
then ignore the entire
declaration.
170. This means we have to rewrite
the background property for each
vendor-specific property. Any
other background images have
to be included in each
declaration.
171. For example:
p
{
background:
url(demo.jpg) no-repeat 0 0,
-moz-linear-gradient(left, blue, green);
background:
url(demo.jpg) no-repeat 0 0,
-o-linear-gradient(left, blue, green);
background:
url(demo.jpg) no-repeat 0 0,
-webkit-gradient(linear, left top, right top,
from(blue), to(green));
}