CSS - Color References

-

Color Names

CSS offers a set of predefined color names that you can use to specify colors in your styles. These color names are easy to remember and provide a quick way to apply colors without needing to know the specific hexadecimal, RGB, or HSL values. Some commonly used color names include red, blue, green, yellow, purple, orange, black, white, and gray.

Example: Set the text color to red

p {
  color: red;
}

You can also set the background color of an element to blue using the background-color property:

Example: Set the background color to blue

div {
  background-color: blue;
}

While color names are handy for quick prototyping or when you need a basic set of colors, they have some limitations. The range of colors available through predefined names is limited compared to the vast possibilities offered by hexadecimal, RGB, and HSL values. Color names may not provide the exact shade or variation you need for your design. Using color names can make your code less maintainable as it becomes harder to make global changes across your stylesheets.

Despite these limitations, color names still have their place in CSS for beginners or when you need to apply colors quickly without worrying about specific values. It's important to be aware of these trade-offs and consider using more flexible notations like hexadecimal or RGB values for more control and precision in your choices.

Hexadecimal Color Values

Hexadecimal color notation is a common way to specify colors in CSS. It provides a compact method for representing many colors. Hexadecimal color codes consist of a hash symbol (#) followed by six characters, where each pair of characters represents the intensity of red, green, and blue (RGB) components.

The structure of a hexadecimal color code is as follows:

#RRGGBB
  • RR represents the red component (00 to FF)
  • GG represents the green component (00 to FF)
  • BB represents the blue component (00 to FF)

The values for each component range from 00 (lowest intensity) to FF (highest intensity). For example, #000000 represents black (no color intensity), while #FFFFFF represents white (full color intensity).

Example usage of hexadecimal color values in CSS

h1 {
  color: #FF0000; /* Red */
}

You can also set the background color of an element using a hexadecimal value:

Example: Setting background color in CSS

div {
  background-color: #00FF00; /* Green */
}

Hexadecimal notation supports many colors. By combining different values for red, green, and blue components, you can create any needed color. For example:

Examples of various hexadecimal color values in CSS

p {
  color: #0000FF; /* Blue */
}

.highlight {
  background-color: #FFFF00; /* Yellow */
}

a {
  color: #800080; /* Purple */
}

Hexadecimal notation is case-insensitive so you can use both uppercase and lowercase letters for component values (e.g., #FF0000 is the same as #ff0000).

Using hexadecimal values gives you precise control over colors on your web pages. They are widely supported by browsers and commonly used in CSS frameworks and design tools. Hexadecimal notation allows for many options and is an important skill for web developers and designers.

RGB Color Values

The RGB (Red, Green, Blue) color model is a way to specify colors in CSS. It represents colors by mixing different levels of red, green, and blue light. RGB color values in CSS are specified using the rgb() function, which takes three arguments representing the red, green, and blue components.

The structure of an RGB color value in CSS is as follows:

rgb(red, green, blue)

Each component value ranges from 0 to 255. For example:

  • rgb(255, 0, 0) represents pure red
  • rgb(0, 255, 0) represents pure green
  • rgb(0, 0, 255) represents pure blue

Setting text color using RGB values

Example: Setting text color using RGB values

p {
  color: rgb(255, 0 ,0); /* Red */
}

You can mix different levels of red, green and blue to create different colors:

Examples of RGB color values in CSS

Examples of RGB color values in CSS

h1 {
    color: rgb(0 ,128 ,0); /* Green */
}

.highlight {
    background-color: rgb(255 ,255 ,0); /* Yellow */
}

a {
    color: rgb(128 ,0 ,128); /* Purple */
}

RGB allows you to create many colors by changing the level of each component. This offers more control than using predefined names.

CSS also supports RGBA (Red Green Blue Alpha) values which add an opacity component to the color. The opacity value ranges from zero (full transparency) to one (full opacity).

Using RGBA for transparency

Example: Using RGBA for transparency

div { 
    background-color: rgba(255 ,00 ,00 ,.5); /* Red with fifty percent opacity */ 
} 

Using RGBA allows you to create colors with transparency without affecting the entire element's opacity. This is useful for creating overlays and other effects where you need control over individual colors' transparency.

RGB and RGBA are widely supported in CSS and give you a broad range of options compared to names commonly used in web design.

HSL Color Values

HSL (Hue, Saturation, Lightness) is another color model used in CSS to specify colors. It provides a way to describe colors based on their hue, saturation, and lightness values. The HSL color model is represented using the hsl() function in CSS.

The structure of an HSL color value in CSS is as follows:

hsl(hue, saturation, lightness)
  • Hue: Represents the color wheel and is specified as an angle between 0 and 360 degrees. 0 degrees represents red, 120 degrees represents green, and 240 degrees represents blue.
  • Saturation: Represents the intensity of the color and is specified as a percentage from 0% (no saturation) to 100% (full saturation).
  • Lightness: Represents the brightness of the color and is specified as a percentage from 0% (black) to 100% (white).

Setting text color using HSL values

Example: Setting text color using HSL values

p {
    color: hsl(0, 100%, 50%); /* Red */
}

Examples of HSL color values in CSS

Example of HSL color values in CSS

h1 {
    color: hsl(120, 100%, 50%); /* Green */
}

.highlight {
    background-color: hsl(60, 100%, 50%); /* Yellow */
}

a {
    color: hsl(300, 100%, 50%); /* Purple */
}

The HSL model allows you to create many colors by adjusting hue, saturation, and lightness. It provides an intuitive way compared to hexadecimal or RGB values.

Similar to RGBA, CSS also supports HSLA (Hue, Saturation, Lightness, Alpha) values which add opacity component. The opacity value ranges from zero (fully transparent) to one (fully opaque).

Using HSLA for transparency

Example: Using HSLA for transparency

div {
    background-color: hsla(0, 100%, 50%, 0.5); /* Red with fifty percent opacity */
}

Using HSLA allows you to control transparency of individual colors without affecting the entire element's opacity. This is useful for creating translucent overlays, gradients, and other visual effects.

HSL and HSLA provide a flexible way to specify colors. CSS offers an intuitive approach to selection that allows easy adjustments to create variations. HSL is a good choice when you want to create schemes based on a single hue or need to make subtle adjustments to a design.

Color Keywords

In addition to color names, hexadecimal values, RGB, and HSL, CSS also provides a set of color keywords. Color keywords are predefined color values that have a specific meaning or purpose in web design. These keywords offer a quick way to apply colors without having to remember specific color codes.

Some commonly used color keywords in CSS include:

  • transparent: Represents a fully transparent color.
  • currentColor: Represents the value of an element's color property. It allows you to inherit the color value from the parent element or use the current color value for other properties like border-color or background-color.
  • inherit: Inherits the color value from the parent element. This keyword is useful when you want an element to have the same color as its parent.

Using Color Keywords in CSS

Example: Using the transparent keyword

.overlay {
  background-color: transparent;
}

Example: Using the currentColor keyword

.button {
  color: blue;
  border: 2px solid currentColor;
}

Example: Using the inherit keyword

.child-element {
  color: inherit;
}

.overlay has a transparent background, allowing content behind it to be visible. This is useful for creating overlays or semi-transparent elements.

.button uses currentColor. The text is blue and by using currentColor for border-color, both text and border will be blue. If you change the text color later, the border will update automatically.

.child-element uses inherit. It takes its parent's text color without needing explicit specification.

Color keywords provide a way to apply colors based on specific meanings or relationships between elements. They can be used with other notations like hexadecimal values, RGB, or HSL for flexible styling in your CSS.

CSS Color Properties

CSS provides properties that accept color values, allowing you to apply colors to different parts of an element. The commonly used color properties in CSS are color, background-color, and border-color.

Setting Text Color

The color property sets the text color of an element. It can be applied to any element that contains text, such as paragraphs, headings, links, or span elements.

Example: Setting Text Color

p {
  color: blue;
}

h1 {
  color: #ff0000;
}

a {
  color: rgb(0, 128, 0);
}

In these examples, the color property sets the text color of paragraphs to blue, headings to red using a hexadecimal value, and links to green using an RGB value.

Setting Background Color

The background-color property sets the background color of an element. It can be applied to any element.

Example: Setting Background Color

div {
  background-color: yellow;
}

.container {
  background-color: #f0f0f0;
}

button {
  background-color: hsla(240, 100%, 50%, 0.8);
}

In these examples, the background-color property sets the background color of a div element to yellow, a container class to light gray using a hexadecimal value, and a button to semi-transparent blue using an HSLA value.

Setting Border Color

The border-color property sets the border's color for an element. It can be applied if the element has a border.

Example: Setting Border Color

.box {
  border: 2px solid;
  border-color: red;
}

input[type="text"] {
  border:1px solid; 
  border-color:#ccc; 
} 

button { 
  border: 3px solid; 
  border-color: rgb(0, 0, 0); 
} 

In these examples, the border-color property sets the border color for a box class to red, an input field to light gray using a hexadecimal value, and a button to black using an RGB value. These color properties accept various color notations such as color names, hexadecimal values, RGB, RGBA, HSL, and HSLA. You can use these notations according to the color you want to apply. By using these color properties, you can control the text color, background color, and border color of elements in your web pages, creating visually appealing and consistent designs.

Color Transparency and Opacity

Transparency and opacity are concepts in CSS that let you control the visibility of colors and elements. CSS provides ways to apply transparency to colors using RGBA and HSLA color values, and control the opacity of elements using the opacity property.

When working with colors, you can use RGBA (Red, Green, Blue, Alpha) or HSLA (Hue, Saturation, Lightness, Alpha) color values to specify a color with transparency. The alpha value in RGBA and HSLA represents the opacity of the color; 0 means fully transparent and 1 means fully opaque.

Using RGBA for Color Transparency

To apply transparency to a color using RGBA, use the rgba() function:

Example: Using RGBA for Color Transparency

.overlay {
  background-color: rgba(255, 0, 0, 0.5); /* Red with 50% opacity */
}

p {
  color: rgba(0, 0, 255, 0.8); /* Blue with 80% opacity */
}
  • The .overlay element has a semi-transparent red background.
  • The text in paragraphs is set to blue with an alpha value of .8.

Using HSLA for Color Transparency

Similar to RGBA:

Example: Using HSLA for Color Transparency

.button {
  background-color: hsla(120%,100%,50%, .6); /* Green with .6 opacity */
}

a {
  color: hsla(240%,100%,50%, .9); /* Blue with .9 opacity */
}
  • The .button element has a semi-transparent green background.
  • Links have a blue color set at an alpha value of .9.

Using the Opacity Property

CSS also provides the opacity property:

Example: Using the Opacity Property

img {
  opacity: .5; /* Makes image half opaque */
}

.container { 
  opacity: .8; /* Makes container mostly opaque */ 
}
  • An image is made half opaque by setting its opacity.
  • A container element is mostly opaque by setting its opacity.

Note that when using opacity, it affects both an element and its contents. If you want transparency only on specific parts or colors within an element without affecting child elements' opacities, use either RGBA or HSLA instead.

Gradients

CSS gradients let you create transitions between two or more colors. Gradients add visual interest to your web designs without using images. CSS supports two types of gradients: linear gradients and radial gradients.

Linear Gradients

Linear gradients create a straight line transition from one color to another. To create a linear gradient, use the linear-gradient() function as the value for the background-image property.

Example: Creating a linear gradient

.linear-gradient {
  background-image: linear-gradient(to right, red, blue);
}

In this example, the gradient starts with red on the left and changes to blue on the right. You can specify the direction of the gradient using keywords like to right, to bottom, to top, or to left, or by using angles like 45deg.

Example: Linear gradient with multiple colors

.linear-gradient-multiple {
  background-image: linear-gradient(to bottom right, red, yellow, green);
}

You can also create linear gradients with multiple color stops. In this example, it starts with red at the top left, changes to yellow in the middle, and ends with green at the bottom right.

Radial Gradients

Radial gradients create a circular or elliptical transition between colors. To create a radial gradient, use the radial-gradient() function as the value for the background-image property.

Example: Creating a radial gradient

.radial-gradient {
  background-image: radial-gradient(circle, blue, green);
}

In this example, it starts with blue at the center and changes to green towards the edges. You can specify its shape using keywords like circle or ellipse. You can also define its size and position.

Example: Radial gradient with multiple colors and position

.radial-gradient-multiple {
  background-image: radial-gradient(circle at top left, red, yellow, green);
}

This example creates one starting with red at top left that changes to yellow then ends in green towards edges.

Examples of Creating Gradients in CSS

Here are some more examples:

Example: Linear gradient with transparency

.linear-gradient-transparent { 
  background-image: linear-gradient(to right, rgba(255, 0, 0, 0.5), rgba(0, 0, 255, 0.5)); 
} 

Example: Repeating linear gradient

.repeating-linear-gradient {   
  background-image: repeating-linear-gradient(45deg, red 10px, blue 20px);    
}  

Example: Radial gradient custom shape position

.radial-gradient-custom {     
  background-image: radial-gradient(ellipse, yellow, green, blue);      
}    

Gradients are useful features in CSS that help you make appealing backgrounds for buttons, sections, full-page backgrounds, and more. With the flexibility offered by both types along with the ability to control properties such as color positions and transparency, many creative effects are possible within your web pages.

Color Palette and Schemes

Color palettes and color schemes play a key role in web design. A well-chosen color palette can make your website look professional, evoke the right emotions, and create a cohesive visual experience for your users. When selecting colors for your website, consider the purpose, branding, and target audience of your site.

There are several types of color schemes that you can use to create a harmonious design:

Monochromatic

A monochromatic color scheme uses different shades, tints, and tones of a single color. It creates a unified look and is easy on the eyes. To create this scheme, choose a base color and then use its lighter and darker variations.

Complementary

A complementary color scheme uses colors that are opposite each other on the color wheel. These colors create high contrast but should be used carefully to avoid visual tension. Examples include blue and orange, red and green, or purple and yellow.

Analogous

An analogous color scheme uses colors that are next to each other on the color wheel. These colors create a harmonious combination. To create this scheme, choose a primary color then use the colors directly to its left and right.

Triadic

A triadic color scheme uses three colors that are evenly spaced on the color wheel. This scheme creates a balanced look. To create this scheme, choose a primary color then draw an equilateral triangle on the wheel to find the other two colors.

When creating a palette for your website, tools can help generate schemes based on your preferences:

  • Adobe Color: This tool allows you to make schemes based on different rules like monochromatic or complementary.
  • Coolors: Coolors is fast and easy-to-use for generating random palettes or adjusting individual colors.
  • Paletton: Paletton lets you make schemes based on models like monochromatic or triadic with fine-tuning options.

By using these tools and understanding different types of schemes, you can make appealing palettes for your web designs while considering branding message user experience when choosing your colors experimenting until finding perfect palette for site