Chapter Three CSS
Chapter Three CSS
(CSS)
What is CSS?
CSS is a style sheet language which is used to describe the look and formatting of a document
written in markup language. It provides an additional feature to HTML. It is generally used
with HTML to change the style of web pages and user interfaces.
CSS is used along with HTML and JavaScript in most websites to create user interfaces for
web applications and user interfaces for many mobile applications.
HTML was NEVER intended to contain tags for formatting a web page! HTML was created
to describe the content of a web page, like:
<h1> This is a heading </h1>
<p> This is a paragraph. </p>
When tags like <font>, and color attributes were added to the HTML specifications, 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.
CSS removed the style formatting from the HTML page.
1
CSS Saves a Lot of Work!
The style definitions are normally saved in external .css files.
With an external style sheet 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
Selector: the selector points to the HTML element you want to style. It could be any tag like
<h1>, <title> etc.
The declaration block: the declaration block contains one or more declarations separated
by semicolons. For the above example, there are two declarations:
1. color: blue;
2. font-size: 12px;
Each declaration includes a CSS property name and a value, separated by a colon.
Multiple CSS declarations are separated with semicolons, and declaration blocks are
surrounded by curly braces.
Property: A Property is a type of attribute of HTML element. It could be color, border etc.
Value: Values are assigned to CSS properties. In the above example, value "blue" is
assigned to color property.
Selector { Property1: value1; Property2: value2; ..........; }
Example
In this example all <p> elements will be center-aligned, with a red text color:
p {
color: red;
text-align: center;
}
Example Explained
2
• 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 are used to
"find" (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
The element selector selects HTML elements based on the element name.
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>
Output:
3
CSS id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique
element!
To select an element with a specific id, write a hash (#) character, followed by the id of the
element.
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>
4
CSS class Selector
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
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>
Output:
5
Example
In this example the <p> element will be styled according to class="center" and to
class="large"
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
p.large {
font-size: 300%;
}
</style>
</head>
<body>
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page.
6
<!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>
Output:
p {
7
text-align: center;
color: red;
}
It will be better to group the selectors, to minimize the code. To group selectors, separate each
selector with a comma.
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>
8
element,element,.. div, p Selects all <div> elements and all <p> elements
When a browser reads a style sheet, it will format the HTML document according to
the information in the style sheet.
Three Ways to Insert CSS
There are three ways of inserting a style sheet:
• 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.
<!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.
Here is how the "mystyle.css" file looks:
"mystyle.css"
body {
background-color: lightblue;
}
h1 {
color: navy;
9
margin-left: 20px;
}
Note: Do not add a space between the property value and the unit (such as margin-left:
20 px;). The correct way is: 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:
10
<!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>
11
a multi-line
comment */
p {
color: red;
}
12