SlideShare ist ein Scribd-Unternehmen logo
1 von 172
Downloaden Sie, um offline zu lesen
CSS3
BACKGROUND
CSS2.1 has five background
properties that we can use to
  control the background of
           elements.
The five background-properties:
          • background-color
          • background-image
         • background-repeat
     • background-attachment
       • background-position
CSS3 gives us three new
background-properties, as well
 as a range of new background
        property values.
All of these are designed to give
      us more control over
      background images.
However, we need to understand
 three important boxes, before
   we look at these new CSS3
     properties and values.
The three
 boxes
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.
content-box


The quick brown
fox jumped over
the lazy web
developer.
If we add some padding to all
sides of the container, we will
   have a new box called a
        padding-box.
padding-box




The quick brown
fox jumped over
the lazy web
developer.
If we add a border to all sides
of the container, we will have
    our third box - called a
         border-box.
border-box




The quick brown
fox jumped over
the lazy web
developer.
These three boxes are used
 to define where background
images are placed, how they
  are sized and where they
      can be cropped.
More on these boxes later…
 But for now, let’s look at
background-position and
   background-repeat.
background-
  position
By default, background images
are placed in the top left corner
      of the padding-box.
Image placed in top left of padding-box
We can change this default
 position using the background-
        position property.




p { background-position: 10px 10px; }
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; }
The first value represents
     the horizontal position.



                     Horizontal axis



p { background-position: 10% 10%; }
The second value represents
        the vertical axis.



                          Vertical axis



p { background-position: 10% 10%; }
In CSS3, we can specify up to
  four values for background-
           position.


     1    2    3   4


p { background-position:
    left 10px top 15px; }
The first two values represent
       the horizontal axis.




    Horizontal axis

p { background-position:
    left 10px top 15px; }
The second two values
    represent the vertical axis.




             Vertical axis

p { background-position:
    left 10px top 15px; }
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.
Positive and
negative values
We can use both positive and
 negative values to determine the
  position of background images.


                         Negative values



p { background-position: -5px -9px; }
Positive values will move
the background image to the
 right and down - inside the
   background area of the
           element.
Positive x value
Positive y value
Negative values will move the
background image to the left and
  up - out of the background
      area of the element.
Negative x value
Negative y value
background-
   repeat
By default, images will repeat
along both the x and y axis -
 starting from the top left
corner of the padding-box.
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.
In CSS2.1, we could change
 the repeat behaviour using
  four different keywords.
          • repeat
         • repeat-x
         • repeat-y
        • no-repeat
repeat
Repeat-x
Repeat-y
no-repeat
repeat repeat?
In CSS3, we can now define
   background-repeat using two
      values instead of one.




div { background-repeat:
    repeat repeat; }
The first of these two values
  represents the horizontal axis.



  Horizontal axis


div { background-repeat:
    repeat repeat; }
The second value represents the
         vertical axis.



         Vertical axis


div { background-repeat:
    repeat repeat; }
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]; }
Using ‘space’ and
     ‘round’
CSS3 also allows us to use
 two new values with the
background-repeat property:
          • space
          • round
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
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; }
Images set to space along the vertical axis
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; }
Images set to round along the vertical axis
Be aware that the background
images may be stretched or
 distorted using this method.
These new values gives us
 much more flexibility when
laying out background-images.
For example, we can now use
   two values to define different
      horizontal and vertical
            behaviour:




div { background-repeat:
    space no-repeat; }
Three new
properties
In CSS3, we can also use three
   entirely new background
           properties.
      • background-origin
        • background-clip
        • background-size
background-
   origin
The background-origin
   property is used to determine
   where background images are
      positioned inside a box.



div { background-origin:
    padding-box; }
We can position our
  background images via
background-origin using
  three possible values:
      • content-box
      • padding-box
       • border-box
Positioned in relation to the content-box
Positioned in relation to the padding-box
Positioned in relation to the border-box
Browser          background-origin
Firefox 3.6, 4   Yes
Safari 4, 5      Yes
Chrome 10        Yes
IE 6, 7, 8       No
IE 9             Yes
Opera 10, 11     Yes
background-
    clip
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; }
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
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
We can clip our background
images via the background-
  clip property using three
     possible values:
        • content-box
        • padding-box
         • border-box
