Skip to main content

Command Palette

Search for a command to run...

CSS Colors Tutorial

Learn How to Work With the Different Types of Colors in CSS

Updated
3 min read
CSS Colors Tutorial
R

Passionate Software Engineer with three years of experience, dedicated to creating robust and scalable web applications. I have a strong foundation in JavaScript, React, Python, and Node.js, and enjoy working with modern tech stacks to build innovative solutions. I'm committed to writing clean and efficient code, following best practices, and staying updated with the latest industry trends.

Throughout my career, I've gained valuable experience in full-stack development, including frontend design, backend development, and database management. I love solving complex problems and implementing creative solutions that enhance user experiences. I believe in the power of collaboration and thrive in cross-functional teams where I can contribute my skills and learn from others.

Beyond coding, I am an avid learner and enjoy sharing my knowledge with the developer community. I regularly contribute to open-source projects, write technical blog posts, and engage in discussions on emerging technologies. I'm always seeking opportunities to expand my skill set and stay at the forefront of software engineering.

In this tutorial, we’ll be learning about colors in CSS.

Without CSS, our web pages would look pretty dull. In HTML we get a plain white background, black text, and blue links. And that’s about it.

So let’s use CSS to add some color!

Working with colors

The CSS color property sets the color of our text: CSS Colors The color property can accept any CSS color value. Which are categorized as: named colors, hex colors, RGB & RGBa colors or HSL & HSLa colors.

Let’s take a look at each!

Named Colors

CSS Colors Named colors (or keyword colors) are CSS keywords that define colors. Such as red, blue, lime, aqua, light coral, etc.

CSS originally started with 16 colors, however today there are tons of options. Check out the list of supported colors.

Named colors are great for convenience. However, to get more specific colors we should look at other options.

Hex Colors

Hex (or hexadecimal) colors are defined with alphanumeric values.

For example: Hex Color Each hex color is expressed as a six-digit combination of numbers and letters defined by its mix of red, green, and blue (RGB).

In the above example, the first two characters represent the red value, the second two the green value, and the third the blue value. The range is from 00 to FF.

The hex code is essentially shorthand for its RGB value!

In the case where our pairs of red, blue, and green values are all doubles, we can abbreviate the value to a 3 character shorthand. So #FFFFFF can be abbreviated to #FFF. Hex Color

RGB and RGBa Colors

Alternatively, you can use rgb() to calculate color from its RGB notation.

To define our colors, we use a comma-separated list of three numeric values (ranging from 0 to 255). The first represents the red value, the second is the green value, and the third is the blue value. For example: RGB Color We can use rgba() to adjust the opacity. With RGBa we add a fourth value. The value is a number within a range from 0.0 (fully transparent) to 1 (fully opaque). For example: RGBA Color

HSL and HSLa Colors

HSL (Hue Saturation Lightness) colors are a more recent addition to CSS.

As with RGB we also use a comma-separated list of three values. The first is the degree of Hue (from 0 to 360), then a Saturation percentage (from 0% to 100%), and then a Lightness percentage (from 0% to 100%). HSL Color HSL colors are opaque by default.

To add opacity, use hsla() adding a fourth value ranging from 0.0 (fully transparent) to 1 (fully opaque). HSLa Color

Color accessibility

When working with colors we should always be mindful of accessibility. Ensuring the contrast ratio between the color of the text and the background meets a certain standard. This way people with vision considerations are still able to read the content of the page.

The ratio is determined by comparing the luminosity of the text and background color values. And we can compare colors with the Web Content Accessibility Guidelines (WCAG) color checker tool.

Conclusion

And there you go! We’ve seen how to add color to our pages with named, HEX, RGB & HSL colors. Next up we’ll be looking at working with backgrounds in CSS. See you then!

If you liked this post, make sure to follow me on Twitter where I post daily about Tech related things!

https://www.buymeacoffee.com/rembertdesigns

🌎 Let's Connect

E

I enjoy reading these CSS articles, they are easily digestible, and you get to learn sth new :)

R

Thank you very much! I appreciate you taking the time to read them & giving feedback! Thank you again!

CSS Tutorials

Part 8 of 25

Learn the ins and outs of CSS with my comprehensive blog series! From the basics of CSS syntax to advanced techniques like responsive design and CSS animations, these tutorials will help you!

Up next

CSS Backgrounds Tutorial

Learn How to Change the Backgrounds of CSS Elements

More from this blog

Rembert Designs Blog | Helping Web Developers And Designers with Resources

107 posts

Passionate Software Engineer with three years of experience, dedicated to creating robust and scalable web applications. I have a strong foundation in JavaScript, React, Python, and Node.js.