CSS Notes
CSS Notes
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
Welcome to My Homepage
Use the menu to select different Stylesheets
Stylesheet 1
Stylesheet 2
Stylesheet 3
Stylesheet 4
No Stylesheet
Same Page
Different
Stylesheets
This is a
demonstration of
how different
stylesheets can
change the layout of
your HTML page.
You can change the
layout of this page
by selecting
different stylesheets
in the menu, or by
selecting one of the
following links:
Stylesheet1, Stylesh
eet2, Stylesheet3, St
ylesheet4.
No Styles
This page uses DIV
elements to group
different sections of
the HTML page.
Click here to see
how the page looks
like with no
stylesheet:
No Stylesheet.
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. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum
dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit
augue duis dolore te feugait nulla facilisi.
CSS Example
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p {
font-family: verdana;
font-size: 20px;
}
CSS Solved a Big Problem
HTML was NEVER intended to contain tags for formatting a web page!
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2
specification, it started a nightmare for web developers. Development of
large websites, where fonts and color information were added to every
single page, became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created
CSS.
With an external stylesheet file, you can change the look of an entire
website by changing just one file!
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:
p {
color: red;
text-align: center;
}
Example Explained
p is a selector in CSS (it points to the HTML element you want to
style: <p>).
color is a property, and red is the property value
text-align is a property, and center is the property value
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.
Example
Here, all <p> elements on the page will be center-aligned, with a red text
color:
p {
text-align: center;
color: red;
}
Example
The CSS rule below will be applied to the HTML element with id="para1":
#para1 {
text-align: center;
color: red;
}
Example
In this example all HTML elements with class="center" will be red and center-
aligned:
.center {
text-align: center;
color: red;
}
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:
p.center {
text-align: center;
color: red;
}
Example
In this example the <p> element will be styled according to class="center" and
to class="large":
Example
The CSS rule below will affect every HTML element on the page:
* {
text-align: center;
color: blue;
}
Look at the following CSS code (the h1, h2, and p elements have the
same style definitions):
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p {
text-align: center;
color: red;
}
Example
In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
External CSS
Internal CSS
Inline CSS
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="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
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.
"mystyle.css"
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Note: Do not add a space between the property value and the unit:
Incorrect (space): margin-left: 20 px;
Correct (nospace): margin-left: 20px;
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>
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>
</body>
</html>
Tip: An inline style loses many of the advantages of a style sheet (by mixing
content with presentation). Use this method sparingly.
Assume that an external style sheet has the following style for the <h1>
element:
h1 {
color: navy;
}
Then, assume that an internal style sheet also has the following style for the
<h1> element:
h1 {
color: orange;
}
Example
If the internal style is defined after the link to the external style sheet, the
<h1> elements will be "orange":
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
Example
However, if the internal style is defined before the link to the external style
sheet, the <h1> elements will be "navy":
<head>
<style>
h1 {
color: orange;
}
</style>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
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:
So, an inline style has the highest priority, and will override external and
internal styles and browser defaults.
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.
Example
Example
p {
color: red; /* Set text color to red */
}
Example
/* This is
a multi-line
comment */
p {
color: red;
}
Example
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: red; /* Set text color to red */
}
</style>
</head>
<body>
<h2>My Heading</h2>
CSS Colors
Colors are specified using predefined color names, or RGB, HEX,
HSL, RGBA, HSLA values.
Tomato
Orange
DodgerBlue
MediumSeaGreen
Gray
SlateBlue
Violet
LightGray
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.
Example
Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.
Example
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
Hello World
Hello World
Hello World
Example
#ff6347
CSS Backgrounds
The CSS background properties are used to add background effects
for elements.
In these chapters, you will learn about the following CSS background
properties:
background-color
background-image
background-repeat
background-attachment
background-position
background (shorthand property)
CSS background-color
The background-color property specifies the background color of an
element.
Example
body {
background-color: lightblue;
}
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:
opacity 1
opacity 0.6
opacity 0.3
opacity 0.1
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.
100% opacity
60% opacity
30% opacity
10% opacity
You learned from our CSS Colors Chapter, that you can use RGB as a color
value. In addition to RGB, you can use an RGB color value with
an alpha channel (RGBA) - which specifies the opacity for a color.
Tip: You will learn more about RGBA Colors in our CSS Colors Chapter.
Example
div {
background: rgba(0, 128, 0, 0.3) /* Green background with 30% opacity
*/
}
The CSS Background Color Property
Property Description
CSS Borders
The CSS border properties allow you to specify the style, width, and
color of an element's border.
The border-style property can have from one to four values (for the top
border, right border, bottom border, and the left border).
Example
Result:
A dotted border.
A dashed border.
A solid border.
A double border.
No border.
A hidden border.
A mixed border.
Note: None of the OTHER CSS border properties (which you will learn more about
in the next chapters) will have ANY effect unless the border-style property is set!
CSS Margins
Margins are used to create space around elements, outside of any
defined borders.
CSS Margins
The CSS margin properties are used to create space around elements,
outside of any defined borders.
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).
margin-top
margin-right
margin-bottom
margin-left
Example
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
margin-top
margin-right
margin-bottom
margin-left
Example
p {
margin: 25px 50px 75px 100px;
}
Example
p {
margin: 25px 50px 75px;
}
Example
margin: 25px;
o all four margins are 25px
Example
p {
margin: 25px;
}
Try it Yourself »
The element will then take up the specified width, and the remaining
space will be split equally between the left and right margins.
Example
div {
width: 300px;
margin: auto;
border: 1px solid red;
}
Example
div {
border: 1px solid red;
margin-left: 100px;
}
p.ex1 {
margin-left: inherit;
}
margin A shorthand property for setting all the margin properties in one de
CSS Padding
Padding is used to create space around an element's content, inside
of any defined borders.
This element has a padding of 70px.
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).
padding-top
padding-right
padding-bottom
padding-left
Example
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
Padding - Shorthand Property
To shorten the code, it is possible to specify all the padding properties in
one property.
padding-top
padding-right
padding-bottom
padding-left
Example
div {
padding: 25px 50px 75px 100px;
}
Example
Example
div {
padding: 25px 50px;
}
padding: 25px;
o all four paddings are 25px
Example
div {
padding: 25px;
}
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;
}
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:
div {
width: 300px;
padding: 25px;
box-sizing: border-box;
}
More Examples
Set the left padding
This example demonstrates how to set the left padding of a <p> element.
auto - This is default. The browser calculates the height and width
length - Defines the height/width in px, cm, etc.
% - Defines the height/width in percent of the containing block
initial - Sets the height/width to its default value
inherit - The height/width will be inherited from its parent value
Example
div {
height: 200px;
width: 50%;
background-color: powderblue;
}
This element has a height of 100 pixels and a width of 500 pixels.
Example
div {
height: 100px;
width: 500px;
background-color: powderblue;
}
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.
Tip: Drag the browser window to smaller than 500px wide, to see the
difference between the two divs!
This element has a height of 100 pixels and a max-width of 500 pixels.
Note: If you for some reason 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 will be used
(and the width property will be ignored).
Example
This <div> element has a height of 100 pixels and a max-width of 500 pixels:
div {
max-width: 500px;
height: 100px;
background-color: powderblue;
}
The CSS box model is essentially a box that wraps around every HTML
element. It consists of: margins, borders, padding, and the actual
content. The image below illustrates the box model:
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is
transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is
transparent
The box model allows us to add a border around elements, and to define
space between elements.
Example
Demonstration of the box model:
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
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 full
size of an element, you must also add padding, borders and margins.
Example
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
Total element width = width + left padding + right padding + left border
+ right border + left margin + right margin
CSS Outline
An outline is a line drawn outside the element's border.
This element has a black border and a green outline with a width of
10px.
CSS Outline
An outline is a line that is drawn around elements, OUTSIDE the borders, to make the
element "stand out".
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
Result:
A dotted outline.
A dashed outline.
A solid outline.
A double outline.
Note: None of the other outline properties (which you will learn more about in the
next chapters) will have ANY effect unless the outline-style property is set!
CSS Text
CSS has a lot of properties for formatting text.
TEXT FORMATTING
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 "Try it Yourself" link.
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.
Example
body {
color: blue;
}
h1 {
color: green;
}
Example
body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: white;
}
div {
background-color: blue;
color: white;
}
Important: High contrast is very important for people with vision problems. So,
always ensure that the contrast between the text color and the background color
(or background image) is good!
CSS Fonts
Choosing the right font for your website is important!
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.
4. Cursive fonts imitate human handwriting.
5. Fantasy fonts are decorative/playful fonts.
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".
Example
Specify some different fonts for three paragraphs:
.p1 {
font-family: "Times New Roman", Times, serif;
}
.p2 {
font-family: Arial, Helvetica, sans-serif;
}
.p3 {
font-family: "Lucida Console", "Courier New", monospace;
CSS Icons
Icons can easily be added to your HTML page, by using an icon library.
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.)
<script src="https://kit.fontawesome.com/yourcode.js"
crossorigin="anonymous"></script>
Read more about how to get started with Font Awesome in our Font Awesome 5 tutorial.
<!DOCTYPE html>
<html>
<head>
<script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anon
ymous"></script>
</head>
<body>
</body>
</html>
Result:
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:
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstr
ap.min.css">
Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/
3.3.7/css/bootstrap.min.css">
</head>
<body>
<i class="glyphicon glyphicon-cloud"></i>
<i class="glyphicon glyphicon-remove"></i>
<i class="glyphicon glyphicon-user"></i>
<i class="glyphicon glyphicon-envelope"></i>
<i class="glyphicon glyphicon-thumbs-up"></i>
</body>
</html>
Google Icons
To use the Google icons, add the following line inside the <head> section
of your HTML page:
Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?
family=Material+Icons">
</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>
</body>
</html>
CSS Links
With CSS, links can be styled in many different ways.
Styling Links
Links can be styled with any CSS property (e.g. color, font-
family, background, etc.).
Example
a {
color: hotpink;
}
Example
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* selected link */
a:active {
color: blue;
}
When setting the style for several link states, there are some order rules:
Text Decoration
The text-decoration property is mostly used to remove underlines from
links:
Example
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: underline;
}
Background Color
The background-color property can be used to specify a background color
for links:
Example
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
}
a:hover {
background-color: lightgreen;
}
a:active {
background-color: hotpink;
}
Link Buttons
This example demonstrates a more advanced example where we combine
several CSS properties to display links as boxes/buttons:
Example
a:link, a:visited {
background-color: #f44336;
color: white;
padding: 14px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a:hover, a:active {
background-color: red;
}
CSS Lists
Unordered Lists:
o Coffee
o Tea
o Coca Cola
Coffee
Tea
Coca Cola
Ordered Lists:
1. Coffee
2. Tea
3. Coca Cola
I. Coffee
II. Tea
III. Coca Cola
unordered lists (<ul>) - the list items are marked with bullets
ordered lists (<ol>) - the list items are marked with numbers or
letters
The following example shows some of the available list item markers:
Example
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
Note: Some of the values are for unordered lists, and some for ordered
lists.
Example
ul {
list-style-image: url('sqpurple.gif');
}
"list-style-position: inside;" means that the bullet points will be inside the
list item. As it is part of the list item, it will be part of the text and push
the text at the start:
Coffee - A brewed drink prepared from roasted coffee beans...
Tea
Coca-cola
Example
ul.a {
list-style-position: outside;
}
ul.b {
list-style-position: inside;
}
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
Try it Yourself »
Example
ul {
list-style: square inside url("sqpurple.gif");
}
When using the shorthand property, the order of the property values are:
If one of the property values above is missing, the default value for the
missing property will be inserted, if any.
Anything added to the <ol> or <ul> tag, affects the entire list, while
properties added to the <li> tag will affect the individual list items:
Example
ol {
background: #ff9999;
padding: 20px;
}
ul {
background: #3399ff;
padding: 20px;
}
ol li {
background: #ffe5e5;
color: darkred;
padding: 5px;
margin-left: 35px;
}
ul li {
background: #cce5ff;
color: darkblue;
margin: 5px;
}
Result:
1. Coffee
2. Tea
3. Coca Cola
Coffee
Tea
Coca Cola
Try it Yourself »
More Examples
Customized list with a red left border
This example demonstrates how to create a list with a red left border.
More Examples
Example
Example
a:link, a:visited {
background-color: white;
color: black;
border: 2px solid green;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a:hover, a:active {
background-color: green;
color: white;
}
Example
This example demonstrates the different types of cursors (can be useful for
links):
CSS Tables
The look of an HTML table can be greatly improved with CSS:
Company Contact
Table Borders
To specify table borders in CSS, use the border property.
The example below specifies a solid border for <table>, <th>, and <td>
elements:
Firstname Lastname
Peter Griffin
Lois Griffin
Example
table, th, td {
border: 1px solid;
}
Full-Width Table
Firstname
Peter
Lois
The table above might seem small in some cases. If you need a table that
should span the entire screen (full-width), add width: 100% to the
<table> element:
Example
table {
width: 100%;
}
Double Borders
Notice that the table in the examples above have double borders. This is because
both the table and the <th> and <td> elements have separate borders.
Firstname
Peter
Lois
table {
border-collapse: collapse;
}
Firstname Lastname
Peter Griffin
Lois Griffin
Example
table {
border: 1px solid;
}
Every HTML element has a default display value depending on what type
of element it is. The default display value for most elements
is block or inline.
<div>
<h1> - <h6>
<p>
<form>
<header>
<footer>
<section>
Inline Elements
An inline element does not start on a new line and only takes up as much
width as necessary.
<span>
<a>
<img>
Display: none;
display: none; is commonly used with JavaScript to hide and show
elements without deleting and recreating them. Take a look at our last
example on this page if you want to know how this can be achieved.
Example
li {
display: inline;
}
Note: Setting the display property of an element only changes how the element
is displayed, NOT what kind of element it is. So, an inline element with display:
block; is not allowed to have other block elements inside it.
Example
span {
display: block;
}
Example
a {
display: block;
}
Hide an Element - display:none or
visibility:hidden?
display:none
Remove
visibility:hidden
Hide
Reset
Reset All
Example
h1.hidden {
display: none;
}
However, the element will still take up the same space as before. The
element will be hidden, but still affect the layout:
Example
h1.hidden {
visibility: hidden;
}
More Examples
Differences between display: none; and visibility: hidden;
This example demonstrates display: none; versus visibility: hidden;
Property Description