
How to Use vh and vw Units in CSS
11/10/2019
How to Create a Parallax Effect with HTML and CSS
11/10/2019
5. CSS colors and backgrounds
This section covers the CSS properties that control colors and backgrounds.
5.1. Ways to write CSS colors
You can specify a color in several ways:
- Named color: red, yellow, blue, green, and others.
- Hexadecimal: #RRGGBB or #RRGGBBAA.
- RGB: red, green, blue.
- RGBA: red, green, blue, and alpha (opacity).
- HSL: hue, saturation, lightness.
- HSLA: hue, saturation, lightness, and alpha.
This course mainly uses named colors, hexadecimal values, and RGB or RGBA.
5.2. Basic color keywords
CSS recognizes more than 140 named colors. The table shows some common ones and their equivalent HEX and RGB values.
| Color | Name | HEX | RGB |
|---|---|---|---|
| black | #000000 | 0,0,0 | |
| white | #ffffff | 255,255,255 | |
| red | #ff0000 | 255,0,0 | |
| blue | #0000ff | 0,0,255 | |
| yellow | #ffff00 | 255,255,0 | |
| gray | #808080 | 128,128,128 | |
| green | #008000 | 0,128,0 | |
| lime | #00ff00 | 0,255,0 |
CodePen example: Hexadecimal colors with transparency.
5.3. Common properties
Here are some of the most frequently used CSS properties for colors and backgrounds.
| Property | Description | Values |
|---|---|---|
color | Text color | RGB | HSL | HEX | color keyword | RGBA | HSLA |
background-color | Background color | RGB | HSL | HEX | color keyword | RGBA | HSLA | transparent |
background-image | Background image | url(“…”) | none |
background-repeat | Background tiling | repeat | repeat-x | repeat-y | no-repeat |
background-attachment | Background scrolling | scroll | fixed |
background-position | Background position | percentage | length | left | center | right |
background-size | Background size | auto | cover | contain | custom size |
Opacity | Element opacity | [ 0 – 1 ] (0 → fully transparent) |
5.3.1. color: Text color
Use color to set the color of text in an HTML element.
Example:
p { color: #FF0000; /* Red in hexadecimal notation */ }
5.3.2. background-color: Background color
Use background-color to set the background color of an element such as a div or a section.
Example:
div { background-color: rgb(0, 128, 255); /* Blue background in RGB notation */ }
Exercise: Compare background-color values
View the results of the following styles and compare the values. Pay particular attention to the difference between setting opacity on the element and using an alpha channel on its background color.
.a { background-color: red; }
.b { background-color: #FF0000; }
.c { background-color: RGB(255,0,0); }
.d { background-color: HSL(0,100%,50%); }
.e { background-color: red; opacity: 0.5; }
.f { background-color: rgba(255, 0, 0, 0.5); }
.g { background-color: transparent; }
5.3.3. background-image: Background images
Use background-image to place an image behind an HTML element. Pass the image path to url("…"). Use none to remove the background image.
Example:
section { background-image: url("background.jpg"); }
Exercise: Compare background-image styles
Apply these rules individually to a web page and explain what changes in each case.
body { background-image: url("background.jpg");}
body { background-image: url("background.jpg"); background-repeat: repeat-x;}
body { background-image: url("background.jpg"); background-repeat: repeat-y;}
body { background-image: url("background.jpg"); background-repeat: no-repeat;}
body { background-image: url("background.jpg"); background-repeat: repeat;}
body { background-image: url("background.jpg"); background-size: 100px 100px;}
5.3.4. background-repeat: Tiling the background image
Use background-repeat to control how the image tiles. The values repeat, repeat-x, repeat-y, and no-repeat repeat it in both directions, horizontally, vertically, or neither, respectively.
Example:
header { background-image: url("background-pattern.png"); background-repeat: repeat-x; /* Repeat horizontally only */ }
Exercise: Compare fixed and scrolling backgrounds
The background-attachment property determines whether the image stays in place or scrolls with the page. Explain what you see when you try the two rules below.
body { background-image: url("background.jpg"); background-repeat: no-repeat; background-attachment: fixed;}
body { background-image: url("background.jpg"); background-repeat: no-repeat; background-attachment: scroll;}
5.3.5. background-attachment: Background scrolling
Use background-attachment to make the background image scroll with the element (scroll) or stay fixed relative to the viewport (fixed).
Example:
body { background-image: url("starry-background.jpg"); background-attachment: fixed; /* Fixed relative to the viewport */ }
5.3.6. background-position: Positioning the background image
Use background-position to define the image’s starting position in the background positioning area.
Examples:
Top center: Align the image with the top edge and center it horizontally.
div { background-image: url("background.jpg"); background-position: center top; }
Top right: Place the image in the upper-right corner.
div { background-image: url("background.jpg"); background-position: right top; }
Bottom left: Place the image in the lower-left corner.
div { background-image: url("background.jpg"); background-position: left bottom; }
Pixel offsets: Specify exact horizontal and vertical offsets in pixels.
div { background-image: url("background.jpg"); background-position: 50px 30px; /* 50px from the left, 30px from the top */ }
Centered: Center the background image both vertically and horizontally.
div { background-image: url("background.jpg"); background-position: center center; }
Percentages: Align the specified point on the image with the same percentage point in the background positioning area.
div { background-image: url("background.jpg"); background-position: 25% 75%; /* Align the 25% horizontal and 75% vertical points */ }
5.3.7. background-size: Sizing the background image
Use background-size to control the size of the image relative to the element it paints. The syntax is:
background-size: value;
Possible values include:
- auto: Lets the browser determine the natural dimension of the image, where available.
background-repeatcontrols whether the image tiles. - cover: Scales the image proportionally until it covers the entire area. Parts outside the area are clipped.
- contain: Scales the entire image to fit inside the area without cropping. If the aspect ratios differ, there can be space around it.
- Custom size: Use lengths or percentages. You can also set one dimension explicitly and let
autodetermine the other one.
Examples:
/* Scale the image to cover the container */ background-size: cover; /* Scale the entire image to fit inside the container */ background-size: contain; /* Set the width to 200px and calculate the height automatically */ background-size: 200px auto; /* Set the width to 50% and the height to 100px */ background-size: 50% 100px;
This property is useful for images in a page header, section, or other container where you need to control how the image fits as the element changes size.
5.3.8. opacity: Element transparency
Use opacity to control the transparency of the whole element, including its content. A value of 1 is fully opaque and 0 is fully transparent. Values between them produce partial transparency.
Example:
.translucent-box { background-color: #000; /* Black background */ opacity: 0.7; /* 70% opacity */ }
5.4. Practical example: A parallax-style background
The example is a call to action. A section with a background image invites visitors to do something specific, such as buy a product or subscribe to a service.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Call to Action</title>
<style>
*{font-family: Arial;}
.cta-container {
background-image: url("cta-background.jpg"); /* Background image */
background-size: cover; /* Cover the entire container */
background-position: center center; /* Center the image */
background-repeat: no-repeat; /* Do not tile the background image */
background-attachment: fixed; /* Fixed relative to the viewport */
opacity: 0.9; /* Container opacity */
text-align: center; /* Center the content */
padding: 60px;
}
.cta-text {
font-size: 24px;
}
.cta-button {
background-color: #FF5733; /* Button background color */
color: #fff; /* White button text */
padding: 10px 20px; /* Button padding */
border: none; /* No border */
border-radius: 5px; /* Rounded corners */
font-size: 18px;
cursor: pointer; /* Show a pointer cursor on hover */
}
</style>
</head>
<body>
<div class="cta-container">
<h2 class="cta-text">Discover our featured product!</h2>
<p class="cta-text">Get 20% off your first purchase.</p>
<button class="cta-button">Shop now</button>
</div>
</body>
</html>
The cta-container uses cta-background.jpg as its background image and applies the following properties:
- background-size: cover scales the image to cover the container.
- background-position: center center centers the image both vertically and horizontally.
- background-repeat: no-repeat prevents the image from tiling.
- background-attachment: fixed anchors the image to the viewport. The difference in movement between the image and the page creates the parallax effect.
- opacity: 0.9 renders the entire section, including its image and content, at 90% opacity.
The cta-text is centered. The cta-button has a colored background, white text, rounded corners, and a pointer cursor on hover.
Now compare a second call to action without background-attachment: fixed. The image scrolls with its section, so the parallax effect is absent.
.hero-image {
background-image: url("https://www.eniun.com/wp-content/uploads/eniun-home-historia.jpg");
height: 400px;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
}