Background-image clipped inside content-box
Background-image clipped inside padding-box
Background-image clipped inside border-box
background-
    size
In CSS2.1, we could apply
background images to elements,
 but we had no way to control
 the size of these background
            images.
However, CSS3 allows us to set
    the size of our background
  images using the background-
          size property.



div { background-size: 10px 20px; }
We can set the size using length
values, percentage values or one
     of two new keywords:
            • contain
              • cover
Browser          background-size
Firefox 3.6, 4   Yes
Safari 4, 5      Yes
Chrome 10        Yes
IE 6, 7, 8       No
IE 9             Yes
Opera 10, 11     Yes
Length values
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; }
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]; }
Percentage values
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%; }
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]; }
The ‘contain’ value
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; }
Large background image scaled down to fit using contain
The ‘cover’ value
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; }
Small background image scaled up to fit using cover
Now, it’s time to talk about
the most exciting part of
   CSS3 backgrounds.
Multiple
backgrounds
With CSS2.1, we could only add
one background image to any
       HTML element.
However, CSS3 allows us to
 add multiple background
  images to any element!
Browser          Multiple backgrounds
Firefox 3.6, 4   Yes
Safari 4, 5      Yes
Chrome 10        Yes
IE 6, 7, 8       No
IE 9             Yes
Opera 10, 11     Yes
Longhand
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);
}
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    ;
}
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    ;
}
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    ;
}
Each of the images is sized,
positioned, and tiled according
to the corresponding value in
    the other background
          properties.
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.
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]  ;
}
Layer upon layer
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.
Layer 3
Layer 2
Layer 1
Background-color
Only one background-color
can be defined for any element!
 This background-color sits
below the background image on
        this final layer.
An example of three background
      images inside one element -
           written in longhand
         Background-color
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    ;
Background-color: yellow
If more that one background-
  color value is assigned, all
   background-colors will be
            ignored.
Shorthand
properties
Some CSS properties can be
   combined into shorthand
properties. This saves us having
  to write multiple declarations.
The background property allows
  us to set all of the individual
 background properties using
             one rule.
CSS2.1 background syntax:


p { background:
    [background-color]
    [background-image]
    [background-repeat]
    [background-attachment]
    [background-position]
;}
The CSS3 background syntax

p { background:
    [background-image]
    [background-position]
    [/ background-size]
    [background-repeat]
    [background-attachment]
    [background-origin]
                              Three new
    [background-clip]       CSS3 properties
    [background-color]
;}
Understanding
 initial values
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!
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; }
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; }
The initial values are:
background-color        transparent
background-image        none
background-repeat       repeat
background-attachment   scroll
background-position     0% 0%
background-origin       padding-box
background-clip         border-box
background-size         auto
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; }
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;
}
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;
}
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;
}
How to write
 shorthand
backgrounds
All eight background properties
can be combined into a single
    shorthand background
            property.
Multiple shorthand backgrounds
 are written in the same way as
single shorthand backgrounds -
with comma’s separating each
      background value.
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;
}
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;
}
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;
}
Just as with the longhand
 version, each of the background
images is displayed as a layer -
    one on top of each other.
Layer 3
Layer 2
Layer 1
Background-color
  and multiple
  backgrounds
Only the lowest layer, called
the ‘final layer’, can be given
   a background-color.
The background-color
sits below the background image
     on this final layer only.
If a background-color value is
 assigned to any other layer apart
   from the final layer, the entire
     rule will not be displayed.
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;
}
It may be safer to add the
  background-color as a
separate declaration using the
     background-color
          property.
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;
}
Some catches with
complex shorthands
If you are writing more complex
  shorthand rules, you have to
  be aware of some browser
      quirks and catches.
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];
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];
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];
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];
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];
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];
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];
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
You may have noticed that there
  is a forward slash between
 background-position and
      background-size.
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];
Safari 5, firefox 4, and Chrome 10
all have two problems with rules
     written out in full like this.
Problem 1:
These browsers ignore the entire
declaration when a forward slash
 or background-size values are
            included.
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.
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.
Multiple
backgrounds and
   gradients
Keep in mind that gradients are a
 type of generated image. They
   can be used in place of the
          url() value.
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);
}
Vendor-specific
extensions and
    multiple
 backgrounds.
