What Is CSS?: Body (Background-Color: Lightblue ) h1 (
What Is CSS?: Body (Background-Color: Lightblue ) h1 (
CSS Introduction
CSS is the language we use to style a Web page.
What is CSS?
CSS stands for Cascading Style Sheets
CSS describes how HTML elements are to be displayed on screen, paper, or in other media
CSS saves a lot of work. It can control the layout of multiple web pages all at once
External stylesheets are stored in CSS files
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}
h1 {
CSS-2
color: white;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
}
</style>
</head>
<body>
<h1>My First CSS Example</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS Syntax
A CSS rule consists of a selector and a declaration block.
CSS Syntax
Example
In this example all <p> elements will be center-aligned, with a red text color:
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
text-align: center;
}
</style>
</head>
<body>
<p>Hello World!</p>
<p>These paragraphs are styled with CSS.</p>
</body>
</html>
CSS-4
CSS Selectors
A CSS selector selects the HTML element(s) you want to style.
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
Simple selectors (select elements based on name, id, class)
Combinator selectors (select elements based on a specific relationship between them)
Pseudo-class selectors (select elements based on a certain state)
Pseudo-elements selectors (select and style a part of an element)
Attribute selectors (select elements based on an attribute or attribute value)
This page will explain the most basic CSS selectors.
Example
Here, all <p> elements on the page will be center-aligned, with a red text color:
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: red;
}
</style>
</head>
<body>
<p>Every paragraph will be affected by the style.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
CSS-5
Example
The CSS rule below will be applied to the HTML element with id="para1":
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>
Example
In this example all HTML elements with class="center" will be red and center-aligned:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">Red and center-aligned heading</h1>
<p class="center">Red and center-aligned paragraph.</p>
</body>
</html>
You can also specify that only specific HTML elements should be affected by a class.
Example
In this example only <p> elements with class="center" will be red and center-aligned:
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
text-align: center;
color: red;
}
</style>
</head>
CSS-7
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be red and center-aligned.</p>
</body>
</html>
Example
In this example the <p> element will be styled according to class="center" and to class="large":
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
text-align: center;
color: red;
}
[Link] {
font-size: 300%;
}
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be red and center-aligned.</p>
<p class="center large">This paragraph will be red, center-aligned, and in a large
font-size.</p>
</body>
</html>
CSS-8
Example
The CSS rule below will affect every HTML element on the page:
<!DOCTYPE html>
<html>
<head>
<style>
*{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1>Hello world!</h1>
<p>Every element on the page will be affected by the style.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
CSS-9
Example
In this example we have grouped the selectors from the code above:
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body>
</html>
CSS-10
External CSS
With an external style sheet, you can change the look of an entire website by changing just one
file!
Each HTML page must include a reference to the external style sheet file inside the <link>
element, inside the head section.
Example
External styles are defined within the <link> element, inside the <head> section of an HTML page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="[Link]">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS-11
An external style sheet can be written in any text editor, and must be saved with a .css extension.
The external .css file should not contain any HTML tags.
Here is how the "[Link]" file looks:
"[Link]"
Internal CSS
An internal style sheet may be used if one single HTML page has a unique style.
The internal style is defined inside the <style> element, inside the head section.
Example
Internal styles are defined within the <style> element, inside the <head> section of an HTML page:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS-12
Inline CSS
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style attribute can contain
any CSS property.
Example
Inline styles are defined within the "style" attribute of the relevant element:
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;text-align:center;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
</html>
CSS-13
Tip: An inline style loses many of the advantages of a style sheet (by mixing content with presentation).
Use this method sparingly.
Example
If the internal style is defined after the link to the external style sheet, the <h1> elements will be
"orange":
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="[Link]">
<style>
h1 {
color: orange;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>The style of this document is a combination of an external stylesheet, and internal style</p>
</body>
</html>
CSS-14
Example
However, if the internal style is defined before the link to the external style sheet, the <h1> elements will
be "navy":
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: orange;
}
</style>
<link rel="stylesheet" type="text/css" href="[Link]">
</head>
<body>
<h1>This is a heading</h1>
<p>The style of this document is a combination of an external stylesheet, and internal style</p>
</body>
</html>
CSS-15
Cascading Order
What style will be used when there is more than one style specified for an HTML element?
All the styles in a page will "cascade" into a new "virtual" style sheet by the following rules, where
number one has the highest priority:
1. Inline style (inside an HTML element)
2. External and internal style sheets (in the head section)
3. Browser default
So, an inline style has the highest priority, and will override external and internal styles and browser
defaults.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="[Link]">
<style>
body {background-color: linen;}
</style>
</head>
<body style="background-color: lavender">
<h1>Multiple Styles Will Cascade into One</h1>
<p>Here, the background color of the page is set with inline CSS, and also with an internal CSS,
and also with an external CSS.</p>
<p>Try experimenting by removing styles to see how the cascading stylesheets work (try
removing the inline CSS first, then the internal, then the external).</p>
</body>
</html>
CSS-16
CSS Comments
CSS comments are not displayed in the browser, but they can help document your source code.
CSS Comments
Comments are used to explain the code, and may help when you edit the source code at a later
date.
Comments are ignored by browsers.
A CSS comment is placed inside the <style> element, and starts with /* and ends with */:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
CSS-17
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
CSS-18
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: /*red*/blue;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
CSS-19
Example
<!DOCTYPE html>
<html>
<head>
<style>
/* This is
a multi-line
comment */
p{
color: red;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
</style>
</head>
<body>
<h2>My Heading</h2>
CSS-21
<p>Hello World!</p>
</body>
</html>
CSS-22
CSS Colors
Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values.
<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:Tomato;">Tomato</h1>
<h1 style="background-color:Orange;">Orange</h1>
<h1 style="background-color:DodgerBlue;">DodgerBlue</h1>
<h1 style="background-color:MediumSeaGreen;">MediumSeaGreen</h1>
<h1 style="background-color:Gray;">Gray</h1>
<h1 style="background-color:SlateBlue;">SlateBlue</h1>
<h1 style="background-color:Violet;">Violet</h1>
<h1 style="background-color:LightGray;">LightGray</h1>
</body>
</html>
CSS-23
<!DOCTYPE html>
<html>
<body>
<p style="background-color:Tomato;">
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.
</p>
</body>
</html>
<!DOCTYPE html>
<html>
CSS-24
<body>
<p style="color:DodgerBlue;">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed
diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p style="color:MediumSeaGreen;">Ut wisi enim ad minim veniam, quis nostrud exerci tation
ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
</body>
CSS-25
</html>
<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:#ff6347;">#ff6347</h1>
<p>In addition to the predefined color names, colors can be specified using RGB, HEX, HSL, or
even transparent colors using RGBA or HSLA color values.</p>
</body>
</html>
CSS-26
CSS-27
CSS Backgrounds
The CSS background properties are used to add background effects for elements.
CSS background-color
The background-color property specifies the background color of an element.
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
CSS-28
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:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
background-color: green;
div {
background-color: lightblue;
p{
background-color: yellow;
</style>
</head>
<body>
<div>
CSS-29
</div>
</body>
</html>
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:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: green;
[Link] {
opacity: 0.1;
CSS-30
[Link] {
opacity: 0.3;
[Link] {
opacity: 0.6;
</style>
</head>
<body>
<h1>Transparent Boxes</h1>
<p>When using the opacity property to add transparency to the background of an element, all of
its child elements become transparent as well. This can make the text inside a fully transparent
element hard to read:</p>
<div class="first">
<h1>opacity 0.1</h1>
</div>
<div class="second">
<h1>opacity 0.3</h1>
</div>
<div class="third">
<h1>opacity 0.6</h1>
</div>
<div>
<h1>opacity 1 (default)</h1>
</div>
</body>
</html>
CSS-31
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.
Tip: You will learn more about RGBA Colors in our CSS Colors Chapter.
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
CSS-32
[Link] {
[Link] {
[Link] {
</style>
</head>
<body>
<div style="opacity:0.1;">
<h1>10% opacity</h1>
</div>
<div style="opacity:0.3;">
<h1>30% opacity</h1>
</div>
<div style="opacity:0.6;">
<h1>60% opacity</h1>
</div>
<div>
<h1>opacity 1</h1>
</div>
CSS-33
<div class="first">
<h1>10% opacity</h1>
</div>
<div class="second">
<h1>30% opacity</h1>
</div>
<div class="third">
<h1>60% opacity</h1>
</div>
<div>
<h1>default</h1>
</div>
<p>Notice how the text gets transparent as well as the background color when using the opacity
property.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("[Link]");
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Example
CSS-35
This example shows a bad combination of text and background image. The text is hardly readable:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("[Link]");
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Note: When using a background image, use an image that does not disturb the text.
The background image can also be set for specific elements, like the <p> element:
CSS-36
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-image: url("[Link]");
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
CSS background-repeat
By default, the background-image property repeats an image both horizontally and vertically.
Some images should be repeated only horizontally or vertically, or they will look strange, like
this:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
If the image above is repeated only horizontally (background-repeat: repeat-x;), the background will look
better:
CSS-38
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>The background image only shows once, but it is disturbing the reader!</p>
</body>
</html>
In the example above, the background image is placed in the same place as the text. We want to change
the position of the image, so that it does not disturb the text too much.
CSS-40
CSS background-position
The background-position property is used to specify the position of the background image.
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
margin-right: 200px;
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Here, the background image is only shown once. In addition it is positioned away from the
text.</p>
<p>In this example we have also added a margin on the right side, so that the background image
will not disturb the text.</p>
</body>
</html>
CSS-41
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
margin-right: 200px;
background-attachment: fixed;
</style>
</head>
CSS-42
<body>
<p>The background-attachment property specifies whether the background image should scroll
or be fixed (will not scroll with the rest of the page).</p>
<p><strong>Tip:</strong> If you do not see any scrollbars, try to resize the browser
window.</p>
</body>
CSS-43
</html>
Example
Specify that the background image should scroll with the rest of the page:
<!DOCTYPE html>
<html>
<head>
<style>
body {
CSS-44
background-image: url("img_tree.png");
background-repeat: no-repeat;
margin-right: 200px;
background-attachment: scroll;
</style>
</head>
<body>
<p>The background-attachment property specifies whether the background image should scroll
or be fixed (will not scroll with the rest of the page).</p>
<p><strong>Tip:</strong> If you do not see any scrollbars, try to resize the browser
window.</p>
</body>
</html>
To shorten the code, it is also possible to specify all the background properties in one single property.
This is called a shorthand property.
Instead of writing:
Example
Use the shorthand property to set the background properties in one declaration:
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin-right: 200px;
</style>
</head>
<body>
<p>The background property is a shorthand property for specifying all the background
properties in one declaration.</p>
<p>Here, the background image is only shown once, and it is also positioned in the top-right
corner.</p>
<p>We have also added a right margin, so that the text will not write over the background
image.</p>
CSS-47
</body>
</html>
When using the shorthand property the order of the property values is:
background-color
background-image
background-repeat
background-attachment
background-position
It does not matter if one of the property values is missing, as long as the other ones are in this order. Note
that we do not use the background-attachment property in the examples above, as it does not have a value.
CSS Borders
The CSS border properties allow you to specify the style, width, and color of an element's border.
CSS-48
groove - Defines a 3D grooved border. The effect depends on the border-color value
ridge - Defines a 3D ridged border. The effect depends on the border-color value
inset - Defines a 3D inset border. The effect depends on the border-color value
outset - Defines a 3D outset border. The effect depends on the border-color value
The border-style property can have from one to four values (for the top border, right border, bottom
border, and the left border).
Example
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
</body>
</html>
CSS-50
The width can be set as a specific size (in px, pt, cm, em, etc) or by using one of the three pre-
defined values: thin, medium, or thick:
Example
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
border-style: solid;
border-width: 5px;
[Link] {
border-style: solid;
border-width: medium;
[Link] {
border-style: dotted;
border-width: 2px;
[Link] {
border-style: dotted;
border-width: thick;
[Link] {
border-style: double;
border-width: 15px;
}
CSS-52
[Link] {
border-style: double;
border-width: thick;
</style>
</head>
<body>
</body>
</html>
CSS-53
Example
<!DOCTYPE html>
<html>
CSS-54
<head>
<style>
[Link] {
border-style: solid;
border-width: 5px 20px; /* 5px top and bottom, 20px on the sides */
[Link] {
border-style: solid;
border-width: 20px 5px; /* 20px top and bottom, 5px on the sides */
[Link] {
border-style: solid;
border-width: 25px 10px 4px 35px; /* 25px top, 10px right, 4px bottom and 35px left */
</style>
</head>
<body>
<p>The border-width property can have from one to four values (for the top border, right
border, bottom border, and the left border):</p>
</body>
</html>
CSS-55
transparent
Example
<!DOCTYPE html>
CSS-56
<html>
<head>
<style>
[Link] {
border-style: solid;
border-color: red;
[Link] {
border-style: solid;
border-color: green;
[Link] {
border-style: dotted;
border-color: blue;
</style>
</head>
<body>
<p><b>Note:</b> The "border-color" property does not work if it is used alone. Use the
"border-style" property to set the borders first.</p>
</body>
</html>
CSS-57
Example
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
border-style: solid;
border-color: red green blue yellow; /* red top, green right, blue bottom and yellow left
*/
</style>
</head>
<body>
<p>The border-color property can have from one to four values (for the top border, right border,
bottom border, and the left border):</p>
</body>
</html>
CSS-58
HEX Values
The color of the border can also be specified using a hexadecimal value (HEX):
Example
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
border-style: solid;
[Link] {
border-style: solid;
[Link] {
border-style: solid;
</style>
CSS-59
</head>
<body>
<p>The color of the border can also be specified using a hexadecimal value (HEX):</p>
</body>
</html>
RGB Values
Or by using RGB values:
Example
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
border-style: solid;
}
CSS-60
[Link] {
border-style: solid;
[Link] {
border-style: solid;
</style>
</head>
<body>
<p>The color of the border can also be specified using RGB values:</p>
</body>
</html>
HSL Values
You can also use HSL values:
Example
CSS-61
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
border-style: solid;
[Link] {
border-style: solid;
[Link] {
border-style: solid;
</style>
</head>
<body>
<p>The color of the border can also be specified using HSL values:</p>
</body>
</html>
CSS-62
From the examples on the previous pages, you have seen that it is possible to specify a different border
for each side.
In CSS, there are also properties for specifying each of the borders (top, right, bottom, and left):
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
</style>
</head>
CSS-63
<body>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
</style>
</head>
<body>
</body>
CSS-64
</html>
border-style: dotted;
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: center;
/* Four values */
[Link] {
/* Three values */
[Link] {
/* Two values */
[Link] {
/* One value */
[Link] {
border-style: dotted;
</style>
</head>
<body>
</body>
</html>
Like you saw in the previous page, there are many properties to consider when dealing with borders.
To shorten the code, it is also possible to specify all the individual border properties in one property.
The border property is a shorthand property for the following individual border properties:
border-width
border-style (required)
border-color
Example
CSS-67
<!DOCTYPE html>
<html>
<head>
<style>
p{
</style>
</head>
<body>
</body>
</html>
You can also specify all the individual border properties for just one side:
Left Border
CSS-68
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: lightgrey;
</style>
</head>
<body>
</body>
</html>
Bottom Border
<!DOCTYPE html>
CSS-69
<html>
<head>
<style>
p{
background-color: lightgrey;
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
padding: 5px;
p.round1 {
border-radius: 5px;
padding: 5px;
p.round2 {
border-radius: 8px;
padding: 5px;
p.round3 {
border-radius: 12px;
padding: 5px;
</style>
</head>
<body>
</body>
</html>
More Examples
CSS Margins
Margins are used to create space around elements, outside of any defined borders.
CSS-72
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: 70px;
</style>
</head>
<body>
<h2>CSS Margins</h2>
</body>
</html>
CSS Margins
The CSS margin properties are used to create space around elements, outside of any defined borders.
CSS-73
With CSS, you have full control over the margins. There are properties for setting the margin for each
side of an element (top, right, bottom, and left).
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
inherit - specifies that the margin should be inherited from the parent element
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
background-color: lightblue;
CSS-74
</style>
</head>
<body>
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of
100px, and a left margin of 80px.</div>
</body>
</html>
The margin property is a shorthand property for the following individual margin properties:
margin-top
margin-right
margin-bottom
margin-left
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
</style>
</head>
<body>
<div>This div element has a top margin of 25px, a right margin of 50px, a bottom margin of
75px, and a left margin of 100px.</div>
<hr>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
</style>
</head>
<body>
<div>This div element has a top margin of 25px, a right and left margin of 50px, and a bottom
margin of 75px.</div>
<hr>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
</style>
</head>
<body>
<div>This div element has a top and bottom margin of 25px, and a right and left margin of
50px.</div>
<hr>
</body>
</html>
margin: 25px;
CSS-78
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: 25px;
background-color: lightblue;
</style>
</head>
<body>
<div>This div element has a top, bottom, left, and right margin of 25px.</div>
<hr>
</body>
</html>
The element will then take up the specified width, and the remaining space will be split equally between
the left and right margins.
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
margin: auto;
</style>
</head>
<body>
<p>You can set the margin property to auto to horizontally center the element within its container. The
element will then take up the specified width, and the remaining space will be split equally between the
left and right margins:</p>
<div>
</div>
</body>
</html>
CSS-80
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin-left: 100px;
p.ex1 {
margin-left: inherit;
</style>
</head>
<body>
<div>
CSS-81
<p class="ex1">This paragraph has an inherited left margin (from the div
element).</p>
</div>
</body>
</html>
Margin Collapse
Top and bottom margins of elements are sometimes collapsed into a single margin that is equal to the
largest of the two margins.
This does not happen on left and right margins! Only top and bottom margins!
Example
CSS-82
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
margin: 0 0 50px 0;
h2 {
margin: 20px 0 0 0;
</style>
</head>
<body>
<p>In this example the h1 element has a bottom margin of 50px and the h2 element has a top
margin of 20px. So, the vertical margin between h1 and h2 should have been 70px (50px + 20px).
However, due to margin collapse, the actual margin ends up being 50px.</p>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
</body>
</html>
CSS Padding
CSS-83
Padding is used to create space around an element's content, inside of any defined borders.
<!DOCTYPE html>
<html>
<head>
<style>
div {
padding: 70px;
</style>
</head>
<body>
<h2>CSS Padding</h2>
</body>
</html>
CSS Padding
The CSS padding properties are used to generate space around an element's content, inside of any defined
borders.
CSS-84
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).
padding-top
padding-right
padding-bottom
padding-left
inherit - specifies that the padding should be inherited from the parent element
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
CSS-85
</style>
</head>
<body>
<div>This div element has a top padding of 50px, a right padding of 30px, a bottom padding of
50px, and a left padding of 80px.</div>
</body>
</html>
The padding property is a shorthand property for the following individual padding properties:
padding-top
padding-right
padding-bottom
padding-left
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
</style>
</head>
<body>
<div>This div element has a top padding of 25px, a right padding of 50px, a bottom padding of
75px, and a left padding of 100px.</div>
</body>
</html>
CSS-87
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
</style>
</head>
<body>
<div>This div element has a top padding of 25px, a right and left padding of 50px, and a bottom
padding of 75px.</div>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
}
CSS-89
</style>
</head>
<body>
<div>This div element has a top and bottom padding of 25px, and a right and left padding of
50px.</div>
</body>
</html>
padding: 25px;
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
padding: 25px;
CSS-90
background-color: lightblue;
</style>
</head>
<body>
<div>This div element has a top, bottom, left, and right padding of 25px.</div>
</body>
</html>
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):
CSS-91
<!DOCTYPE html>
<html>
<head>
<style>
div.ex1 {
width: 300px;
background-color: yellow;
div.ex2 {
width: 300px;
padding: 25px;
background-color: lightblue;
</style>
</head>
<body>
<br>
<div class="ex2">The width of this div is 350px, even though it is defined as 300px in the
CSS.</div>
</body>
</html>
CSS-92
To keep the width at 300px, no matter the amount of padding, you can use the box-sizing property. 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:
<!DOCTYPE html>
<html>
<head>
<style>
div.ex1 {
width: 300px;
background-color: yellow;
div.ex2 {
width: 300px;
padding: 25px;
box-sizing: border-box;
background-color: lightblue;
</style>
</head>
CSS-93
<body>
<br>
<div class="ex2">The width of this div remains at 300px, in spite of the 50px of total left and
right padding, because of the box-sizing: border-box property.
</div>
</body>
</html>
The CSS height and width properties are used to set the height and width of an element.
The CSS max-width property is used to set the maximum width of an element.
CSS-94
<!DOCTYPE html>
<html>
<head>
<style>
div {
height: 50px;
width: 100%;
</style>
</head>
<body>
</body>
</html>
The height and width properties do not include padding, borders, or margins. It sets the height/width of
the area inside the padding, border, and margin of the element.
CSS-95
auto - This is default. The browser calculates the height and width
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
height: 200px;
width: 50%;
background-color: powderblue;
</style>
</head>
<body>
</body>
</html>
This element has a height of 100 pixels and a width of 500 pixels.
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
height: 100px;
width: 500px;
background-color: powderblue;
</style>
</head>
<body>
</body>
</html>
Note: Remember that the height and width properties do not include padding, borders, or margins! They
set the height/width of the area inside the padding, border, and margin of the element!
Setting max-width
The max-width property is used to set the maximum width of an element.
The max-width can be specified in length values, like px, cm, etc., or in percent (%) of the
containing block, or set to none (this is default. Means that there is no maximum width).
The problem with the <div> above occurs when the browser window is smaller than the width of
the element (500px). The browser then adds a horizontal scrollbar to the page.
In this situation, using max-width will improve the browser's handling of small windows.
Tip: Drag the browser window to smaller than 500px wide, to see the difference between the two divs!
Example
This <div> element has a height of 100 pixels and a max-width of 500 pixels:
<!DOCTYPE html>
<html>
<head>
<style>
div {
max-width: 500px;
CSS-98
height: 100px;
background-color: powderblue;
</style>
</head>
<body>
</body>
</html>
The CSS box model is essentially a box that wraps around every HTML element. It consists of: content,
padding, borders and margins. The image below illustrates the box model:
Content - The content of the box, where text and images appear
The box model allows us to add a border around elements, and to define space between elements.
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
padding: 50px;
margin: 20px;
</style>
</head>
<body>
CSS-100
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists
of: borders, padding, margins, and the actual content.</p>
<div>This text is the content of the box. We have added a 50px padding, 20px margin and a 15px
green border. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum.</div>
</body>
</html>
Important: When you set the width and height properties of an element with CSS, you just set the width
and height of the content area. To calculate the total width and height of an element, you must also
include the padding and borders.
Example
This <div> element will have a total width of 350px and a total height of 80px:
CSS-101
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 320px;
height: 50px;
padding: 10px;
margin: 0;
</style>
</head>
<body>
<div>The picture above is 350px wide. The total width of this element is also 350px. The total
height of this element is 80px.</div>
</body>
</html>
320px (width of content area) + 20px (left padding + right padding) + 10px (left border + right border)
= 350px (total width)
CSS-102
50px (height of content area) + 20px (top padding + bottom padding) + 10px (top border + bottom
border)
= 80px (total height)
Total element width = width + left padding + right padding + left border + right border
Total element height = height + top padding + bottom padding + top border + bottom border
CSS Outline
CSS-103
<!DOCTYPE html>
<html>
<head>
<style>
p{
margin: auto;
padding: 20px;
text-align: center;
</style>
</head>
<body>
<h2>CSS Outline</h2>
<p>This element has a 2px black border and a green outline with a width of 10px.</p>
</body>
</html>
CSS Outline
An outline is a line that is drawn around elements, OUTSIDE the borders, to make the element "stand
out".
CSS-104
outline-style
outline-color
outline-width
outline-offset
outline
Note: Outline differs from borders! Unlike border, the outline is drawn outside the element's border, and
may overlap other content. Also, the outline is NOT a part of the element's dimensions; the element's total
width and height is not affected by the width of the outline.
Example
<!DOCTYPE html>
<html>
<head>
<style>
p {outline-color:red;}
</style>
</head>
<body>
<p class="groove">A groove outline. The effect depends on the outline-color value.</p>
<p class="ridge">A ridge outline. The effect depends on the outline-color value.</p>
CSS-106
<p class="inset">An inset outline. The effect depends on the outline-color value.</p>
<p class="outset">An outset outline. The effect depends on the outline-color value.</p>
</body>
</html>
<!DOCTYPE html>
<html>
CSS-107
<head>
<style>
p.ex1 {
outline-style: solid;
outline-color: red;
outline-width: thin;
p.ex2 {
outline-style: solid;
outline-color: red;
outline-width: medium;
p.ex3 {
outline-style: solid;
outline-color: red;
outline-width: thick;
p.ex4 {
outline-style: solid;
outline-color: red;
outline-width: 4px;
</style>
</head>
CSS-108
<body>
</body>
</html>
invert - performs a color inversion (which ensures that the outline is visible, regardless of color
background)
The following example shows some different outlines with different colors. Also notice that these
elements also have a thin black border inside the outline:
<!DOCTYPE html>
<html>
<head>
<style>
p.ex1 {
outline-style: solid;
outline-color: red;
p.ex2 {
outline-style: dotted;
outline-color: blue;
p.ex3 {
outline-style: outset;
outline-color: grey;
</style>
CSS-110
</head>
<body>
</body>
</html>
HEX Values
The outline color can also be specified using a hexadecimal value (HEX):
<!DOCTYPE html>
CSS-111
<html>
<head>
<style>
p.ex1 {
outline-style: solid;
p.ex2 {
outline-style: dotted;
p.ex3 {
outline-style: solid;
</style>
</head>
<body>
<p>The color of the outline can also be specified using a hexadecimal value (HEX):</p>
</body>
</html>
CSS-112
RGB Values
Or by using RGB values:
<!DOCTYPE html>
<html>
<head>
<style>
p.ex1 {
outline-style: solid;
p.ex2 {
outline-style: dotted;
p.ex3 {
outline-style: solid;
</style>
</head>
<body>
<p>The color of the outline can also be specified using RGB values:</p>
</body>
</html>
HSL Values
You can also use HSL values:
<!DOCTYPE html>
CSS-114
<html>
<head>
<style>
p.ex1 {
outline-style: solid;
p.ex2 {
outline-style: dotted;
p.ex3 {
outline-style: solid;
</style>
</head>
<body>
<p>The color of the outline can also be specified using HSL values:</p>
</body>
</html>
CSS-115
outline-width
outline-style (required)
outline-color
The outline property is specified as one, two, or three values from the list above. The order of the values
does not matter.
The following example shows some outlines specified with the shorthand outline property:
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
</body>
</html>
The following example specifies an outline 15px outside the border edge:
<!DOCTYPE html>
<html>
<head>
<style>
p{
margin: 30px;
outline-offset: 15px;
</style>
</head>
<body>
</body>
</html>
The following example shows that the space between an element and its outline is transparent:
<!DOCTYPE html>
CSS-118
<html>
<head>
<style>
p{
margin: 30px;
background:yellow;
outline-offset: 15px;
</style>
</head>
<body>
</body>
</html>
CSS Text
CSS-119
<!DOCTYPE html>
<html>
<head>
<style>
div {
padding: 8px;
h1 {
text-align: center;
text-transform: uppercase;
color: #4CAF50;
p{
text-indent: 50px;
text-align: justify;
letter-spacing: 3px;
a{
text-decoration: none;
color: #008CBA;
</style>
</head>
<body>
<div>
<h1>text formatting</h1>
CSS-120
<p>This text is styled with some of the text formatting properties. The heading uses the
text-align, text-transform, and color properties.
The paragraph is indented, aligned, and the space between characters is specified. The
underline is removed from this colored
</div>
</body>
</html>
Text Color
The color property is used to set the color of the text. The color is specified by:
Look at CSS Color Values for a complete list of possible color values.
The default text color for a page is defined in the body selector.
<!DOCTYPE html>
<html>
<head>
CSS-121
<style>
body {
color: blue;
h1 {
color: green;
</style>
</head>
<body>
<p>This is an ordinary paragraph. Notice that this text is blue. The default text color for a page
is defined in the body selector.</p>
<p>Another paragraph.</p>
</body>
</html>
<!DOCTYPE html>
CSS-122
<html>
<head>
<style>
body {
background-color: lightgrey;
color: blue;
h1 {
background-color: black;
color: white;
div {
background-color: blue;
color: white;
</style>
</head>
<body>
<h1>This is a Heading</h1>
<div>This is a div.</div>
</body>
</html>
CSS-123
text-align
text-align-last
direction
unicode-bidi
vertical-align
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
The following example shows center aligned, and left and right aligned text (left alignment is default if
text direction is left-to-right, and right alignment is default if text direction is right-to-left):
Example
<!DOCTYPE html>
<html>
CSS-124
<head>
<style>
h1 {
text-align: center;
h2 {
text-align: left;
h3 {
text-align: right;
</style>
</head>
<body>
<h1>Heading 1 (center)</h1>
<h2>Heading 2 (left)</h2>
<h3>Heading 3 (right)</h3>
<p>The three headings above are aligned center, left and right.</p>
</body>
</html>
CSS-125
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
<!DOCTYPE html>
<html>
<head>
<style>
div {
padding: 10px;
width: 200px;
height: 200px;
text-align: justify;
</style>
</head>
<body>
<p>The text-align: justify; value stretches the lines so that each line has equal width (like in
newspapers and magazines).</p>
<div>
CSS-126
In my younger and more vulnerable years my father gave me some advice that I've been
turning over in my mind ever since. 'Whenever you feel like criticizing anyone,' he told
me, 'just remember that all the people in this world haven't had the advantages that
you've had.'
</div>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
p.a {
text-align-last: right;
p.b {
text-align-last: center;
CSS-127
p.c {
text-align-last: justify;
</style>
</head>
<body>
<h2>text-align-last: right:</h2>
<p class="a">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at
erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat tellus diam, consequat gravida libero
rhoncus ut.</p>
<h2>text-align-last: center:</h2>
<p class="b">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at
erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat tellus diam, consequat gravida libero
rhoncus ut.</p>
<h2>text-align-last: justify:</h2>
<p class="c">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at
erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat tellus diam, consequat gravida libero
rhoncus ut.</p>
</body>
</html>
Text Direction
The direction and unicode-bidi properties can be used to change the text direction of an element:
<!DOCTYPE html>
<html>
CSS-128
<head>
<style>
p.ex1 {
direction: rtl;
unicode-bidi: bidi-override;
</style>
</head>
<body>
</body>
</html>
Vertical Alignment
The vertical-align property sets the vertical alignment of an element.
<!DOCTYPE html>
<html>
CSS-129
<head>
<style>
img.a {
vertical-align: baseline;
img.b {
vertical-align: text-top;
img.c {
vertical-align: text-bottom;
img.d {
vertical-align: sub;
img.e {
vertical-align: super;
</style>
</head>
<body>
<h2>vertical-align: text-top:</h2>
<h2>vertical-align: text-bottom:</h2>
CSS-130
<h2>vertical-align: sub:</h2>
<h2>vertical-align: sup:</h2>
</body>
</html>
Text Decoration
In this chapter you will learn about the following properties:
text-decoration-line
CSS-131
text-decoration-color
text-decoration-style
text-decoration-thickness
text-decoration
Tip: You can combine more than one value, like overline and underline to display lines both over and
under a text.
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration: overline;
h2 {
text-decoration: line-through;
h3 {
text-decoration: underline;
[Link] {
</style>
CSS-132
</head>
<body>
the reader.</p>
</body>
</html>
Note: It is not recommended to underline text that is not a link, as this often confuses the reader.
Example
<!DOCTYPE html>
CSS-133
<html>
<head>
<style>
h1 {
text-decoration-line: overline;
text-decoration-color: red;
h2 {
text-decoration-line: line-through;
text-decoration-color: blue;
h3 {
text-decoration-line: underline;
text-decoration-color: green;
p{
text-decoration-color: purple;
</style>
</head>
<body>
</body>
</html>
CSS-134
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration-line: underline;
h2 {
text-decoration-line: underline;
text-decoration-style: double;
}
CSS-135
h3 {
text-decoration-line: underline;
text-decoration-style: dotted;
p.ex1 {
text-decoration-line: underline;
text-decoration-style: dashed;
p.ex2 {
text-decoration-line: underline;
text-decoration-style: wavy;
p.ex3 {
text-decoration-line: underline;
text-decoration-color: red;
text-decoration-style: wavy;
</style>
</head>
<body>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
</body>
</html>
CSS-136
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration-line: underline;
h2 {
text-decoration-line: underline;
text-decoration-thickness: 5px;
h3 {
text-decoration-line: underline;
text-decoration-thickness: 25%;
CSS-137
p{
text-decoration-line: underline;
text-decoration-color: red;
text-decoration-style: double;
text-decoration-thickness: 5px;
</style>
</head>
<body>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<p>A paragraph.</p>
</body>
</html>
text-decoration-line (required)
text-decoration-color (optional)
CSS-138
text-decoration-style (optional)
text-decoration-thickness (optional)
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration: underline;
h2 {
h3 {
p{
</style>
</head>
<body>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<p>A paragraph.</p>
</body>
</html>
CSS-139
It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter of each
word:
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
text-transform: uppercase;
[Link] {
text-transform: lowercase;
[Link] {
text-transform: capitalize;
CSS-140
</style>
</head>
<body>
</body>
</html>
text-indent
CSS-141
letter-spacing
line-height
word-spacing
white-space
Text Indentation
The text-indent property is used to specify the indentation of the first line of a text:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-indent: 50px;
</style>
</head>
<body>
<h1>Using text-indent</h1>
<p>In my younger and more vulnerable years my father gave me some advice that I've been
turning over in my mind ever since. 'Whenever you feel like criticizing anyone,' he told me, 'just
remember that all the people in this world haven't had the advantages that you've had.'</p>
</body>
</html>
Letter Spacing
The letter-spacing property is used to specify the space between the characters in a text.
The following example demonstrates how to increase or decrease the space between characters:
Example
CSS-142
<!DOCTYPE html>
<html>
<head>
<style>
h2 {
letter-spacing: 5px;
h3 {
letter-spacing: -2px;
</style>
</head>
<body>
<h1>Using letter-spacing</h1>
</body>
</html>
Line Height
The line-height property is used to specify the space between lines:
<!DOCTYPE html>
CSS-143
<html>
<head>
<style>
[Link] {
line-height: 0.7;
[Link] {
line-height: 1.8;
</style>
</head>
<body>
<h1>Using line-height</h1>
<p>
</p>
<p class="small">
</p>
<p class="big">
</p>
</body>
</html>
CSS-144
Word Spacing
The word-spacing property is used to specify the space between the words in a text.
The following example demonstrates how to increase or decrease the space between words:
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
word-spacing: 10px;
[Link] {
word-spacing: -2px;
</style>
</head>
<body>
<h1>Using word-spacing</h1>
</body>
</html>
White Space
The white-space property specifies how white-space inside an element is handled.
<!DOCTYPE html>
<html>
<head>
<style>
p{
white-space: nowrap;
</style>
</head>
<body>
<h1>Using white-space</h1>
<p>
CSS-146
</p>
</body>
</html>
In its simplest use, you only specify the horizontal shadow (2px) and the vertical shadow (2px):
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
CSS-148
<style>
h1 {
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
CSS-149
<style>
h1 {
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
</body>
</html>
Example 1
<!DOCTYPE html>
CSS-150
<html>
<head>
<style>
h1 {
color: white;
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
</body>
</html>
Example 2
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
</style>
</head>
<body>
</body>
CSS-151
</html>
Example 3
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
</style>
</head>
<body>
</body>
</html>
Example 4
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: white;
</style>
CSS-152
</head>
<body>
<h1>Text-shadow effect!</h1>
</body>
</html>
CSS Fonts
Choosing the right font for your website is important!
Choosing the right font has a huge impact on how the readers experience a website.
CSS-153
The right font can create a strong identity for your brand.
Using a font that is easy to read is important. The font adds value to your text. It is also important
to choose the correct color and text size for the font.
1. Serif fonts have a small stroke at the edges of each letter. They create a sense of formality and
elegance.
2. Sans-serif fonts have clean lines (no small strokes attached). They create a modern and
minimalistic look.
3. Monospace fonts - here all the letters have the same fixed width. They create a mechanical look.
All the different font names belong to one of the generic font families.
Note: On computer screens, sans-serif fonts are considered easier to read than serif fonts.
Note: If the font name is more than one word, it must be in quotation marks, like: "Times New Roman".
Tip: The font-family property should hold several font names as a "fallback" system, to ensure maximum
compatibility between browsers/operating systems. Start with the font you want, and end with a generic
family (to let the browser pick a similar font in the generic family, if no other fonts are available). The
font names should be separated with a comma. Read more about fallback fonts in the next chapter.
Example
<!DOCTYPE html>
<html>
CSS-154
<head>
<style>
.p1 {
.p2 {
.p3 {
</style>
</head>
<body>
<h1>CSS font-family</h1>
</body>
</html>
CSS-155
Web safe fonts are fonts that are universally installed across all browsers and devices.
Fallback Fonts
However, there are no 100% completely web safe fonts. There is always a chance that a font is
not found or is not installed properly.
Therefore, it is very important to always use fallback fonts.
This means that you should add a list of similar "backup fonts" in the font-family property. If the
first font does not work, the browser will try the next one, and the next one, and so on. Always
end the list with a generic font family name.
Example
Here, there are three font types: Tahoma, Verdana, and sans-serif. The second and third fonts are backups,
in case the first one is not found.
<!DOCTYPE html>
<html>
<head>
<style>
p{
}
CSS-156
</style>
</head>
<body>
<p>This is a paragraph.</p>
</body>
</html>
The following list are the best web safe fonts for HTML and CSS:
Arial (sans-serif)
Verdana (sans-serif)
Tahoma (sans-serif)
Trebuchet MS (sans-serif)
Georgia (serif)
Garamond (serif)
CSS-157
Note: Before you publish your website, always check how your fonts appear on different browsers and
devices, and always use fallback fonts!
Arial (sans-serif)
Arial is the most widely used font for both online and printed media. Arial is also the default font in
Google Docs.
Arial is one of the safest web fonts, and it is available on all major operating systems.
<!DOCTYPE html>
<html>
<head>
<style>
body {
</style>
</head>
<body>
<p>0 1 2 3 4 5 6 7 8 9</p>
</body>
</html>
Verdana (sans-serif)
Verdana is a very popular font. Verdana is easily readable even for small font sizes.
<!DOCTYPE html>
<html>
CSS-158
<head>
<style>
body {
</style>
</head>
<body>
<p>0 1 2 3 4 5 6 7 8 9</p>
</body>
</html>
Tahoma (sans-serif)
<!DOCTYPE html>
<html>
<head>
<style>
body {
</style>
</head>
<body>
<p>0 1 2 3 4 5 6 7 8 9</p>
</body>
</html>
Trebuchet MS (sans-serif)
Trebuchet MS was designed by Microsoft in 1996. Use this font carefully. Not supported by all mobile
operating systems.
<!DOCTYPE html>
<html>
<head>
<style>
body {
</style>
</head>
<body>
<p>0 1 2 3 4 5 6 7 8 9</p>
</body>
</html>
Times New Roman is one of the most recognizable fonts in the world. It looks professional and is used in
many newspapers and "news" websites. It is also the primary font for Windows devices and applications.
<!DOCTYPE html>
CSS-160
<html>
<head>
<style>
body {
</style>
</head>
<body>
<p>0 1 2 3 4 5 6 7 8 9</p>
</body>
</html>
Georgia (serif)
Georgia is an elegant serif font. It is very readable at different font sizes, so it is a good candidate for
mobile-responsive design.
<!DOCTYPE html>
<html>
<head>
<style>
body {
</style>
</head>
<body>
CSS-161
<p>0 1 2 3 4 5 6 7 8 9</p>
</body>
</html>
Garamond (serif)
Garamond is a classical font used for many printed books. It has a timeless look and good readability.
<!DOCTYPE html>
<html>
<head>
<style>
body {
</style>
</head>
<body>
<p>0 1 2 3 4 5 6 7 8 9</p>
</body>
</html>
Courier New is the most widely used monospace serif font. Courier New is often used with coding
displays, and many email providers use it as their default font. Courier New is also the standard font for
movie screenplays.
CSS-162
<!DOCTYPE html>
<html>
<head>
<style>
body {
</style>
</head>
<body>
<p>0 1 2 3 4 5 6 7 8 9</p>
</body>
</html>
The Brush Script MT font was designed to mimic handwriting. It is elegant and sophisticated, but can be
hard to read. Use it carefully.
<!DOCTYPE html>
<html>
<head>
<style>
body {
</style>
</head>
CSS-163
<body>
<p>0 1 2 3 4 5 6 7 8 9</p>
</body>
</html>
oblique - The text is "leaning" (oblique is very similar to italic, but less supported)
Example
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
font-style: normal;
[Link] {
font-style: italic;
[Link] {
font-style: oblique;
}
CSS-164
</style>
</head>
<body>
</body>
</html>
Font Weight
The font-weight property specifies the weight of a font:
Example
<!DOCTYPE html>
CSS-165
<html>
<head>
<style>
[Link] {
font-weight: normal;
[Link] {
font-weight: lighter;
[Link] {
font-weight: bold;
[Link] {
font-weight: 900;
</style>
</head>
<body>
</body>
</html>
CSS-166
Font Variant
The font-variant property specifies whether or not a text should be displayed in a small-caps font.
In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted
uppercase letters appears in a smaller font size than the original uppercase letters in the text.
Example
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
font-variant: normal;
[Link] {
font-variant: small-caps;
</style>
</head>
<body>
</body>
</html>
Being able to manage the text size is important in web design. However, you should not use font size
adjustments to make paragraphs look like headings, or headings look like paragraphs.
Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for paragraphs.
Absolute size:
Does not allow a user to change the text size in all browsers (bad for accessibility reasons)
Absolute size is useful when the physical size of the output is known
CSS-168
Relative size:
Note: If you do not specify a font size, the default size for normal text, like paragraphs, is 16px
(16px=1em).
Setting the text size with pixels gives you full control over the text size:
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
font-size: 40px;
h2 {
font-size: 30px;
p{
font-size: 14px;
</style>
</head>
<body>
<p>This is a paragraph.</p>
CSS-169
</body>
</html>
Tip: If you use pixels, you can still use the zoom tool to resize the entire page.
To allow users to resize the text (in the browser menu), many developers use em instead of pixels.
1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of 1em
is 16px.
The size can be calculated from pixels to em using this formula: pixels/16=em
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
h2 {
p{
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p>Specifying the font-size in em allows all major browsers to resize the text.
Unfortunately, there is still a problem with older versions of IE. When resizing the text, it
becomes larger/smaller than it should.</p>
</body>
</html>
In the example above, the text size in em is the same as the previous example in pixels. However, with the
em size, it is possible to adjust the text size in all browsers.
Unfortunately, there is still a problem with older versions of Internet Explorer. The text becomes larger
than it should when made larger, and smaller than it should when made smaller.
CSS-171
The solution that works in all browsers, is to set a default font-size in percent for the <body> element:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-size: 100%;
h1 {
font-size: 2.5em;
h2 {
font-size: 1.875em;
p{
font-size: 0.875em;
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p>Specifying the font-size in percent and em displays the same size in all major browsers, and
allows all browsers to resize the text!</p>
</body>
CSS-172
</html>
Our code now works great! It shows the same text size in all browsers, and allows all browsers to zoom or
resize the text!
The text size can be set with a vw unit, which means the "viewport width".
That way the text size will follow the size of the browser window:
<!DOCTYPE html>
<html>
<body>
<p style="font-size:5vw;">Resize the browser window to see how the text size scales.</p>
<p style="font-size:5vw;">Use the "vw" unit when sizing the text. 10vw will set the size to 10%
of the viewport width.</p>
<p>Viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is 50cm
wide, 1vw is 0.5cm.</p>
</body>
</html>
CSS-173
CSS Icons
Icons can easily be added to your HTML page, by using an icon library.
The simplest way to add an icon to your HTML page, is with an icon library, such as Font
Awesome.
Add the name of the specified icon class to any inline HTML element (like <i> or <span>).
All the icons in the icon libraries below, are scalable vectors that can be customized with CSS
(size, color, shadow, etc.)
To use the Font Awesome icons, go to [Link], sign in, and get a code to add in
the <head> section of your HTML page:
Read more about how to get started with Font Awesome in our Font Awesome 5 tutorial.
Example
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
For a complete reference of all Font Awesome icons, visit our Icon Reference.
Bootstrap Icons
To use the Bootstrap glyphicons, add the following line inside the <head> section of your HTML page:
Example
CSS-176
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Icons</title>
</head>
<body class="container">
<br><br>
</body>
</html>
CSS-177
Google Icons
To use the Google icons, add the following line inside the <head> section of your HTML page:
Example
<!DOCTYPE html>
<html>
<head>
<title>Google Icons</title>
</head>
<body>
<i class="material-icons">cloud</i>
<i class="material-icons">favorite</i>
<i class="material-icons">attachment</i>
<i class="material-icons">computer</i>
<i class="material-icons">traffic</i>
CSS-178
<br><br>
</body>
</html>
CSS Links
With CSS, links can be styled in many different ways.
Styling Links
CSS-179
Links can be styled with any CSS property (e.g. color, font-family, background, etc.).
Example
<!DOCTYPE html>
<html>
<head>
<style>
a{
color: hotpink;
</style>
</head>
<body>
</body>
</html>
CSS-180
In addition, links can be styled differently depending on what state they are in.
Example
<!DOCTYPE html>
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
/* visited link */
a:visited {
color: green;
}
CSS-181
a:hover {
color: hotpink;
/* selected link */
a:active {
color: blue;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order
to be effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be
effective.</p>
</body>
</html>
CSS-182
When setting the style for several link states, there are some order rules:
Text Decoration
Example
<!DOCTYPE html>
<html>
<head>
CSS-183
<style>
a:link {
text-decoration: none;
a:visited {
text-decoration: none;
a:hover {
text-decoration: underline;
a:active {
text-decoration: underline;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order
to be effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be
effective.</p>
</body>
</html>
CSS-184
Background Color
The background-color property can be used to specify a background color for links:
Example
<!DOCTYPE html>
<html>
<head>
<style>
CSS-185
a:link {
background-color: yellow;
a:visited {
background-color: cyan;
a:hover {
background-color: lightgreen;
a:active {
background-color: hotpink;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order
to be effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be
effective.</p>
</body>
</html>
CSS-186
Link Buttons
This example demonstrates a more advanced example where we combine several CSS properties to
display links as boxes/buttons:
Example
<!DOCTYPE html>
<html>
<head>
<style>
CSS-187
a:link, a:visited {
background-color: #f44336;
color: black;
text-align: center;
text-decoration: none;
display: inline-block;
a:hover, a:active {
background-color: red;
</style>
</head>
<body>
<h2>Link Button</h2>
</body>
</html>
CSS-188
More Examples
Example
<!DOCTYPE html>
<html>
<head>
<style>
[Link]:link {color:#ff0000;}
[Link]:visited {color:#0000ff;}
[Link]:hover {color:#ffcc00;}
[Link]:link {color:#ff0000;}
[Link]:visited {color:#0000ff;}
[Link]:hover {font-size:150%;}
[Link]:link {color:#ff0000;}
[Link]:visited {color:#0000ff;}
CSS-189
[Link]:hover {background:#66ff66;}
[Link]:link {color:#ff0000;}
[Link]:visited {color:#0000ff;}
[Link]:hover {font-family:monospace;}
[Link]:link {color:#ff0000;text-decoration:none;}
[Link]:visited {color:#0000ff;text-decoration:none;}
[Link]:hover {text-decoration:underline;}
</style>
</head>
<body>
<h2>Styling Links</h2>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
CSS-190
a:link, a:visited {
background-color: white;
color: black;
text-align: center;
text-decoration: none;
display: inline-block;
a:hover, a:active {
background-color: green;
color: white;
</style>
</head>
<body>
<h2>Link Button</h2>
</body>
</html>
Example
This example demonstrates the different types of cursors (can be useful for links):
<!DOCTYPE html>
<html>
<body>
<span style="cursor:auto">auto</span><br>
CSS-191
<span style="cursor:crosshair">crosshair</span><br>
<span style="cursor:default">default</span><br>
<span style="cursor:e-resize">e-resize</span><br>
<span style="cursor:help">help</span><br>
<span style="cursor:move">move</span><br>
<span style="cursor:n-resize">n-resize</span><br>
<span style="cursor:ne-resize">ne-resize</span><br>
<span style="cursor:nw-resize">nw-resize</span><br>
<span style="cursor:pointer">pointer</span><br>
<span style="cursor:progress">progress</span><br>
<span style="cursor:s-resize">s-resize</span><br>
<span style="cursor:se-resize">se-resize</span><br>
<span style="cursor:sw-resize">sw-resize</span><br>
<span style="cursor:text">text</span><br>
<span style="cursor:w-resize">w-resize</span><br>
<span style="cursor:wait">wait</span><br>
</body>
</html>