Home >Web Front-end >CSS Tutorial >Exploring Colors in CSS
<p>
paragraph), you can modify its text color using the color
property:
<code class="language-css">p { color: red; }</code><p>Similarly, the
background-color
property changes the element's background:
<code class="language-css">p { background-color: darkorange; }</code><p>While CSS supports common color names like "red" and "darkorange," it doesn't encompass every possible hue. For precise color control, RGB, HEX, and HSL color models offer greater flexibility.
rgb()
function defines RGB colors:
<code class="language-css">rgb(<red>, <green>, <blue>)</code><p>Each parameter (
red
, green
, blue
) accepts an integer value between 0 and 255, representing the intensity of each color component. 0 indicates the weakest intensity, and 255 the strongest. For instance:
<code class="language-css">p { color: rgb(255, 0, 0); /* Equivalent to color: red; */ }</code><p>Combining different intensities creates diverse colors:
<code class="language-css">p { color: rgb(168, 189, 45); }</code><p>A color picker tool is highly recommended for selecting RGB values visually, as it's challenging to predict the resulting color from numerical values alone. <p>
rgba()
function extends rgb()
by adding an alpha channel:
<code class="language-css">rgba(<red>, <green>, <blue>, <alpha>)</code><p>The
alpha
parameter (0.0 to 1.0) controls transparency; 0.0 is fully transparent, and 1.0 is fully opaque:
<code class="language-css">p { color: rgba(167, 189, 45, 0.408); }</code><p>
<code class="language-css">#rrggbb</code><p>The
#
symbol precedes the six-digit hexadecimal code. Each pair (rr
, gg
, bb
) represents the red, green, and blue components, respectively, ranging from 00 (0 decimal) to ff (255 decimal). For example:
<code class="language-css">p { color: #ff0000; /* Equivalent to color: rgb(255, 0, 0); and color: red; */ }</code><p>Transparency can be added using eight-digit HEX codes:
<code class="language-css">#rrggbbaa</code><p>
aa
represents the alpha channel (00 to ff, mapping to 0.0 to 1.0).
<code class="language-css">p { color: #a7bd2d68; }</code><p>This is equivalent to
rgba(167, 189, 45, 0.408)
.
<p>hsl()
function uses:
<code class="language-css">p { color: red; }</code><p>
hue
represents the color's position on the color wheel (0 to 360 degrees).
<p>saturation
(0% to 100%) indicates the color's intensity (0% is gray, 100% is full color).
<p>lightness
(0% to 100%) determines the amount of black or white added (0% is black, 100% is white).
<p>hsla()
adds an alpha channel for transparency:
<code class="language-css">p { background-color: darkorange; }</code>
<code class="language-css">rgb(<red>, <green>, <blue>)</code><p>This is equivalent to
#a7bd2d68
and rgba(167, 189, 45, 0.408)
.
The above is the detailed content of Exploring Colors in CSS. For more information, please follow other related articles on the PHP Chinese website!