CSS color and background tutorial: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
mNo edit summary
m (Text replacement - "<pageby nominor="false" comments="false"/>" to "<!-- <pageby nominor="false" comments="false"/> -->")
 
(46 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Incomplete}}
{{Incomplete}}
{{Web technology tutorial|Beginners}}
{{Web technology tutorial|Beginners}}
<pageby nominor="false" comments="false"/>
<!-- <pageby nominor="false" comments="false"/> -->


<div class="tut_goals">
<div class="tut_goals">
Line 13: Line 13:


; Moving on
; Moving on
* [[CSS box model tutorial]]
* [[CSS positioning tutorial]]
* [[CSS positioning tutorial]]
* [[CSS for XML tutorial]]
* [[CSS for XML tutorial]]
Line 21: Line 22:


; Teaching materials
; Teaching materials
* ....
* http://tecfa.unige.ch/guides/css/ex/css-page-background.html (ugly background example page)
* http://tecfa.unige.ch/guides/css/ex/css-page-background-example.html (background example page)


; Remarks
; Remarks
Line 34: Line 36:
In CSS 2.1 (support by nearly all browsers on the market, i.e. also back two older generations), there exist several properties for which a color can be set.
In CSS 2.1 (support by nearly all browsers on the market, i.e. also back two older generations), there exist several properties for which a color can be set.


Let's have a look at some example code that demontrates two of the most popular color properties and the typical RGB hex notation for color values:
Let's have a look at some example code that demonstrates two of the most popular color properties and the typical RGB hex notation for color values:


The following inline CSS code
The following inline CSS code
<source lang="CSS">
<source lang="CSS">
   <p> <span style="background-color: #0000ff; color: #ffffff;"> Blue background and white foreground</span> demo </p>
   <p> <span style="background-color: #0000ff; color: #ffffff;">  
      Blue background and white foreground</span> demo </p>
</source>
</source>
would show like this:
would show like this:
   <p> <span style="background-color: #0000ff; color: #ffffff;"> Blue background and white foreground</span> demo </p>
   <p> <span style="background-color: #0000ff; color: #ffffff;"> Blue background and white foreground</span> demo </p>


We shall explain more details below, but befor we shall present an overview of CSS 2 color properties and introduce the color model used in CSS 2.
We shall explain more details below, but before we shall present an overview of CSS 2 color properties and then introduce the RGB color model used in CSS 2.


=== CSS 2 color related properties ===
=== CSS 2 color related properties ===


''' Setting the font color '''
''' (1) Setting the font color '''


* '''''color'''''
* '''''color'''''
Line 53: Line 56:
: Example: ''color: #ff0000;''
: Example: ''color: #ff0000;''


