What is CSS
What is CSS
h1 {
color: white;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
}
</style>
</head>
<body>
</body>
</html>
CSS Syntax
A CSS rule consists of a selector and a declaration block.
CSS Syntax
<!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>
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
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.
</body>
</html>
The 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>
</body>
</html>
Note: An id name cannot start with a number!
The 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>
</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>
p.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
HTML elements can also refer to more than one class.
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>
</body>
</html>
Note: A class name cannot start with a number!
<h1>Hello world!</h1>
</body>
</html>
The CSS Grouping Selector
The grouping selector selects all the HTML elements with the
same style definitions.
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;
}
t 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:
h1, h2, p {
text-align: center;
color: red;
}
All CSS Simple Selectors
Selector Example Example description
#id #firstname Selects the element
with id="firstname"
.class .intro Selects all elements
with class="intro"
element.class p.intro Selects only <p>
elements with
class="intro"
* * Selects all elements
element p Selects all <p> elements
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.
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>
n 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;
margin-left: 20px;
}
Note: Do not add a space between the property value (20) and
the
Note: Do not add a space between the property value (20) and
the unit (px):
Incorrect (space): margin-left: 20 px;
Correct (no space): 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>
<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>
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="mystyle.css">
</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>
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="mystyle.css">
<style>
body {background-color: linen;}
</style>
</head>
<body style="background-color: lavender">
</body>
</html>