In CSS, colors are specified by using a predefined color name, or with a RGB, HEX, HSL, RGBA, HSLA value.
CSS Background Color
You can set the background color for HTML elements:
Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
In CSS, a color can be specified using a hexadecimal value in the form:
#rrggbb
Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00 and ff (same as decimal 0-255).
For example, #ff0000 is displayed as red, because red is set to its highest value (ff) and the others are set to the lowest value (00).
To display black, set all values to 00, like this: #000000.
To display white, set all values to ff, like this: #ffffff.
Experiment by mixing the HEX values below:
#ff6347
3 Digit HEX Value
Sometimes you will see a 3-digit hex code in the CSS source.
The 3-digit hex code is a shorthand for some 6-digit hex codes.
The 3-digit hex code has the following form:
#rgb
Where r, g, and b represent the red, green, and blue components with values between 0 and f.
The 3-digit hex code can only be used when both the values (RR, GG, and BB) are the same for each component. So, if we have #ff00cc, it can be written like this: #f0c.
In CSS, a color can be specified using hue, saturation, and lightness (HSL) in the form:
hsl(hue, saturation, lightness)
Hue is a degree on the color wheel (from 0 to 360):
0 (or 360) is red
120 is green
240 is blue
Saturation is a percentage value: 0% means a shade of gray, and 100% is the full color.
Lightness is also a percentage; 0% is black, 50% is neither light or dark, 100% is white.
Experiment by mixing the HSL values below:
hsl(0, 100%, 50%)
Saturation
Saturation can be described as the intensity of a color.
100% is pure color, no shades of gray.
50% is 50% gray, but you can still see the color.
0% is completely gray; you can no longer see the color.
Lightness
The lightness of a color can be described as how much light you want to give the color, where 0% means no light (black), 50% means 50% light (neither dark nor light) and 100% means full lightness (white).
HSLA Value
HSLA color values are an extension of HSL color values with an alpha channel - which specifies the opacity for a color.
An HSLA color value is specified with:
hsla(hue,saturation, lightness, alpha)
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at all):
The text-align property is used to set the horizontal alignment of a text.
This property can have one of the following values:
left - Aligns the text to the left
right - Aligns the text to the right
center - Centers the text
justify - Stretches the lines so that each line has equal width
When the text-align property is set to "justify", each line is stretched so that every line has equal width, and the left and right margins are straight (like in magazines and newspapers):
Example
div {
text-align: justify;
}
Text Align Last
The text-align-last property specifies how to align the last line of a text.
This property can have one of the following values:
auto - Default value. The last line is justified and aligned left
left - The last line is aligned to the left
right - The last line is aligned to the right
center - The last line is center-aligned
justify - The last line is justified as the rest of the lines
start - The last line is aligned at the start of the line
end - The last line is aligned at the end of the line
Example
Different alignment of the last line in three <p> elements:
p.a {
text-align-last: right;
}
p.b {
text-align-last: center;
}
p.c {
text-align-last: justify;
}
Vertical Alignment
The vertical-align property sets the vertical alignment of an element.
This property can have one of the following values:
baseline - Default value. The element is aligned with the baseline of the parent
length/% - Raises or lower an element by the specified length or percent
sub - The element is aligned with the subscript baseline of the parent
super - The element is aligned with the superscript baseline of the parent
top - The element is aligned with the top of the tallest element on the line
text-top - The element is aligned with the top of the parent element's font
middle - The element is placed in the middle of the parent element
bottom - The element is aligned with the lowest element on the line
text-bottom - The element is aligned with the bottom of the parent element's font
The CSS text-decoration-line property sets the type of decoration line added to the text.
This property can have one or more of the following values:
none - Default value. Displays no decoration line
underline - The decoration line is displayed under the text
overline - The decoration line is displayed over the text
line-through - The decoration line is displayed through the text
Tip: You can combine multiple values, like overline and underline to display lines both over and under a text.
Example
h1 {
text-decoration-line: overline;
}
h2 {
text-decoration-line: line-through;
}
h3 {
text-decoration-line: underline;
}
p {
text-decoration-line: overline underline;
}
Specify a Color for the Decoration Line
The CSS text-decoration-color property is used to set the color of the decoration line.
Example
Add different colors to the decoration line:
h1 {
text-decoration-line: overline;
text-decoration-color: red;
}
The text-decoration-style property sets the style of the text decoration (like solid, wavy, dotted, dashed, double).
Tip: Also look at the text-decoration property, which is a short-hand property for text-decoration-line, text-decoration-style, text-decoration-color, and text-decoration-thickness.
The text-decoration-thickness property specifies the thickness of the decoration line.
The CSS text-transform property is used to control the capitalization of text in an element.
It can be used to transform a text into uppercase or lowercase letters, or capitalize the first letter of each word, without changing the original content in HTML.
This property can have one of the following values:
none - No transformation. Text renders as it is
capitalize - Transforms the first character of each word to uppercase
uppercase - Transforms all characters to uppercase
lowercase - Transforms all characters to lowercase
The background-color property specifies the background color of an element.
Example
The background color of a page is set like this:
body {
background-color: lightblue;
}
With CSS, a color is most often specified by:
a valid color name - like "red"
a HEX value - like "#ff0000"
an RGB value - like "rgb(255,0,0)"
Look at CSS Color Values for a complete list of possible color values.
Other Elements
You can set the background color for any HTML elements:
Example
Here, the <h1>, <p>, and <div> elements will have different background colors:
h1 {
background-color: green;
}
div {
background-color: lightblue;
}
p {
background-color: yellow;
}
Opacity / Transparency
The opacity property specifies the opacity/transparency of an element. It can take a value from 0.0 - 1.0. The lower value, the more transparent:
Example
div {
background-color: green;
opacity: 0.3;
}
Note: When using the opacity property to add transparency to the background of an element, all of its child elements inherit the same transparency. This can make the text inside a fully transparent element hard to read.
Transparency using RGBA
If you do not want to apply opacity to child elements, like in our example above, use RGBA color values. The following example sets the opacity for the background color and not the text:
Example
div {
background: rgba(0, 128, 0, 0.3) /* Green background with 30% opacity */
The border-radius property is used to add rounded borders to an element:
Example
p {
border: 2px solid red;
border-radius: 5px;
}
Definition and Usage
The border-radius property defines the radius of the element's corners.
Tip: This property allows you to add rounded corners to elements!
This property can have from one to four values. Here are the rules:
Four values - border-radius: 15px 50px 30px 5px; (first value applies to top-left corner, second value applies to top-right corner, third
value applies to bottom-right corner, and fourth value applies to bottom-left corner)
Three values - border-radius: 15px 50px 30px; (first value applies to top-left corner, second value applies to top-right and bottom-left corners, and third value applies to bottom-right corner)
Two values - border-radius: 15px 50px; (first value applies to top-left and bottom-right corners, and the second value applies to top-right and bottom-left corners)
One value - border-radius: 15px; (the value applies to all four corners, which are rounded equally
CSS Margins
CSS Margins
The CSS margin properties are used to create space around elements, outside of any defined borders.
Margins define the distance between an element's border and the surrounding elements.
With CSS, you have full control over the margins. CSS has properties for setting the margin for each individual side of an element (top, right, bottom, and left), and a shorthand property for setting all the margin properties in one declaration.
Margin - Individual Sides
CSS has properties for specifying the margin for each side of an element:
Margin collapse is when two margins collapse into a single margin.
Top and bottom margins of elements are sometimes collapsed into a single margin that is equal to the largest of the two margins.
Note: Margin collapse only happens with top and bottom margins! Not left and right margins!
CSS Padding
CSS Padding
The CSS padding properties are used to generate space around an element's content, inside of any defined borders.
With CSS, you have full control over the padding. There are properties for setting the padding for each side of an element (top, right, bottom, and left), and a shorthand property for setting all the padding properties in one declaration.
Padding - Individual Sides
CSS has properties for specifying the padding for each side of an element:
The CSS width property specifies the width of the element's content area. The content area is the portion inside the padding, border, and margin of an element (the box model).
So, if an element has a specified width, the padding added to that element will be added to the total width of the element. This is often an undesirable result.
Example
Here, the <div> element is given a width of 300px. However, the actual width of the <div> element will be 350px (300px + 25px of left padding + 25px of right padding):
div {
width: 300px;
padding: 25px;
}
Padding and box-sizing
The box-sizing property defines how the width and height of an element are calculated: should they include padding and borders, or not.
The box-sizing property can have the following values:
content-box - This is default. The width and height properties includes only the content (border and padding are not included)
border-box - The width and height properties includes content, padding and border
So, to keep the width at 300px, no matter the amount of padding, you can use the box-sizing: border-box;. This causes the element to maintain its actual width; if you increase the padding, the available content space will decrease.
Example
Use the box-sizing property to keep the width at 300px, no matter the amount of padding:
div {
width: 300px;
padding: 25px;
box-sizing: border-box;
}
CSS Height and Width
height and width
The height and width properties are used to set the height and width of an element.
The height and width do not include padding, borders, or margins. It sets the height and width of the area inside the padding, border, and margin of the element.
The height property sets the height of an element.
The height of an element does not include padding, borders, or margins!
If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly.
If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow. How the container will handle the overflowing content is defined by the overflow property.
The max-width property sets the maximum allowed width of an element. This prevents the width of an element to be larger than the max-width property value.
The max-width property can have the following values:
length - Defines the maximum width in px, cm, etc.
% - Defines the maximum width in percent of the containing block
none - This is default. Means that there is no maximum width
One problem with the width property can occur when the browser window is smaller than the width of the element. The browser then adds a horizontal scrollbar to the page. So, using max-width will improve the browser's handling on small windows.
One <div> element with a max-width of 500 pixels, and one <div> element with a width of 500 pixels:
.div1 {
max-width: 500px;
background-color: powderblue;
}
.div2 {
width: 500px;
background-color: powderblue;
}
Note: If you use both the width property and the max-width property on the same element, and the value of the width property is larger than the max-width property; the max-width property value will be used!
The min-width property defines the minimum width of an element.
If the content is smaller than the minimum width, the minimum width will be applied.
If the content is larger than the minimum width, the min-width property has no effect.
Note: This prevents the value of the width property from becoming smaller than min-width.
Definition and Usage
The max-height property defines the maximum height of an element.
If the content is larger than the maximum height, it will overflow. How the container will handle the overflowing content is defined by the overflow property.
If the content is smaller than the maximum height, the max-height property has no effect.
Note: This prevents the value of the height property from becoming larger than max-height. The value of the max-height property overrides the height property.
The min-height property defines the minimum height of an element.
If the content is smaller than the minimum height, the minimum height will be applied.
If the content is larger than the minimum height, the min-height property has no effect.
Note: This prevents the value of the height property from becoming smaller than min-height.