What happens when you want to
add vendor-specific properties
  into multiple backgrounds?
Browsers will ignore CSS they
don’t understand. In fact, they will
 ignore the entire declaration.
Vendor-specific properties are
  only understood by individual
browsers - that’s their purpose.
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.
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.
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));
}
Russ Weakley
Max Design

Site: maxdesign.com.au
Twitter: twitter.com/russmaxdesign
Slideshare: slideshare.net/maxdesign
Linkedin: linkedin.com/in/russweakley

Weitere ähnliche Inhalte

Ähnlich wie CSS3 Backgrounds

CSS3: Background And DropShadows:
CSS3: Background And DropShadows:CSS3: Background And DropShadows:
CSS3: Background And DropShadows:Reema
 
The Right Layout Tool for the Job
The Right Layout Tool for the JobThe Right Layout Tool for the Job
The Right Layout Tool for the JobRachel Andrew
 
Css3 Complete Reference
Css3 Complete ReferenceCss3 Complete Reference
Css3 Complete ReferenceEPAM Systems
 
CSS3 : Animation ,Transitions, Gradients
CSS3 : Animation ,Transitions, GradientsCSS3 : Animation ,Transitions, Gradients
CSS3 : Animation ,Transitions, GradientsJatin_23
 
Accelerated Stylesheets
Accelerated StylesheetsAccelerated Stylesheets
Accelerated StylesheetsWynn Netherland
 
basics of css
basics of cssbasics of css
basics of cssPriya Goyal
 
What's new in Responsive Design - Rachel Andrew
What's new in Responsive Design - Rachel AndrewWhat's new in Responsive Design - Rachel Andrew
What's new in Responsive Design - Rachel AndrewWey Wey Web
 
9781305503922t4-200824144338 (2).pdf
9781305503922t4-200824144338 (2).pdf9781305503922t4-200824144338 (2).pdf
9781305503922t4-200824144338 (2).pdfHaboonMohmed
 
Chapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSChapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSDr. Ahmed Al Zaidy
 
Lab#9 graphic and color
Lab#9 graphic and colorLab#9 graphic and color
Lab#9 graphic and colorYaowaluck Promdee
 
DotNetNuke World CSS3
DotNetNuke World CSS3DotNetNuke World CSS3
DotNetNuke World CSS3gravityworksdd
 
CSSConf.asia - Laying out the future
CSSConf.asia - Laying out the futureCSSConf.asia - Laying out the future
CSSConf.asia - Laying out the futureRachel Andrew
 
CSS3 basics for beginners - Imrokraft
CSS3 basics for beginners - ImrokraftCSS3 basics for beginners - Imrokraft
CSS3 basics for beginners - Imrokraftimrokraft
 
CSS for basic learner
CSS for basic learnerCSS for basic learner
CSS for basic learnerYoeung Vibol
 
Start Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJSStart Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJSRachel Andrew
 

Ähnlich wie CSS3 Backgrounds (20)

CSS3: Background And DropShadows:
CSS3: Background And DropShadows:CSS3: Background And DropShadows:
CSS3: Background And DropShadows:
 
Css3
Css3Css3
Css3
 
The Right Layout Tool for the Job
The Right Layout Tool for the JobThe Right Layout Tool for the Job
The Right Layout Tool for the Job
 
Css 3 session1
Css 3 session1Css 3 session1
Css 3 session1
 
Css3 Complete Reference
Css3 Complete ReferenceCss3 Complete Reference
Css3 Complete Reference
 
CSS3 : Animation ,Transitions, Gradients
CSS3 : Animation ,Transitions, GradientsCSS3 : Animation ,Transitions, Gradients
CSS3 : Animation ,Transitions, Gradients
 
Accelerated Stylesheets
Accelerated StylesheetsAccelerated Stylesheets
Accelerated Stylesheets
 
basics of css
basics of cssbasics of css
basics of css
 
What's new in Responsive Design - Rachel Andrew
What's new in Responsive Design - Rachel AndrewWhat's new in Responsive Design - Rachel Andrew
What's new in Responsive Design - Rachel Andrew
 
CSS Box Model
CSS Box ModelCSS Box Model
CSS Box Model
 
