Code Highlights logo

Learn

Visual Rules

Color and Background Color

Color is an essential part of visual design and styling. You can use colors to set the mood of your web page and grab the attention of your users. Let's discuss some ways you can use colors in your CSS code.

Setting Text Color

You can set the text color of an HTML element using the color property in CSS. The value of the property can be any valid CSS color.

1h1 {
2 color: #f00; /* This will set the color of all h1 elements to red */
3}

You can also set the color of multiple elements at once by using a class or an ID, or by selecting all elements of a specific type.

Setting Background Color

You can set the background color of an HTML element using the background-color property in CSS. The value of the property can be any valid CSS color.

1body {
2 background-color: #fff; /* This will set the background color of the body element to white */
3}

You can also set the background color of multiple elements at once by using a class or an ID, or by selecting all elements of a specific type.

Using RGBA Colors

RGBA colors allow you to set the color of an element while also controlling its opacity. The rgba() function uses four values: red, green, blue, and alpha (opacity). The red, green, and blue values are expressed as numbers between 0 and 255, while the alpha value is expressed as a number between 0 and 1.

1h1 {
2 color: rgba(255, 0, 0, 0.5); /* This will set the color of all h1 elements to semi-transparent red */
3 background-color: rgba(0, 0, 255, 0.2); /* This will set the background color of all h1 elements to semi-transparent blue */
4}

Using Color Names

CSS has a number of predefined color names that you can use instead of specifying the color using an RGB or hex value. Some of the most common color names include red, green, blue, yellow, black, and white.

1h1 {
2 color: blue; /* This will set the color of all h1 elements to blue */
3}

Remember, you want to choose colors that evoke the emotions you want in your users. Use color creatively to leave a lasting impression on your audience.


Instructions

1.

Change the background color of the body to rgba(255, 0, 0, 0.5).

2.

Change the text color of the table element to #fff.

Sign up to start coding

Already have an account?

Sign In

Course content