Cascading Style Sheets
CSS
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
CSS
Why Use CSS?
CSS is used to define styles for your web pages, including the design,
layout and variations in display for different devices and screen sizes .
How To Add CSS
There are three ways of inserting a style sheet
CSS
Three Ways to Insert CSS
Inline CSS
Internal CSS
External CSS
CSS
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
The selector points to the HTML element you want to style.
The declaration block contains one or more declarations separated by
semicolons.
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.
CSS
Inline CSS
To use inline styles, add the style attribute to the relevant element. The
style attribute can contain any CSS property.
Example:
<h1 style="color:blue;">This is a heading</h1>
CSS
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:
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
}
</style>
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:
<link rel="stylesheet" href="mystyle.css">
SELECTORS
CSS
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you
want to style.
The CSS element Selector
The element selector selects HTML elements based on the element
name. Example:
p {
text-align: center;
color: red;
}
CSS
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:
#para1 {
text-align: center;
color: red;
}
Note: An id name cannot start with a number!
CSS
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.
HTML elements can also refer to more than one class.
Example:
.center {
text-align: center;
color: red;
}
Note: A class name cannot start with a number!
CSS
The CSS Universal Selector
The universal selector (*) selects all HTML elements on the page.
Example:
*{
text-align: center;
color: blue;
}
Note: A class name cannot start with a number!
CSS
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;
}
CSS
The CSS Grouping Selector
It will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
Example:
h1, h2, p {
text-align: center;
color: red;
}
COMMENTS
COLORS
CSS
CSS Color Names
In CSS, a color can be specified by using a color name
CSS Text Color
You can set the color of text:
<h1 style="color:Tomato;">Hello World</h1>
CSS
CSS Background Color
You can set the background color for HTML elements:
<h1 style="background-color:DodgerBlue;">Hello World</h1>
CSS Color Values
In CSS, colors can also be specified using RGB values, HEX values, HSL
values, RGBA values, and HSLA values:
RGB Value
rgb(red, green, blue)
HEX Value
In CSS, a color can be specified using a hexadecimal value in the form:
CSS
HSL Value
In CSS, a color can be specified using hue, saturation, and lightness
(HSL) in the form