9781305503922t4-200824144338 (2).pdf
9781305503922t4-200824144338 (2).pdf9781305503922t4-200824144338 (2).pdf
9781305503922t4-200824144338 (2).pdf
 
Lecture-8.pptx
Lecture-8.pptxLecture-8.pptx
Lecture-8.pptx
 
Chapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSChapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSS
 
Lab#9 graphic and color
Lab#9 graphic and colorLab#9 graphic and color
Lab#9 graphic and color
 
DotNetNuke World CSS3
DotNetNuke World CSS3DotNetNuke World CSS3
DotNetNuke World CSS3
 
Web Development - Lecture 6
Web Development - Lecture 6Web Development - Lecture 6
Web Development - Lecture 6
 
CSSConf.asia - Laying out the future
CSSConf.asia - Laying out the futureCSSConf.asia - Laying out the future
CSSConf.asia - Laying out the future
 
CSS3 basics for beginners - Imrokraft
CSS3 basics for beginners - ImrokraftCSS3 basics for beginners - Imrokraft
CSS3 basics for beginners - Imrokraft
 
CSS for basic learner
CSS for basic learnerCSS for basic learner
CSS for basic learner
 
Start Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJSStart Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJS
 

Mehr von Russ Weakley

Accessible chat windows
Accessible chat windowsAccessible chat windows
Accessible chat windowsRuss Weakley
 
Accessible names & descriptions
Accessible names & descriptionsAccessible names & descriptions
Accessible names & descriptionsRuss Weakley
 
A deep dive into accessible names
A deep dive into accessible namesA deep dive into accessible names
A deep dive into accessible namesRuss Weakley
 
What are accessible names and why should you care?
What are accessible names and why should you care?What are accessible names and why should you care?
What are accessible names and why should you care?Russ Weakley
 
How to build accessible UI components
How to build accessible UI componentsHow to build accessible UI components
How to build accessible UI componentsRuss Weakley
 
What is WCAG 2 and why should we care?
What is WCAG 2 and why should we care?What is WCAG 2 and why should we care?
What is WCAG 2 and why should we care?Russ Weakley
 
Accessible states in Design Systems
Accessible states in Design SystemsAccessible states in Design Systems
Accessible states in Design SystemsRuss Weakley
 
Creating accessible modals and autocompletes
Creating accessible modals and autocompletesCreating accessible modals and autocompletes
Creating accessible modals and autocompletesRuss Weakley
 
Building an accessible progressive loader
Building an accessible progressive loaderBuilding an accessible progressive loader
Building an accessible progressive loaderRuss Weakley
 
Accessibility in Design systems - the pain and glory
Accessibility in Design systems - the pain and gloryAccessibility in Design systems - the pain and glory
Accessibility in Design systems - the pain and gloryRuss Weakley
 
Accessible Inline errors messages
Accessible Inline errors messagesAccessible Inline errors messages
Accessible Inline errors messagesRuss Weakley
 
Accessible Form Hints and Errors
Accessible Form Hints and ErrorsAccessible Form Hints and Errors
Accessible Form Hints and ErrorsRuss Weakley
 
What is accessibility?
What is accessibility?What is accessibility?
What is accessibility?Russ Weakley
 
Accessibility in Pattern Libraries
Accessibility in Pattern LibrariesAccessibility in Pattern Libraries
Accessibility in Pattern LibrariesRuss Weakley
 
Accessibility in pattern libraries
Accessibility in pattern librariesAccessibility in pattern libraries
Accessibility in pattern librariesRuss Weakley
 
Building an accessible auto-complete - #ID24
Building an accessible auto-complete - #ID24Building an accessible auto-complete - #ID24
Building an accessible auto-complete - #ID24Russ Weakley
 
Building an accessible auto-complete
Building an accessible auto-completeBuilding an accessible auto-complete
Building an accessible auto-completeRuss Weakley
 
Creating Acessible floating labels
Creating Acessible floating labelsCreating Acessible floating labels
Creating Acessible floating labelsRuss Weakley
 
Creating an Accessible button dropdown
Creating an Accessible button dropdownCreating an Accessible button dropdown
Creating an Accessible button dropdownRuss Weakley
 
