Encyclopedia CSS: color
CSS: color | |
Thematic Portals CSS Portal • Web Developers Portal
|
The "color" property in CSS defines the text color of an element. It is used to style text and supports both simple color names and more complex formats such as RGB, HEX, HSL, and others. This is one of the most fundamental and frequently used properties in web development.
Color Formats
The color
property supports various ways of defining colors:
- Color names: Simple names like
red
,blue
,green
. - HEX color code: Hexadecimal notation, e.g.,
#ff0000
(red). - RGB: Defined as
rgb(255, 0, 0)
. - RGBA: Similar to RGB but with transparency, e.g.,
rgba(255, 0, 0, 0.5)
. - HSL: Defines colors using hue, saturation, and lightness, e.g.,
hsl(0, 100%, 50%)
. - HSLA: HSL with transparency, e.g.,
hsla(0, 100%, 50%, 0.5)
. - CSS variables: Colors can be defined using variables, e.g.,
var(--main-color)
.
Usage Example
HTML
<body>
<p class="example1">This text is red.</p>
<p class="example2">This text is blue.</p>
<p class="example3">This text is green.</p>
<p class="example4">This text is orange with transparency.</p>
</body>
CSS
p {
font-size: 16px;
font-family: Arial, sans-serif;
line-height: 1.5;
}
.example1 {
color: red; /* Color name */
}
.example2 {
color: #0000ff; /* HEX color code */
}
.example3 {
color: rgb(0, 255, 0); /* RGB */
}
.example4 {
color: rgba(255, 165, 0, 0.8); /* RGBA */
}
Browser Support
The color
property is supported by all modern browsers, including mobile versions. It is one of the basic CSS properties available even in the earliest browser versions.
Notes
- The text color of an element can be inherited from its parent element if not explicitly defined.
- For more complex color effects (e.g., gradients), use the
background
orbackground-image
property.
Useful Tips
- Use semantic color variables like
--main-color
to easily manage your website's color scheme. - Check text and background contrast to ensure good readability (e.g., using the WebAIM Contrast Checker).
Example Using Variables
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
}
h1 {
color: var(--primary-color);
}
p {
color: var(--secondary-color);
}
In this example, CSS variables are used to define primary colors:
:root
is a pseudo-class representing the root element of the document (typically<html>
). Variables defined in:root
are available throughout the website.--primary-color
and--secondary-color
are custom variables holding color values. They can be overridden for specific parts of the site if needed.- Within the rules for
h1
andp
, thevar()
function is used to apply the variable values to thecolor
property.
This is a convenient way to organize the color palette of your website. For instance, if you need to change the primary color of your design, you can edit the variable value in one place, and the changes will automatically reflect on all elements using that variable.
Variables are especially useful when creating themes (e.g., light/dark mode) as they make it easy to switch color values based on user preferences.
Conclusion
The color
property is an essential part of CSS, enabling efficient control of text colors on web pages. It supports various formats, making it versatile for any project.