Cascading Style Sheets
Cascading Style Sheets
Cascading Style Sheets
p {
text-align: center;
color: red;
}
CSS Selectors
The id Selector:
The id selector uses the id attribute of an HTML element to
select a specific element.
#para1 {
text-align: center;
color: red;
}
Note: An id name cannot start with a number!
CSS Selectors
The class Selector:
The class selector selects elements with a specific class
attribute.
.center {
text-align: center;
color: red;
}
CSS Selectors
Example
In this example only <p> elements with
class="center" will be center-aligned:
p.center {
text-align: center;
color: red;
}
Note: A class name cannot start with a number!
CSS Selectors
Grouping Selectors:
If you have elements with the same style definitions, like this:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p {
text-align: center;
color: red;
}
CSS Selectors
Grouping Selectors:
It will be better to group the selectors, to minimize the code.
h1, h2, p {
text-align: center;
color: red;
}
CSS Comments
Comments are used to explain the code, and may help
when you edit the source code at a later date.
p {
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is
a multi-line
comment */
Three Ways to Insert CSS
There are three ways of inserting a style sheet:
1. External style sheet
2. Internal style sheet
3. Inline style
External Style Sheet
With an external style sheet, you can change the look of
an entire website by changing just one file!
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Internal Style Sheet
An internal style sheet may be used if one single page has
a unique style.
Internal Style Sheet
Example
Internal styles are defined within the <style> element,
inside the <head> section of an HTML page:
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
Inline Styles
An inline style may be used to apply a unique style for a
single element.
<h1 style="color:blue;margin-
left:30px;">This is a heading</h1>