Creating a Simple, Accessible On/Off Switch
Creating a Simple, Accessible On/Off SwitchCreating a Simple, Accessible On/Off Switch
Creating a Simple, Accessible On/Off SwitchRuss Weakley
 

Mehr von Russ Weakley (20)

Accessible chat windows
Accessible chat windowsAccessible chat windows
Accessible chat windows
 
Accessible names & descriptions
Accessible names & descriptionsAccessible names & descriptions
Accessible names & descriptions
 
A deep dive into accessible names
A deep dive into accessible namesA deep dive into accessible names
A deep dive into accessible names
 
What are accessible names and why should you care?
What are accessible names and why should you care?What are accessible names and why should you care?
What are accessible names and why should you care?
 
How to build accessible UI components
How to build accessible UI componentsHow to build accessible UI components
How to build accessible UI components
 
What is WCAG 2 and why should we care?
What is WCAG 2 and why should we care?What is WCAG 2 and why should we care?
What is WCAG 2 and why should we care?
 
Accessible states in Design Systems
Accessible states in Design SystemsAccessible states in Design Systems
Accessible states in Design Systems
 
Creating accessible modals and autocompletes
Creating accessible modals and autocompletesCreating accessible modals and autocompletes
Creating accessible modals and autocompletes
 
Building an accessible progressive loader
Building an accessible progressive loaderBuilding an accessible progressive loader
Building an accessible progressive loader
 
Accessibility in Design systems - the pain and glory
Accessibility in Design systems - the pain and gloryAccessibility in Design systems - the pain and glory
Accessibility in Design systems - the pain and glory
 
Accessible Inline errors messages
Accessible Inline errors messagesAccessible Inline errors messages
Accessible Inline errors messages
 
Accessible Form Hints and Errors
Accessible Form Hints and ErrorsAccessible Form Hints and Errors
Accessible Form Hints and Errors
 
What is accessibility?
What is accessibility?What is accessibility?
What is accessibility?
 
Accessibility in Pattern Libraries
Accessibility in Pattern LibrariesAccessibility in Pattern Libraries
Accessibility in Pattern Libraries
 
Accessibility in pattern libraries
Accessibility in pattern librariesAccessibility in pattern libraries
Accessibility in pattern libraries
 
Building an accessible auto-complete - #ID24
Building an accessible auto-complete - #ID24Building an accessible auto-complete - #ID24
Building an accessible auto-complete - #ID24
 
Building an accessible auto-complete
Building an accessible auto-completeBuilding an accessible auto-complete
Building an accessible auto-complete
 
Creating Acessible floating labels
Creating Acessible floating labelsCreating Acessible floating labels
Creating Acessible floating labels
 
Creating an Accessible button dropdown
Creating an Accessible button dropdownCreating an Accessible button dropdown
Creating an Accessible button dropdown
 
Creating a Simple, Accessible On/Off Switch
Creating a Simple, Accessible On/Off SwitchCreating a Simple, Accessible On/Off Switch
Creating a Simple, Accessible On/Off Switch
 

KĂźrzlich hochgeladen

Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 

KĂźrzlich hochgeladen (20)

Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 

