In HTML, color is defined using CSS properties. The property you use for a particular situation will depend on what it is you're applying color to. For example, two popular CSS properties for defining color are the
When it comes to specifying an actual color, that's where things get a little interesting. There are many ways of specifying color in HTML. Most web developers choose their preferred method and stick with that. Having said that, you should be aware of the various methods for specifying a color value, because, you may find that you prefer to use a combination of methods.
But let's not get ahead of ourselves. Let's look at a couple of simple examples first.
Click anywhere in the color picker to the right. This will select a color. After you select a color, you'll notice the RGB, HSL, and hexadecimal values are shown for your selected color.
Here's an explanation of hexadecimal and RGB.
RGB stands for Red Green Blue. Therefore, when using RGB notation, you are specifying how much red, how much green, and how much blue should be included in the final color. This is because all colors are a mix of these three colors.
It's the same with hexadecimal notation, except, the notation uses base 16 to specify the red, green, and blue values.
For example, if you use RGB notation,
You could of course, use less red and more of the other colors, for example
In hexadecimal,
color
property (for applying foreground color to the text) and background-color
property (for applying color to an element's background).When it comes to specifying an actual color, that's where things get a little interesting. There are many ways of specifying color in HTML. Most web developers choose their preferred method and stick with that. Having said that, you should be aware of the various methods for specifying a color value, because, you may find that you prefer to use a combination of methods.
But let's not get ahead of ourselves. Let's look at a couple of simple examples first.
HTML Color Examples
Here are some examples of applying color to HTML elements.Text Color
To specify text color, you need to usecolor:{color code}
, where {color code} is a color name, hex code, or decimal RGB value.Source Code | Result |
---|---|
HTML color example
|
Text Background Color
To specify background color, use thebackground-color
property:Source Code | Result |
---|---|
HTML color example
|
Text Border Color
To specify border color, use theborder
property, along with the border width and style:Source Code | Result |
---|---|
HTML color example
|
Div Color
You can apply color to any element and it will cascade down to all elements within that element. For example, here we apply colors to the
element. The text within the
element automatically inherits the colors that we specify at the
level.Source Code | Result |
---|---|
The surrounding 'div' element has got the following properties applied against it: background-color:yellow; color:blue; border:1px solid black;
|
Div and Text
Here's another example. This time, I've added some more text, with specific styles that over-ride those defined at the
level.Source Code | Result |
---|---|
The surrounding 'div' element has got the following properties applied against it: background-color:yellow; color:blue; border:1px solid black;
This text has got different styles applied. But you can also over-ride these styles by using a 'span' element, like this and this.
|
Specifying a Color Value
As you can see by the above HTML & CSS code, you specify a color by using the relevant CSS color property (eg,background-color
), followed by a colon (:
), followed by the color value (eg, green
). For example, to make something yellow, you type background-color:green;
. Color values can be one of the following:- Color name. For example,
color:blue
- RGB value determined by hexadecimal notation. Example,
color:#0000ff
results in a color of red. - Hexadecimal shorthand. For example,
color:#00f
is the same ascolor:#0000ff
- RGB functional notation. For example,
color:rgb(0,0,255)
results in red. - RGB percentage value. For example,
color:rgb(0,0,100%)
results in red.
About Hexadecimal & RGB Colors
Here's an explanation of hexadecimal and RGB.
RGB stands for Red Green Blue. Therefore, when using RGB notation, you are specifying how much red, how much green, and how much blue should be included in the final color. This is because all colors are a mix of these three colors.
It's the same with hexadecimal notation, except, the notation uses base 16 to specify the red, green, and blue values.
For example, if you use RGB notation,
rgb(255,0,0)
specifies "all red and none of the other colors". This is because 255
is as high as you can go in RGB notation.You could of course, use less red and more of the other colors, for example
rgb(72,61,139)
results in DarkSlateBlue.In hexadecimal,
ff
is as high as you can go and 00
is as low (i.e. none). Therefore, the color "DarkSlateBlue" would be represented as color:#483D8B
. You could have, of course, represented this color by its color name (i.e. color:DarkSlateBlue
).
0 comments:
Post a Comment