Faster PHP Cloud Hosting

How to Set Colors and Backgrounds in CSS: From Named Colors to Gradients

logo icon

Admin Nganggur

-

2025 August 05

iamge article

The visual appearance of a website is an important element in attracting visitors. The use of colors and backgrounds is one of the most important aspects of website design. With CSS, we can set text colors, background elements, and even create beautiful gradient effects. In this article, we will discuss in detail how to set colors and backgrounds in CSS from using color names to creating gradient effects on backgrounds.

Color Properties in CSS

Before that, we need to first understand what properties are used to set colors in CSS.

  1. color Sets the color of the text
  2. background-color Sets the background color of the element. Each example of its use is as follows.
p {
 color: red;
 background-color: gray;
}

Determining Colors in CSS

There are several ways to determine colors in CSS, as follows.

  • Color Names

It is common to specify colors in CSS using color names. CSS also supports approximately 140 standard colors.

p {
 color: crimson;
}
  • Hexadecimal Code (HEX Code)

This format uses numbers and letters after the # sign.

div {
 background-color: #0f0f0f;
}
  • RGB (Red, Green, Blue)

This format specifies colors using a combination of red, green, and blue values.

div {
 background-color: rgb(255, 0, 0);
}
  • RGBA (Red, Green, Blue, Alpha)

Similar to RGB, but with an additional Alpha value for transparency (opacity). The minimum value for alpha is 0, and the maximum value is 1.

div {
 background-color: rgba(0, 0, 0, 0.5);
}

Background Gradient in CSS

We can create color gradients in CSS to create smooth color transition effects. We will use linear-gradient and radial-gradient as examples.

  1. Linear Gradient

Gradient from top to bottom, left to right, or other directions.

div {
 background-color: linear-gradient(to right, #00c6ff, #0072ff);
}

Explanation of the code above:

  • linear-gradient is a function used to create a linear color gradient.
  • to right determines the direction of the gradient. It can also be set to to bottom, to left, or to top right.
  • #00c6ff is the starting color of the gradient.
  • #0072ff is the ending color of the gradient.
  1. Radial Gradient

A type of gradient in CSS that creates a color transition radiating from the center point outward, forming a circle or ellipse.

section {
  background: radial-gradient(circle, #ffcc70, #ff5757);
}

The explanation of the code above is as follows:

  • radial-gradient function to create a radial gradient.
  • circle specifies the shape of the gradient. Alternatives include ellipse, closest-side, or farthest-corner.
  • #ffcc70 is the starting color of the gradient.
  • #0072ff is the ending color of the gradient.

Tips for Effective Use of Colors and Backgrounds

  1. Limit the number of primary colors to a maximum of 3-4 to maintain a consistent appearance.
  2. Match colors to the website theme to create a professional look.
  3. Use neutral colors like gray or black for small text.
  4. Don't overuse gradient effects.
  5. Be consistent in using action colors for buttons, links, or interactive elements so users can easily recognize their functions.

Conclusion

Mastering colors and backgrounds in CSS is an important step in creating an attractive and professional website appearance. By understanding this material, we can create designs that are interactive, responsive, and aesthetically pleasing without having to use images or additional plugins.

Thank you, see you in the next article.

Tags :

css

gradient css

color css

learn coding with nganggurdev

learn css basic

how to set color in css

web

web development

Like the articles on NganggurDev? Let's give support to the writer. Thank you very much!
Load WordPress Sites in as fast as 37ms!

Want to read more? Click me!