CSS3 Backgrounds

  • 2. CSS2.1 has five background properties that we can use to control the background of elements.
  • 3. The five background-properties: • background-color • background-image • background-repeat • background-attachment • background-position
  • 4. CSS3 gives us three new background-properties, as well as a range of new background property values.
  • 5. All of these are designed to give us more control over background images.
  • 6. However, we need to understand three important boxes, before we look at these new CSS3 properties and values.
  • 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.
  • 9. content-box The quick brown fox jumped over the lazy web developer.
  • 10. If we add some padding to all sides of the container, we will have a new box called a padding-box.
  • 11. padding-box The quick brown fox jumped over the lazy web developer.
  • 12. If we add a border to all sides of the container, we will have our third box - called a border-box.
  • 13. border-box The quick brown fox jumped over the lazy web developer.
  • 14. These three boxes are used to define where background images are placed, how they are sized and where they can be cropped.
  • 15. More on these boxes later… But for now, let’s look at background-position and background-repeat.
  • 17. By default, background images are placed in the top left corner of the padding-box.
  • 18. Image placed in top left of padding-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.
  • 28. We can use both positive and negative values to determine the position of background images. Negative values p { background-position: -5px -9px; }
  • 29. Positive values will move the background image to the right and down - inside the background area of the element.
  • 32. Negative values will move the background image to the left and up - out of the background area of the element.
  • 35. background- repeat
  • 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]; }
  • 49. Using ‘space’ and ‘round’
  • 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; }
  • 53. Images set to space along the vertical axis
  • 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; }
  • 55. Images set to round along the vertical axis
  • 56. Be aware that the background images may be stretched or distorted using this method.
  • 57. These new values gives us much more flexibility when laying out background-images.
  • 58. For example, we can now use two values to define different horizontal and vertical behaviour: div { background-repeat: space no-repeat; }
  • 60. In CSS3, we can also use three entirely new background properties. • background-origin • background-clip • background-size
  • 61. background- origin
  • 62. The background-origin property is used to determine where background images are positioned inside a box. div { background-origin: padding-box; }
  • 63. We can position our background images via background-origin using three possible values: • content-box • padding-box • border-box
  • 64. Positioned in relation to the content-box
  • 65. Positioned in relation to the padding-box
  • 66. Positioned in relation to the border-box
  • 67. Browser background-origin Firefox 3.6, 4 Yes Safari 4, 5 Yes Chrome 10 Yes IE 6, 7, 8 No IE 9 Yes Opera 10, 11 Yes
  • 68. background- clip
  • 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
  • 76. background- size
  • 77. In CSS2.1, we could apply background images to elements, but we had no way to control the size of these background images.
  • 78. However, CSS3 allows us to set the size of our background images using the background- size property. div { background-size: 10px 20px; }
  • 79. We can set the size using length values, percentage values or one of two new keywords: • contain • cover
  • 80. Browser background-size Firefox 3.6, 4 Yes Safari 4, 5 Yes Chrome 10 Yes IE 6, 7, 8 No IE 9 Yes Opera 10, 11 Yes
  • 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; }
  • 89. Large background image scaled down to fit using 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; }
  • 92. Small background image scaled up to fit using cover
  • 93. Now, it’s time to talk about the most exciting part of CSS3 backgrounds.
  • 95. With CSS2.1, we could only add one background image to any HTML element.
  • 96. However, CSS3 allows us to add multiple background images to any element!
  • 97. Browser Multiple backgrounds Firefox 3.6, 4 Yes Safari 4, 5 Yes Chrome 10 Yes IE 6, 7, 8 No IE 9 Yes Opera 10, 11 Yes
  • 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.
  • 112. Only one background-color can be defined for any element! This background-color sits below the background image on this final layer.
  • 113. An example of three background images inside one element - written in longhand Background-color 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 ; Background-color: yellow
  • 114. If more that one background- color value is assigned, all background-colors will be ignored.
  • 116. Some CSS properties can be combined into shorthand properties. This saves us having to write multiple declarations.
  • 117. The background property allows us to set all of the individual background properties using one rule.
  • 118. CSS2.1 background syntax: p { background: [background-color] [background-image] [background-repeat] [background-attachment] [background-position] ;}
  • 119. The CSS3 background syntax p { background: [background-image] [background-position] [/ background-size] [background-repeat] [background-attachment] [background-origin] Three new [background-clip] CSS3 properties [background-color] ;}
  • 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; }
  • 124. The initial values are: background-color transparent background-image none background-repeat repeat background-attachment scroll background-position 0% 0% background-origin padding-box background-clip border-box background-size auto
  • 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; }
  • 129. How to write shorthand backgrounds
  • 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.
  • 139. Background-color and multiple backgrounds
  • 140. Only the lowest layer, called the ‘final layer’, can be given a background-color.
  • 141. The background-color sits below the background image on this final layer only.
  • 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); }
  • 165. Vendor-specific extensions and multiple backgrounds.
  • 166. What happens when you want to add vendor-specific properties into multiple backgrounds?
  • 167. Browsers will ignore CSS they don’t understand. In fact, they will ignore the entire declaration.
  • 168. Vendor-specific properties are only understood by individual browsers - that’s their purpose.
  • 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)); }
  • 172. Russ Weakley Max Design Site: maxdesign.com.au Twitter: twitter.com/russmaxdesign Slideshare: slideshare.net/maxdesign Linkedin: linkedin.com/in/russweakley