''' Setting the background color'''
''' (2) Setting the background'''
{{quotation|Authors may specify the background of an element (i.e., its rendering surface) as either a color or an image. In terms of the box model, "background" refers to the background of the content, padding and border areas. Border colors and styles are set with the border properties. Margins are always transparent.}} ([http://www.w3.org/TR/CSS2/colors.html CSS 2 spec.])
{{quotation|Authors may specify the background of an element (i.e., its rendering surface) as either a color or an image. In terms of the box model, "background" refers to the background of the content, padding and border areas. Border colors and styles are set with the border properties. Margins are always transparent.}} ([http://www.w3.org/TR/CSS2/colors.html CSS 2 spec.])


* '''''background-color'''''
* '''''background-color'''''
Line 63: Line 65:
* '''''background-image'''''
* '''''background-image'''''
: sets the background image for an element. An image is not really a color, but can be used for the same purpose
: sets the background image for an element. An image is not really a color, but can be used for the same purpose
: Example: ''background-image: url ("flower.png");''
* '''''background-repeat'''''
* '''''background-repeat'''''
: defines the tiling (and tiling direction) of a background-image
: defines the tiling (and tiling direction) of a background-image
Line 71: Line 74:
* '''''background'''''
* '''''background'''''
: is a short hand notation that allows to define all background definitions
: is a short hand notation that allows to define all background definitions
: Example:''background: url("flower.png") gray 50% repeat fixed''


=== The RGB color model ===
=== The RGB color model ===
[[Image:AdditiveColor-192px.png|right|A representation of additive color mixing ([http://en.wikipedia.org/wiki/RGB_color_mode Wikipedia)]]]
RGB colors are the most popular ones used in computing applications. A color is defined by the '''amount''' of '''R'''ed - '''G'''reen - '''B'''lue.


=== CSS 2 color models ===
RGB is the way computer monitors work. E.g. to get a nice yellow
you need 100% Red + 100% Green + 0% Blue. RGB is a so-called '''additive''' color mixing model. {{quotation|Projection of [http://en.wikipedia.org/wiki/Primary_color primary color] lights on a screen shows secondary colors where two overlap; the combination of all three of red, green, and blue in appropriate intensities makes white.}} ([http://en.wikipedia.org/wiki/RGB_color_model Wikipedia]). Now if you project each of these primary colors with different intensity, overlapping colors will change.


There are two way of defining '''CSS 2''' [[color]]s. Either by its name (but the official list only includes 17 colors) or by a so-called RGB value. The latter can be specified in four different ways as we shall show in the example below.
This model is not how colors work when you mix real paint. Then you'd rather work with a red-yellow-blue model. Color printers yet work with another model, i.e. magenta, cyan and yellow (or more).
 
RGB colors can be encoded in various ways. For Internet formats such as HTML, CSS or Flash, most often a ''hex triplet'' is used, i.e. a hexadecimal 6 digit number. With 2 hexadecimal digits you can represent numbers in the range of 0 to 255, i.e. only a reduced set of colors.
 
=== CSS 2 color values ===
 
There are two way of defining '''CSS 2''' [[color]]s. Either by their name (but this only works for 17 colors) or by a so-called RGB value. The latter can be specified in four different ways as we shall show in the example below.
The 17 pre-defined color names in CSS 2.1 are the following, according to the [http://www.w3.org/TR/CSS2/syndata.html#color-units CSS 2.1 specification] from which we made a screen shot:
The 17 pre-defined color names in CSS 2.1 are the following, according to the [http://www.w3.org/TR/CSS2/syndata.html#color-units CSS 2.1 specification] from which we made a screen shot:
[[image:css-colors.png|frame|none|The 17 official CSS 2 color definitions]]
[[image:css-colors.png|frame|none|The 17 official CSS 2 color definitions]]
Line 96: Line 109:
</source>
</source>


== CSS 3 colors ==
CSS 3 implements (of course) all CSS 2 features. In addition it implements a whole range of new color related properties and values.
=== RGB colors ===
Work like in CSS. The only difference is that there are more color names you could use. The following table was taken as is from Wikipedia:
{{:CSS3 color names table}}
=== The HSL/HSI model ===
The HSL (Hue-saturation-lightness) model, also known as HSI (hue-saturation-intensity) defines colors with a different model.
# '''H'''ue, the color: Represented as a position in the 360 degrees of a color circle.
# '''S'''aturation, the intensity or "purity" of the color: Ranges from 0-100%. 0 means no color, 100% means full color.
# '''L'''ightness or '''I'''ntensity. You could think of 100% as "full white", and of 0% as "no white or full black". 50% is normal (no white, no black).
The Hue scale from 0 to 360 '''degrees''' is the following:
[[image:hue-scale.png|thumb|500px|none|The hue scale ([http://en.wikipedia.org/wiki/Image:HueScale.svg Wikipedia])]]
The following picture shows the same color space as a circle
[[image:HLSColorSpace.png|thumb|300px|none|HSL color space. Source: [http://commons.wikimedia.org/wiki/File:HLSColorSpace.png Wikimedia commons] ]]
Sometimes the HSL is represented as wheel with a triangle inside, but often you will find either a cylinder or circles that represent cross sections of the cylinder since you can't represent all colors in a single flat picture.
Wikipedia's article on [http://en.wikipedia.org/wiki/HSL_and_HSV HSL and HSV] includes a HSL-HSV comparison picture from which we extracted the HSL part:
[[image:hsl-model.svg|thumb|800px|none|HSL Above (a): cut-away 3D models of HSL. To the right two-dimensional plots showing two of the model’s three parameters at once, holding the other constant: cylindrical shell (b) of constant saturation, in this case the outside edge of each cylinder; horizontal cross-section (c) of constant HSL lightness value, in this case the slices halfway down each cylinder; and rectangular vertical cross-section (d) of constant hue, in this case of hues 0° red and its complement 180° cyan.]]


Notice: '''HSV''' is a similar model, but it's a bit more difficult to grasp. Below we just show a figure that compares HSL with HSV.
[[image:HSL_HSV_cylinder_color_solid_comparison.png|thumb|600px|none|Comparison of the HSL (left) and HSV (right) color models. Source: [http://en.wikipedia.org/wiki/File:HSL_HSV_cylinder_color_solid_comparison.png Wikipedia], retrieved 17:38, 11 September 2009 (UTC)]]


== CSS 3 colors ==
According to the [http://www.w3.org/TR/css3-color/#hsl-color CSS specification], retrieved 17:38, 11 September 2009 (UTC)
{{quotationbox|CSS3 adds numerical hue-saturation-lightness (HSL) colors as a complement to numerical RGB colors. It has been observed that RGB colors have the following limitations:
* RGB is hardware-oriented: it reflects the use of CRTs.
* RGB is non-intuitive. People can learn how to use RGB, but actually by internalizing how to translate hue, saturation and lightness, or something similar, to RGB.
Advantages of HSL are that it is symmetrical to lightness and darkness (which is not the case with HSV for example), and it is trivial to convert HSL to RGB.
}}
 
CSS 3 and SVG implement the HSL model. Color (hue) is defined by the position in the color wheel, e.g. red = 0, red = 360, blue=240. Saturation and lightness are represented by percentages from 0 to 100%. Important: "normal" lightness is 50%.
 
Below is table of the main colors (at 30i intervals). We present three situations:
* Saturation is set to 100% and lightness to 50% (normal)
* Saturation of 50% and lightness of 25% (dark)
* Saturation of 75% and lightness of 75% (pastel)
Warning: You need a somewhat CSS 3 capable browser like Firefox 3+ or IE 9 ( IE 8 will not work)
 
{| style border="1"
!Hue !! Color name !! Sat=100% / Light=50% !! Sat=50% / Light=25% !! Sat=75% / light=75%
|-
| 0 || red || style="background-color: hsl(0, 100%, 50%);" | 0 - red || style="background-color: hsl(0, 50%, 25%);" | 0 - red  || style="background-color: hsl(0, 75%, 75%);" | 0 - red
|-
| 30 || orange || style="background-color: hsl(30, 100%, 50%);" | 30 - orange || <span style="background-color: hsl(30, 50%, 25%);" | 30 - orange  || style="background-color: hsl(30, 75%, 75%);" | 30 - orange
|-
| 60 || yellow || style="background-color: hsl(60, 100%, 50%);" | 60 - yellow || <span style="background-color: hsl(60, 50%, 25%);" | 60 - yellow  || style="background-color: hsl(60, 75%, 75%);" | 60 - yellow
|-
| 90 || yellow-green || style="background-color: hsl(90, 100%, 50%);" | 90 - yellow-green || <span style="background-color: hsl(90, 50%, 25%);" | 90 - yellow-green  || style="background-color: hsl(90, 75%, 75%);" | 90 - yellow-green
|-
| 120 || green || style="background-color: hsl(120, 100%, 50%);" | 120 - green || <span style="background-color: hsl(120, 50%, 25%);" | 120 - green  || style="background-color: hsl(120, 75%, 75%);" | 120 - green
|-
| 150 || green-cyan || style="background-color: hsl(150, 100%, 50%);" | 150 - green-cyan || <span style="background-color: hsl(150, 50%, 25%);" | 150 - green-cyan  || style="background-color: hsl(150, 75%, 75%);" | 150 - green-cyan
|-
| 180 || cyan || style="background-color: hsl(180, 100%, 50%);" | 180 - cyan || <span style="background-color: hsl(180, 50%, 25%);" | 180 - cyan  || style="background-color: hsl(180, 75%, 75%);" | 180 - cyan
|-
| 210 || cyan-blue || style="background-color: hsl(210, 100%, 50%);" | 210 - cyan-blue || <span style="background-color: hsl(210, 50%, 25%);" | 210 - cyan-blue  || style="background-color: hsl(210, 75%, 75%);" | 210 - cyan-blue
|-
| 240 || blue || style="background-color: hsl(240, 100%, 50%);" | 240 - blue || <span style="background-color: hsl(240, 50%, 25%);" | 240 - blue  || style="background-color: hsl(240, 75%, 75%);" | 240 - blue
|-
| 270 || blue-magenta || style="background-color: hsl(270, 100%, 50%);" | 270 - blue-magenta || <span style="background-color: hsl(270, 50%, 25%);" | 270 - blue-magenta  || style="background-color: hsl(270, 75%, 75%);" | 270 - blue-magenta
|-
| 300 || magenta || style="background-color: hsl(300, 100%, 50%);" | 300 - magenta || <span style="background-color: hsl(300, 50%, 25%);" | 300 - magenta  || style="background-color: hsl(300, 75%, 75%);" | 300 - magenta
|-
| 330 || magenta-red || style="background-color: hsl(330, 100%, 50%);" | 330 - magenta-red || <span style="background-color: hsl(330, 50%, 25%);" | 330 - magenta-red  || style="background-color: hsl(330, 75%, 75%);" | 330 - magenta-red
|-
| 360 || red || style="background-color: hsl(360, 100%, 50%);" | 360 - red || <span style="background-color: hsl(360, 50%, 25%);" | 360 - red  || style="background-color: hsl(360, 75%, 75%);" | 360 - red
|}
 
Now, where is white an black ?
* Any hue that is 100% saturated and 100% lightness is white
* Any hue that is 0% saturated and/or lightness is black
So basically, an HSL color goes
* From colorful to colorless in terms of saturation
* From black to normal to white in terms of lightness (50% is normal)
 
Below is screen capture from the [http://www.w3.org/TR/css3-color/ CSS Color Module Level 3] (Working draft, retrieved 17:38, 11 September 2009 (UTC)) that shows on the X axis the saturation (100%, 75%, 50%, 25%, 0%) and on the Y axis the lightness.
 
[[image:CSS-3-draft-HSL-red.png|frame|none| HSL Example - 0i red (Source: W3C)]]
 
Finally, here are some CSS code examples:
<source lang="CSS">
{ color: hsl(0, 100%, 50%) }  /* red */
{ color: hsl(120, 100%, 50%) } /* green */
{ color: hsl(120, 100%, 25%) } /* dark green */
{ color: hsl(120, 100%, 75%) } /* light green */
{ color: hsl(120, 75%, 75%) }  /* pastel green, and so on */
</source>
E.g. The following HSL CSS code
<source lang="CSS">
  <p style="color: hsl(240,75%,75%);"> Kind of not so blue</p>
</source>
would show like this (your browser may not support this):
  <p style="color: hsl(240,75%,75%);"> Kind of not so blue</p>


CSS 3 implements (of course) all CSS 2 features. In addition it implements a whole range of new color related properties and values.  
For more information about HSL (and HSV), we point again to Wikipedia's [http://en.wikipedia.org/wiki/HSV_color_space HSL and HSV article].


=== HSL colors ===
=== HSL colors with CSS3 ===


In CSS, the hsl value is defined by the position in the color wheel, e.g. red = 0, red = 360, blue=240. Saturation and lightness are represented by percentages. Here are a few examples:
In CSS, the hsl value is defined by the position in the color wheel, e.g. red = 0, red = 360, blue=240. Saturation and lightness are represented by percentages. Here are a few examples:
Line 119: Line 232:
=== Alpha channel ===
=== Alpha channel ===


In CSS (if your browser supports it), you also can define the alpha channel. You may use so-called RGBA values using the ''%'' notation, i.e.
In computer graphics, alpha compositing is the process of combining an image with a background to create the appearance of partial transparency ([http://en.wikipedia.org/wiki/Alpha_channel Wikipedia])
 
In more simple terms, you can set the alpha to some percentage:
* 100% can't see through
* 80% bad see trough
* 50% in between
* 30% good see through
* 10% good see through, but very little color
* 0% no color left
[[image:flash-cs3-color-alpha-channel.jpg|thumb|300px|none|Alpha color channel]]
 
Hint: With the alpha channel you can create other effects than see-through "windows". E.g. you can overlay textures with color or the other way round.
 
In CSS3 (if your browser supports it), you also can define the alpha channel. You may use so-called RGBA values using the ''rgba ()'' notation for RGB values. ''a'' in ''rgba'' stands for alpha. Some examples:
<source lang="CSS">
<source lang="CSS">
   p { background-color: rgba(0,0,255,0.5) }        /* semi-transparent solid blue */
   p { background-color: rgba(0,0,255,0.5) }        /* semi-transparent solid blue */
Line 129: Line 255:
would show like the "Kind of blue" box to the right (your browser may not support this): <p style="float:right;background-color: rgba(0,0,255,0.3);">Kind of blue</p>
would show like the "Kind of blue" box to the right (your browser may not support this): <p style="float:right;background-color: rgba(0,0,255,0.3);">Kind of blue</p>


Alternatively you can use the opacity property that does the same, e.g.
Alternatively you can use the '''opacity''' property that does the same, e.g.
<source lang="CSS">  
<source lang="CSS">  
  background: rgb(255, 0, 0) ; opacity: 0.2;">
  background: rgb(255, 0, 0) ; opacity: 0.2;">
</source>
</source>


Finally, in CSS 3, you also may use HSL with an alpha channel
Finally, in CSS 3, you also may use HSL with an alpha channel, i.e. define an ''hsla'' value:
<source lang="CSS">
<source lang="CSS">
/* HSL model with alpha channel */
/* HSL model with alpha channel */
Line 142: Line 268:
</source>
</source>


== Links ==
== CSS 2 Background properties ==
 
When you use a background image within an element it is by default tiled vertically and horizentally, under the condition that its x/y dimensions are smaller than the styled element.
 
The '''''background-repeat''''' property allows to change default settings. E.g. to inhibit repetition, use:
<source lang="CSS">
background-image: url(flower.png);
background-repeat: no-repeat;
</source>
 
=== html vs. body vs. universal background ===
 
Example file:
* http://tecfa.unige.ch/guides/css/ex/css-page-background.html
 
Using background images is more difficult than it may seem. In particular there are a few principles and issues that you should be aware of:
* Pictures will shine through all elements, unless these elements explicitly define a background. In other words, if an element is transparent (this is the default), the picture underneath will show through empty spaces in the "box", i.e. the rectangle define by the element's boundaries.
* Watch out for interactions between using the universal rule, the body and the html for setting a background. E.g. if you define a background picture in the universal rule, bits can shine through in various HTML elements, even if you defined a other background in the body element. This, because backgrounds are not inherited.
* Various other properties also may have effects that may need some tuning. E.g. an none repeated background will display the picture as a whole.
 
If you set a background image like this, i.e. as default for all elements:
<source lang="CSS">
    * {
      background-image: url(tournesol.png);
      }
</source>
the pictures will fill the whole web browser window in the background. Images will not repeat again at each element level.
 
If you set a background image as "html" background like this:
<source lang="CSS">
html {
      background-image: url(tournesol.png);
      }
</source>
then the whole visible window area will be filled with the picture.
 
If you set a background image as "body" background like this:
<source lang="CSS">
body {
      background-image: url(tournesol.png);
      }
</source>
and if neither body nor the "*" has a background image it will fill the whole window as above.
 
However, if there is a background imageor color for the html tag or the universal selector, then only the the space taken up by the body will be filled. In particular, margins will not be filled and space left from the end of your content down to the end of the browser window will not be filled with this image.
 
=== Background image repetition ===
 
* Repeat in the x direction
<source lang="CSS">
background-repeat: repeat-x;
</source>
 
=== Positioning background images ===
 
Background pictures can be positioned with respect to the boundaries of the element.
 
E.g. if you insert a picture in the body you could put it at bottom right like this:
<source lang="CSS">
background-image: url(flower.png);
background-repeat: no-repeat;
background-position: bottom right;
</source>
 
'''''background-position''''' takes two values that represent both the y and the x offset, i.e. how much it will go down and to the right. If only one value is specified it refers to the horizontal x position.
 
Examples:
<source lang="CSS">
background-position: 75px 150px;
background-position: 60% 20px;
</source>
 
In order to center a picture, you could use:
 
<source lang="CSS">
background-image: url(flower.png);
background-repeat: no-repeat;
background-position: bottom right;
</source>
 
=== Fixing background images ===
 
You also can fix a background image with respect to the so-called viewport (what the users sees in the browser window), so that it stays when a user will scroll. More precisely: {{quotation|If a background image is specified, this property specifies whether it is fixed with regard to the viewport ('fixed') or scrolls along with the containing block ('scroll').}} ([http://www.w3.org/TR/CSS2/colors.html CSS 2 spec]).
 
Example:
<source lang="CSS">
background-image: url(flower.png);
background-repeat: no-repeat;
background-attachment: fixed;
</source>
 
The '''background-attachment''' property will take the following values:
* '''scrolll''' (this is the default)
* '''fixed''': Will scroll up-down/left-right when the user (or a script) uses scrolling.
* '''inherit'''
 
Note: An image is only visible if it sits in the content, padding or border area of the element.
 
=== The background shorthand ===
 
The CSS3 syntax is the following (see some examples later):
: background: ''background-color-value'' ''background-image-value'' ''background-repeat-value'' ''background-attachment-value'' ''background-position-value''
 
Property values have to be presented in this order, but you can omit any you like:
 
Simple ''background'' examples:
<source lang="CSS">
body {
  background:#ffffff url('flower.png') no-repeat right top;
  }
body {
  background:#url('flower.png') no-repeat 50% top;
}
</source>
 
== CSS3 background properties ==
This is not complete, we only show some properties of the [http://www.w3.org/TR/css3-background/ CSS Backgrounds and Borders Module Level 3] (draft standard, 15 feb 2011).
 
=== Multiple images ===
 
In CSS3 (only), one also can define multiple background images in two different ways. For now, just a simple example:
 
<source lang="CSS">
p {
background:
url(one.jpg) top center no-repeat,
url(two.png) 40% 20px no-repeat,
url(three.png) 60% 20px no-repeat,
url(four.png) 3em 3em repeat-x,
url(car.gif) center no-repeat;
}
</source>
Notice how these background image definitions are separated by '''commas''' !
 
=== CSS 3 background size and background origin ===
 
The CSS3 (only!) property '''background-size''' allows specify the size of background images using length, percentages, or ''contain'', ''cover'' or ''auto''.
 
This property is defined in the [http://www.w3.org/TR/css3-background/ CSS Backgrounds and Borders Module Level 3] W3C Candidate Recommendation 15 February 2011. As of nov 2001 this should be implement in most recent browsers.
 
The most interesting value is ''cover'' since it allows to deal with "fluid" designs, i.e. web pages that adapt to the size of the screen.
 
Examples:
<source lang="CSS">
background-size: 300px;      /* 300 px wide and height is auto*/
background-size: 300px 200px; /* 300px width and 200px height */
background-size: 50% 50%;    /* set size = 50% of the parent element */
background-size: auto;
background-size: contain;    /* largest image size that makes it fit inside */
background-size: cover;      /* smallest image size that makes it fit inside */
</source>
 
If it doesn't work in your (older) browser, you could try:
<source lang="CSS">
-moz-background-size:
-o-background-size:
-webkit-background-size:
</source>
 
In addition, you may have to use the '''background-origin''' property that specifies the background positioning area. The example lines should be self-explaining.
 
Examples:
<source lang="CSS">
background-origin: padding-box /* default value */
background-origin: border-box
background-origin: content-box
</source>
 
Examples:
* See: [http://www.w3.org/TR/css3-background/#the-background-size The ‘background-size’ property] section in the specification.
 
=== CSS 3 gradients ===
 
<source lang="XML">
<div height="100px"
    style="background: linear-gradient(top, #eff484 0%,#1e5799 100%);">
Hello
</div>
</source>
 
The following example shows the kind of code that you '''now''' have to insert for dealing with current and last generation browsers.
 
<source lang="CSS">
background: #eff484; /* Old browsers */
background: -moz-linear-gradient(top,  #eff484 0%, #1e5799 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#eff484), color-stop(100%,#1e5799)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  #eff484 0%,#1e5799 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  #eff484 0%,#1e5799 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  #eff484 0%,#1e5799 100%); /* IE10+ */
background: linear-gradient(top,  #eff484 0%,#1e5799 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#eff484', endColorstr='#1e5799',GradientType=0 ); /* IE6-9 using ActiveX */
</source>
 
=== Border images ===
 
* Firstly, you need to set the with of the border, i.e. rather large
* The '''border-image''' property then will reuse these values
* The stretch keyword means that the image should be stretched to match the sizes above.
 
Example:
<source lang="CSS">
border-style: solid;
border-color: #930;
border-width: 20px 40px 40px 40px;
border-image: url(frame.png) 20 40 40 40 stretch stretch;
-webkit-border-image: url(frame.png) 20 40 40 40 stretch stretch;
-moz-border-image: url(frame.png) 20 40 40 40 stretch round;
</source>
 
== Tools and links ==
 
=== Tools ===
 
; Firefox color picker
 
* [https://addons.mozilla.org/en-US/firefox/addon/271 ColorZilla] includes an Eyedropper, ColorPicker, Page Zoomer and other colorful goodies.
 
; Gradient Generator
 
* [http://gradients.glrzad.com/ CSS3 gradient generator] is on-line tool (free) for creating CSS 3 gradients. It generates both CSS3 code and four vendor-specific codes that one could use before CSS is fully implemented.
 
* [http://www.colorzilla.com/gradient-editor/ Ultimate CSS Gradient Generator]
 
=== Introductions ===
 
; Color
* [[Computer colors tutorial]]
* [http://en.wikipedia.org/wiki/Web_color Web colors] (Wikipedia)
* [http://en.wikipedia.org/wiki/RGB_color_model RGB color model] (Wikipedia)
* [http://en.wikipedia.org/wiki/HSV_color_space HSL and HSV] (Wikipedia)
* [http://www.css3.info/preview/background-size/ background size] (CSS3.info)
 
=== Reference ===
 
CSS specification pages at W3C:
* http://www.w3.org/TR/CSS2/colors.html (stable)
* http://www.w3.org/TR/css3-background/ (W3C Candidate Recommendation 15 February 2011)
* http://www.w3.org/TR/css3-color/ (W3C Recommendation 07 June 2011)
 
Color tables:
* [http://en.wikipedia.org/wiki/Web_colors Web colors] (Wikipedia)
* [http://en.wikipedia.org/wiki/X11_color_names X11 color names] (same as CSS3) at Wikipedia
 
[[Category: CSS]]

Latest revision as of 18:23, 22 August 2016

Learning goals
  • Understand CSS 2 and CSS color models
  • Be able to user background and font colors
Concurrent
Moving on
Level and target population
  • Beginners
Teaching materials
Remarks
  • This tutorial is intended for students in educational technology or any other field that is technology intensive. For people who need less, there exist many easy CSS tutorials on the web. This text is intended for students who also must learn principles and who are willing to learn CSS by doing a project, looking at CSS code and online reference manuals.
  • Ideally, a teacher also should assign a text formatting task, during or before assigning this tutorial for reading).

Introduction

CSS 2 colors

In CSS 2.1 (support by nearly all browsers on the market, i.e. also back two older generations), there exist several properties for which a color can be set.

Let's have a look at some example code that demonstrates two of the most popular color properties and the typical RGB hex notation for color values:

The following inline CSS code

  <p> <span style="background-color: #0000ff; color: #ffffff;"> 
      Blue background and white foreground</span> demo </p>

would show like this:

Blue background and white foreground demo

We shall explain more details below, but before we shall present an overview of CSS 2 color properties and then introduce the RGB color model used in CSS 2.

CSS 2 color related properties

(1) Setting the font color

  • color
defines the foreground color for text content
Example: color: #ff0000;

(2) Setting the background “Authors may specify the background of an element (i.e., its rendering surface) as either a color or an image. In terms of the box model, "background" refers to the background of the content, padding and border areas. Border colors and styles are set with the border properties. Margins are always transparent.” (CSS 2 spec.)

  • background-color
defines the background’s color of an element
Example: background-color: red;
  • background-image
sets the background image for an element. An image is not really a color, but can be used for the same purpose
Example: background-image: url ("flower.png");
  • background-repeat
defines the tiling (and tiling direction) of a background-image
  • background-position
defines the position of a background image
  • background-attachment
defines whether the background image scrolls with the containing block or stays fixed with respect to the viewport (i.e. what is displayed)
  • background
is a short hand notation that allows to define all background definitions
Example:background: url("flower.png") gray 50% repeat fixed

The RGB color model

A representation of additive color mixing (Wikipedia)

RGB colors are the most popular ones used in computing applications. A color is defined by the amount of Red - Green - Blue.

RGB is the way computer monitors work. E.g. to get a nice yellow you need 100% Red + 100% Green + 0% Blue. RGB is a so-called additive color mixing model. “Projection of primary color lights on a screen shows secondary colors where two overlap; the combination of all three of red, green, and blue in appropriate intensities makes white.” (Wikipedia). Now if you project each of these primary colors with different intensity, overlapping colors will change.

This model is not how colors work when you mix real paint. Then you'd rather work with a red-yellow-blue model. Color printers yet work with another model, i.e. magenta, cyan and yellow (or more).

RGB colors can be encoded in various ways. For Internet formats such as HTML, CSS or Flash, most often a hex triplet is used, i.e. a hexadecimal 6 digit number. With 2 hexadecimal digits you can represent numbers in the range of 0 to 255, i.e. only a reduced set of colors.

CSS 2 color values

There are two way of defining CSS 2 colors. Either by their name (but this only works for 17 colors) or by a so-called RGB value. The latter can be specified in four different ways as we shall show in the example below. The 17 pre-defined color names in CSS 2.1 are the following, according to the CSS 2.1 specification from which we made a screen shot:

The 17 official CSS 2 color definitions

Otherwise, you may choose from several RGB numerical colors specifications as the following example shows. The following example will set the color of a text:

em { color: #f00 }              /* #rgb = Red-Green-Blue shortcut for rrggbb*/
em { color: #ff0000 }           /* #rrggbb */
em { color: rgb(255,0,0) }      
em { color: rgb(100%, 0%, 0%) }

In order to set the background-color use something like:

em { background-color: #f00 }              /* #rgb = Red-Green-Blue shortcut for rrggbb*/
em { background-color: #ff0000 }           /* #rrggbb */
em { background-color: rgb(255,0,0) }      
em { background-color: rgb(100%, 0%, 0%) }

CSS 3 colors

CSS 3 implements (of course) all CSS 2 features. In addition it implements a whole range of new color related properties and values.

RGB colors

Work like in CSS. The only difference is that there are more color names you could use. The following table was taken as is from Wikipedia:

X11/CSS 3 color names table (copied "as is" from Wikipedia):
HTML name Hex code
R G B
Decimal code
R G B
Pink colors
Pink FF C0 CB 255 192 203
LightPink FF B6 C1 255 182 193
HotPink FF 69 B4 255 105 180
DeepPink FF 14 93 255  20 147
PaleVioletRed DB 70 93 219 112 147
MediumVioletRed C7 15 85 199  21 133
Red colors
LightSalmon FF A0 7A 255 160 122
Salmon FA 80 72 250 128 114
DarkSalmon E9 96 7A 233 150 122
LightCoral F0 80 80 240 128 128
IndianRed CD 5C 5C 205  92  92
Crimson DC 14 3C 220  20  60
FireBrick B2 22 22 178  34  34
DarkRed 8B 00 00 139   0   0
Red FF 00 00 255   0   0
Orange colors
OrangeRed FF 45 00 255  69   0
Tomato FF 63 47 255  99  71
Coral FF 7F 50 255 127  80
DarkOrange FF 8C 00 255 140   0
Orange FF A5 00 255 165   0
Yellow colors
Yellow FF FF 00 255 255   0
LightYellow FF FF E0 255 255 224
LemonChiffon FF FA CD 255 250 205
LightGoldenrodYellow FA FA D2 250 250 210
PapayaWhip FF EF D5 255 239 213
Moccasin FF E4 B5 255 228 181
PeachPuff FF DA B9 255 218 185
PaleGoldenrod EE E8 AA 238 232 170
Khaki F0 E6 8C 240 230 140
DarkKhaki BD B7 6B 189 183 107
Gold FF D7 00 255 215   0
Brown colors
Cornsilk FF F8 DC 255 248 220
BlanchedAlmond FF EB CD 255 235 205
Bisque FF E4 C4 255 228 196
NavajoWhite FF DE AD 255 222 173
Wheat F5 DE B3 245 222 179
BurlyWood DE B8 87 222 184 135
Tan D2 B4 8C 210 180 140
RosyBrown BC 8F 8F 188 143 143
SandyBrown F4 A4 60 244 164  96
Goldenrod DA A5 20 218 165  32
DarkGoldenrod B8 86 0B 184 134  11
Peru CD 85 3F 205 133  63
Chocolate D2 69 1E 210 105  30
SaddleBrown 8B 45 13 139  69  19
Sienna A0 52 2D 160  82  45
Brown A5 2A 2A 165  42  42
Maroon 80 00 00 128   0   0
HTML name Hex code
R G B
Decimal code
R G B
Green colors
DarkOliveGreen 55 6B 2F  85 107  47
Olive 80 80 00 128 128   0
OliveDrab 6B 8E 23 107 142  35
YellowGreen 9A CD 32 154 205  50
LimeGreen 32 CD 32  50 205  50
Lime 00 FF 00   0 255   0
LawnGreen 7C FC 00 124 252   0
Chartreuse 7F FF 00 127 255   0
GreenYellow AD FF 2F 173 255  47
SpringGreen 00 FF 7F   0 255 127
MediumSpringGreen 00 FA 9A   0 250 154
LightGreen 90 EE 90 144 238 144
PaleGreen 98 FB 98 152 251 152
DarkSeaGreen 8F BC 8F 143 188 143
MediumSeaGreen 3C B3 71  60 179 113
SeaGreen 2E 8B 57  46 139  87
ForestGreen 22 8B 22  34 139  34
Green 00 80 00   0 128   0
DarkGreen 00 64 00   0 100   0
Cyan colors
MediumAquamarine 66 CD AA 102 205 170
Aqua 00 FF FF   0 255 255
Cyan 00 FF FF   0 255 255
LightCyan E0 FF FF 224 255 255
PaleTurquoise AF EE EE 175 238 238
Aquamarine 7F FF D4 127 255 212
Turquoise 40 E0 D0  64 224 208
MediumTurquoise 48 D1 CC  72 209 204
DarkTurquoise 00 CE D1   0 206 209
LightSeaGreen 20 B2 AA  32 178 170
CadetBlue 5F 9E A0  95 158 160
DarkCyan 00 8B 8B   0 139 139
Teal 00 80 80   0 128 128
Blue colors
LightSteelBlue B0 C4 DE 176 196 222
PowderBlue B0 E0 E6 176 224 230
LightBlue AD D8 E6 173 216 230
SkyBlue 87 CE EB 135 206 235
LightSkyBlue 87 CE FA 135 206 250
DeepSkyBlue 00 BF FF   0 191 255
DodgerBlue 1E 90 FF  30 144 255
CornflowerBlue 64 95 ED 100 149 237
SteelBlue 46 82 B4  70 130 180
RoyalBlue 41 69 E1  65 105 225
Blue 00 00 FF   0   0 255
MediumBlue 00 00 CD   0   0 205
DarkBlue 00 00 8B   0   0 139
Navy 00 00 80   0   0 128
MidnightBlue 19 19 70  25  25 112
HTML name Hex code
R G B
Decimal code
R G B
Purple/Violet/Magenta colors
Lavender E6 E6 FA 230 230 250
Thistle D8 BF D8 216 191 216
Plum DD A0 DD 221 160 221
Violet EE 82 EE 238 130 238
Orchid DA 70 D6 218 112 214
Fuchsia FF 00 FF 255   0 255
Magenta FF 00 FF 255   0 255
MediumOrchid BA 55 D3 186  85 211
MediumPurple 93 70 DB 147 112 219
BlueViolet 8A 2B E2 138  43 226
DarkViolet 94 00 D3 148   0 211
DarkOrchid 99 32 CC 153  50 204
DarkMagenta 8B 00 8B 139   0 139
Purple 80 00 80 128   0 128
Indigo 4B 00 82  75   0 130
DarkSlateBlue 48 3D 8B  72  61 139
RebeccaPurple 66 33 99 102  51 153
SlateBlue 6A 5A CD 106  90 205
MediumSlateBlue 7B 68 EE 123 104 238
White colors
White FF FF FF 255 255 255
Snow FF FA FA 255 250 250
Honeydew F0 FF F0 240 255 240
MintCream F5 FF FA 245 255 250
Azure F0 FF FF 240 255 255
AliceBlue F0 F8 FF 240 248 255
GhostWhite F8 F8 FF 248 248 255
WhiteSmoke F5 F5 F5 245 245 245
Seashell FF F5 EE 255 245 238
Beige F5 F5 DC 245 245 220
OldLace FD F5 E6 253 245 230
FloralWhite FF FA F0 255 250 240
Ivory FF FF F0 255 255 240
AntiqueWhite FA EB D7 250 235 215
Linen FA F0 E6 250 240 230
LavenderBlush FF F0 F5 255 240 245
MistyRose FF E4 E1 255 228 225
Gray/Black colors
Gainsboro DC DC DC 220 220 220
LightGrey D3 D3 D3 211 211 211
Silver C0 C0 C0 192 192 192
DarkGray A9 A9 A9 169 169 169
Gray 80 80 80 128 128 128
DimGray 69 69 69 105 105 105
LightSlateGray 77 88 99 119 136 153
SlateGray 70 80 90 112 128 144
DarkSlateGray 2F 4F 4F  47  79  79
Black 00 00 00   0   0   0

Links to official documents

The HSL/HSI model

The HSL (Hue-saturation-lightness) model, also known as HSI (hue-saturation-intensity) defines colors with a different model.

  1. Hue, the color: Represented as a position in the 360 degrees of a color circle.
  2. Saturation, the intensity or "purity" of the color: Ranges from 0-100%. 0 means no color, 100% means full color.
  3. Lightness or Intensity. You could think of 100% as "full white", and of 0% as "no white or full black". 50% is normal (no white, no black).

The Hue scale from 0 to 360 degrees is the following:

The hue scale (Wikipedia)

The following picture shows the same color space as a circle

HSL color space. Source: Wikimedia commons

Sometimes the HSL is represented as wheel with a triangle inside, but often you will find either a cylinder or circles that represent cross sections of the cylinder since you can't represent all colors in a single flat picture.

Wikipedia's article on HSL and HSV includes a HSL-HSV comparison picture from which we extracted the HSL part:

HSL Above (a): cut-away 3D models of HSL. To the right two-dimensional plots showing two of the model’s three parameters at once, holding the other constant: cylindrical shell (b) of constant saturation, in this case the outside edge of each cylinder; horizontal cross-section (c) of constant HSL lightness value, in this case the slices halfway down each cylinder; and rectangular vertical cross-section (d) of constant hue, in this case of hues 0° red and its complement 180° cyan.

Notice: HSV is a similar model, but it's a bit more difficult to grasp. Below we just show a figure that compares HSL with HSV.

Comparison of the HSL (left) and HSV (right) color models. Source: Wikipedia, retrieved 17:38, 11 September 2009 (UTC)

According to the CSS specification, retrieved 17:38, 11 September 2009 (UTC)

CSS3 adds numerical hue-saturation-lightness (HSL) colors as a complement to numerical RGB colors. It has been observed that RGB colors have the following limitations:

  • RGB is hardware-oriented: it reflects the use of CRTs.
  • RGB is non-intuitive. People can learn how to use RGB, but actually by internalizing how to translate hue, saturation and lightness, or something similar, to RGB.
Advantages of HSL are that it is symmetrical to lightness and darkness (which is not the case with HSV for example), and it is trivial to convert HSL to RGB.

CSS 3 and SVG implement the HSL model. Color (hue) is defined by the position in the color wheel, e.g. red = 0, red = 360, blue=240. Saturation and lightness are represented by percentages from 0 to 100%. Important: "normal" lightness is 50%.

Below is table of the main colors (at 30i intervals). We present three situations:

  • Saturation is set to 100% and lightness to 50% (normal)
  • Saturation of 50% and lightness of 25% (dark)
  • Saturation of 75% and lightness of 75% (pastel)

Warning: You need a somewhat CSS 3 capable browser like Firefox 3+ or IE 9 ( IE 8 will not work)

Hue Color name Sat=100% / Light=50% Sat=50% / Light=25% Sat=75% / light=75%
0 red 0 - red 0 - red 0 - red
30 orange 30 - orange 30 - orange 30 - orange
60 yellow 60 - yellow 60 - yellow 60 - yellow
90 yellow-green 90 - yellow-green 90 - yellow-green 90 - yellow-green
120 green 120 - green 120 - green 120 - green
150 green-cyan 150 - green-cyan 150 - green-cyan 150 - green-cyan
180 cyan 180 - cyan 180 - cyan 180 - cyan
210 cyan-blue 210 - cyan-blue 210 - cyan-blue 210 - cyan-blue
240 blue 240 - blue 240 - blue 240 - blue
270 blue-magenta 270 - blue-magenta 270 - blue-magenta 270 - blue-magenta
300 magenta 300 - magenta 300 - magenta 300 - magenta
330 magenta-red 330 - magenta-red 330 - magenta-red 330 - magenta-red
360 red 360 - red 360 - red 360 - red

Now, where is white an black ?

  • Any hue that is 100% saturated and 100% lightness is white
  • Any hue that is 0% saturated and/or lightness is black

So basically, an HSL color goes

  • From colorful to colorless in terms of saturation
  • From black to normal to white in terms of lightness (50% is normal)

Below is screen capture from the CSS Color Module Level 3 (Working draft, retrieved 17:38, 11 September 2009 (UTC)) that shows on the X axis the saturation (100%, 75%, 50%, 25%, 0%) and on the Y axis the lightness.

HSL Example - 0i red (Source: W3C)

Finally, here are some CSS code examples:

 { color: hsl(0, 100%, 50%) }   /* red */
 { color: hsl(120, 100%, 50%) } /* green */ 
 { color: hsl(120, 100%, 25%) } /* dark green */ 
 { color: hsl(120, 100%, 75%) } /* light green */ 
 { color: hsl(120, 75%, 75%) }  /* pastel green, and so on */

E.g. The following HSL CSS code

  <p style="color: hsl(240,75%,75%);"> Kind of not so blue</p>

would show like this (your browser may not support this):

Kind of not so blue

For more information about HSL (and HSV), we point again to Wikipedia's HSL and HSV article.

HSL colors with CSS3

In CSS, the hsl value is defined by the position in the color wheel, e.g. red = 0, red = 360, blue=240. Saturation and lightness are represented by percentages. Here are a few examples:

 { color: hsl(0, 100%, 50%) }   /* red */
 { color: hsl(120, 100%, 75%) } /* light green */ 
 { color: hsl(120, 75%, 75%) }  /* pastel green, and so on */

E.g. The following HSL CSS code

  <p style="color: hsl(240,75%,75%);"> Kind of not so blue</p>

would show like this (your browser may not support this):

Kind of not so blue

Alpha channel

In computer graphics, alpha compositing is the process of combining an image with a background to create the appearance of partial transparency (Wikipedia)

In more simple terms, you can set the alpha to some percentage:

  • 100% can't see through
  • 80% bad see trough
  • 50% in between
  • 30% good see through
  • 10% good see through, but very little color
  • 0% no color left
Alpha color channel

Hint: With the alpha channel you can create other effects than see-through "windows". E.g. you can overlay textures with color or the other way round.

In CSS3 (if your browser supports it), you also can define the alpha channel. You may use so-called RGBA values using the rgba () notation for RGB values. a in rgba stands for alpha. Some examples:

  p { background-color: rgba(0,0,255,0.5) }        /* semi-transparent solid blue */

The following RGBA CSS inline code

  <p style="float:right; background-color: rgba(0,0,255,0.3);"> Kind of blue</p>

would show like the "Kind of blue" box to the right (your browser may not support this):

Kind of blue

Alternatively you can use the opacity property that does the same, e.g.

 
 background: rgb(255, 0, 0) ; opacity: 0.2;">

Finally, in CSS 3, you also may use HSL with an alpha channel, i.e. define an hsla value:

/* HSL model with alpha channel */
p { color: hsla(120, 100%, 50%, 1) }  /* green */
p { color: hsla(120, 100%, 50%, 0.5) } /* semi-transparent green */
p { color: hsla(120, 100%, 50%, 0.1) } /* very transparent green */

CSS 2 Background properties

When you use a background image within an element it is by default tiled vertically and horizentally, under the condition that its x/y dimensions are smaller than the styled element.

The background-repeat property allows to change default settings. E.g. to inhibit repetition, use:

background-image: url(flower.png);
background-repeat: no-repeat;

html vs. body vs. universal background

Example file:

Using background images is more difficult than it may seem. In particular there are a few principles and issues that you should be aware of:

  • Pictures will shine through all elements, unless these elements explicitly define a background. In other words, if an element is transparent (this is the default), the picture underneath will show through empty spaces in the "box", i.e. the rectangle define by the element's boundaries.
  • Watch out for interactions between using the universal rule, the body and the html for setting a background. E.g. if you define a background picture in the universal rule, bits can shine through in various HTML elements, even if you defined a other background in the body element. This, because backgrounds are not inherited.
  • Various other properties also may have effects that may need some tuning. E.g. an none repeated background will display the picture as a whole.

If you set a background image like this, i.e. as default for all elements:

 
    * {
      background-image: url(tournesol.png);
      }

the pictures will fill the whole web browser window in the background. Images will not repeat again at each element level.

If you set a background image as "html" background like this:

 
 html {
       background-image: url(tournesol.png);
       }

then the whole visible window area will be filled with the picture.

If you set a background image as "body" background like this:

 
 body {
       background-image: url(tournesol.png);
       }

and if neither body nor the "*" has a background image it will fill the whole window as above.

However, if there is a background imageor color for the html tag or the universal selector, then only the the space taken up by the body will be filled. In particular, margins will not be filled and space left from the end of your content down to the end of the browser window will not be filled with this image.

Background image repetition

  • Repeat in the x direction
 
 background-repeat: repeat-x;

Positioning background images

Background pictures can be positioned with respect to the boundaries of the element.

E.g. if you insert a picture in the body you could put it at bottom right like this:

 
background-image: url(flower.png);
background-repeat: no-repeat;
background-position: bottom right;

background-position takes two values that represent both the y and the x offset, i.e. how much it will go down and to the right. If only one value is specified it refers to the horizontal x position.

Examples:

 
 background-position: 75px 150px;
 background-position: 60% 20px;

In order to center a picture, you could use:

 
background-image: url(flower.png);
background-repeat: no-repeat;
background-position: bottom right;

Fixing background images

You also can fix a background image with respect to the so-called viewport (what the users sees in the browser window), so that it stays when a user will scroll. More precisely: “If a background image is specified, this property specifies whether it is fixed with regard to the viewport ('fixed') or scrolls along with the containing block ('scroll').” (CSS 2 spec).

Example:

 
background-image: url(flower.png);
background-repeat: no-repeat;
background-attachment: fixed;

The background-attachment property will take the following values:

  • scrolll (this is the default)
  • fixed: Will scroll up-down/left-right when the user (or a script) uses scrolling.
  • inherit

Note: An image is only visible if it sits in the content, padding or border area of the element.

The background shorthand

The CSS3 syntax is the following (see some examples later):

background: background-color-value background-image-value background-repeat-value background-attachment-value background-position-value

Property values have to be presented in this order, but you can omit any you like:

Simple background examples:

 
 body {
  background:#ffffff url('flower.png') no-repeat right top;
  }
 body {
  background:#url('flower.png') no-repeat 50% top;
 }

CSS3 background properties

This is not complete, we only show some properties of the CSS Backgrounds and Borders Module Level 3 (draft standard, 15 feb 2011).

Multiple images

In CSS3 (only), one also can define multiple background images in two different ways. For now, just a simple example:

 
p {
background: 
url(one.jpg) top center no-repeat,
url(two.png) 40% 20px no-repeat,
url(three.png) 60% 20px no-repeat,
url(four.png) 3em 3em repeat-x,
url(car.gif) center no-repeat;
}

Notice how these background image definitions are separated by commas !

CSS 3 background size and background origin

The CSS3 (only!) property background-size allows specify the size of background images using length, percentages, or contain, cover or auto.

This property is defined in the CSS Backgrounds and Borders Module Level 3 W3C Candidate Recommendation 15 February 2011. As of nov 2001 this should be implement in most recent browsers.

The most interesting value is cover since it allows to deal with "fluid" designs, i.e. web pages that adapt to the size of the screen.

Examples:

background-size: 300px;       /* 300 px wide and height is auto*/
background-size: 300px 200px; /* 300px width and 200px height */
background-size: 50% 50%;     /* set size = 50% of the parent element */
background-size: auto;
background-size: contain;     /* largest image size that makes it fit inside */
background-size: cover;       /* smallest image size that makes it fit inside */

If it doesn't work in your (older) browser, you could try:

 -moz-background-size: 
 -o-background-size: 
 -webkit-background-size:

In addition, you may have to use the background-origin property that specifies the background positioning area. The example lines should be self-explaining.

Examples:

 background-origin: padding-box /* default value */
 background-origin: border-box 
 background-origin: content-box

Examples:

CSS 3 gradients

<div height="100px" 
     style="background: linear-gradient(top, #eff484 0%,#1e5799 100%);">
Hello
</div>

The following example shows the kind of code that you now have to insert for dealing with current and last generation browsers.

 
background: #eff484; /* Old browsers */
background: -moz-linear-gradient(top,  #eff484 0%, #1e5799 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#eff484), color-stop(100%,#1e5799)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  #eff484 0%,#1e5799 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  #eff484 0%,#1e5799 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  #eff484 0%,#1e5799 100%); /* IE10+ */
background: linear-gradient(top,  #eff484 0%,#1e5799 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#eff484', endColorstr='#1e5799',GradientType=0 ); /* IE6-9 using ActiveX */

Border images

  • Firstly, you need to set the with of the border, i.e. rather large
  • The border-image property then will reuse these values
  • The stretch keyword means that the image should be stretched to match the sizes above.

Example:

 
 border-style: solid;
 border-color: #930;
 border-width: 20px 40px 40px 40px;
 border-image: url(frame.png) 20 40 40 40 stretch stretch;
 -webkit-border-image: url(frame.png) 20 40 40 40 stretch stretch;
 -moz-border-image: url(frame.png) 20 40 40 40 stretch round;

Tools and links

Tools

Firefox color picker
  • ColorZilla includes an Eyedropper, ColorPicker, Page Zoomer and other colorful goodies.
Gradient Generator
  • CSS3 gradient generator is on-line tool (free) for creating CSS 3 gradients. It generates both CSS3 code and four vendor-specific codes that one could use before CSS is fully implemented.

Introductions

Color

Reference

CSS specification pages at W3C:

Color tables: