Monday 26 August 2013

Backgrounds & Gradients - Part2

Radial Gradient Background

While the linear gradient is perfect for building a gradient propagating in one direction, often the need for a radial gradient arises. Radial background gradients work just like linear gradients and share a lot of the same values. However, radial gradients can be quite complex with values for location, size, radius, and more. We’ll cover the basics, but please feel free to take a deeper dive into radial gradients.
  1. div {
  2. background: #70bf32;
  3. background: url('radial-gradient.png') 50% 50% no-repeat;
  4. background: radial-gradient(#a1e048, #6a942f);
  5. }
Radial Gradient Background

CSS3 Gradient Background Generator

Programming CSS3 gradients from hand can be quite a task, especially if you are not all that familiar with them. Fortunately, quite a few CSS3 gradient generators have popped up. Each generator works a little differently, some coming with presets and examples and some having an expansive list of options. If interested, I recommend doing some research to find the right generator for your needs.

Gradient Background Stops

So far we have discussed transitioning a gradient from one color to another, however if you want to transition more than two colors you can use color stops. Instead of declaring two color values, you can declare numerous values. The browser will transition from one to the next accordingly. Adding a length value to these color stops determines at what position and distance the transition will take place. If no length value is declared the gradient will transition evenly between all colors declared.
  1. div {
  2. background: #6c9730;
  3. background: url('linear-gradient-stops.png') 0 0 repeat-y;
  4. background: linear-gradient(to right, #8dc63f, #d8ad45, #3b4b94);
  5. }
Gradient Background Stops

Navigation Background Example

HTML
  1. <ul>
  2. <li class="play"><a href="#">Play</a></li>
  3. <li class="back"><a href="#">Skip Backward</a></li>
  4. <li class="stop"><a href="#">Pause/Stop</a></li>
  5. <li class="forward"><a href="#">Skip Forward</a></li>
  6. </ul>
CSS
  1. ul {
  2. background: #f4f4f4;
  3. background: linear-gradient(#fff, #eee);
  4. border: 1px solid #ccc;
  5. border-radius: 6px;
  6. display: inline-block;
  7. height: 22px;
  8. list-style: none;
  9. margin: 0 0 20px 0;
  10. padding: 0 4px 0 0;
  11. }
  12. li {
  13. height: 16px;
  14. float: left;
  15. padding: 3px;
  16. text-indent: -9999px;
  17. width: 16px;
  18. }
  19. .play {
  20. background: #f4f4f4;
  21. background: linear-gradient(#fff, #eee);
  22. border: 1px solid #ccc;
  23. border-radius: 30px;
  24. left: -4px;
  25. padding: 7px;
  26. position: relative;
  27. top: -5px;
  28. }
  29. li a {
  30. background: url('controls.png') 0 0 no-repeat;
  31. display: block;
  32. height: 16px;
  33. width: 16px;
  34. }
  35. .play a:hover {
  36. background-position: 0 -16px;
  37. }
  38. .back a {
  39. background-position: -16px 0;
  40. }
  41. .back a:hover {
  42. background-position: -16px -16px;
  43. }
  44. .stop a {
  45. background-position: -32px 0;
  46. }
  47. .stop a:hover {
  48. background-position: -32px -16px;
  49. }
  50. .forward a {
  51. background-position: -48px 0;
  52. }
  53. .forward a:hover {
  54. background-position: -48px -16px;
  55. }

Demo

Multiple Background Images with CSS3

It used to be if you wanted an element to have more than one background you would have to wrap it with another element and assign a background to the wrapping element. This created bloated code for the simple use of adding additional backgrounds. Now with CSS3 we can use multiple background images on a single element by chaining these background values together.
  1. div {
  2. background:
  3. url('foreground.png') no-repeat 0 0,
  4. url('middle-ground.png') no-repeat 0 0,
  5. url('background.png') no-repeat 0 0;
  6. }
Not only can you chain background property values, you can also chain together background-repeat,background-position, and other background related properties.

Multiple Background Images Example

HTML
  1. <div>Dinosaur with Grass and Clouds</div>
CSS
  1. div {
  2. background:
  3. url('dyno.png') no-repeat 380px 18px,
  4. url('grass.png') no-repeat 0 100%,
  5. url('sky.jpg') no-repeat 0 0;
  6. border-radius: 6px;
  7. height: 200px;
  8. }

Demo

Dinosaur with Grass and Clouds

New CSS3 Background Properties

Along with gradient backgrounds and multiple background images, CSS3 also introduces three new CSS properties,background-sizebackground-clip, and background-origin.

CSS3 Background Size

The background-size property allows you to specify a specific size for your background image. The first value declared is the width of the image and the second value is the height. These values may include any length measurement or keyword, most notably pixels and percentages. If only one value is declared, the auto keyword may be used to keep the proper image proportions.
  1. div {
  2. background: url('shay.jpg') 0 0 no-repeat;
  3. background-size: 85% auto;
  4. border: 1px dashed #8c9198;
  5. height: 240px;
  6. width: 200px;
  7. }

Cover & Contain Values

The cover keyword value specifies that the background image should be resized proportionally to completely cover an element. How the background will be resized depends on the dimensions of the background and element. While the background will hold its dimensions proportionally, the quality of the image may be resized in a way that it distorts the image. Always make sure to check your work.
The contain keyword value will resize a background image proportionally to keep it within the confines of the element. This may mean that there are parts of the element without a background, however the entire background image will be visible. As with the cover keyword value, the resizing of the background image will be proportional to the image’s dimensions, however it may result in a distorted image.

CSS3 Background Clip & Background Origin

The background-clip property specifies the area a background image will cover and the background-originproperty specifies where the background-position should originate. The introduction of these two new properties also includes three new values: border-boxpadding-box, and content-box. Each of these three values may be used to set a value for the background-clip and background-origin properties.
  1. BACKGROUND CLIP & ORIGINdiv {
  2. background: url('shay.jpg') 0 0 no-repeat;
  3. background-clip: padding-box;
  4. background-origin: border-box;
  5. }
Border Box Value
Fig. 5.03The border-box value extends into the border of an element.
Padding Box Value
Fig. 5.04The padding-box value extends into the padding of an element, but is contained within any border.
Content Box Value
Fig. 5.05The content-box value is contained within the border and padding of an element.