CSS Demo - One HTML Page - Multiple Styles!
CSS Demo - One HTML Page - Multiple Styles!
What is CSS?
CSS Example
Welcome to My Homepage
CSS Demo - One HTML Page - Use the menu to select different Stylesheets
No Styles
<head>
<style> </body>
body { </html>
background-color: lightblue;
color: white; HTML was NEVER intended to contain tags for formatting a web
page!
text-align: center;
HTML was created to describe the content of a web page, like:
}
<h1>This is a heading</h1>
p{
<p>This is a paragraph.</p>
font-family: verdana; When tags like <font>, and color attributes were added to the
HTML 3.2 specification, it started a nightmare for web developers.
font-size: 20px; Development of large websites, where fonts and color
information were added to every single page, became a long and
} expensive process.
</style>
To solve this problem, the World Wide Web Consortium (W3C)
created CSS.
If you don't know what HTML is, we suggest that you read our HTML
Tutorial. The selector points to the HTML element you want to style.
p{
color: red;
A CSS rule consists of a selector and a declaration block.
text-align: center;
}
Example Explained
CSS Syntax
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
You will learn much more about CSS selectors and CSS properties in the
next chapters!
A CSS selector selects the HTML element(s) you want to style. Example
CSS Selectors Here, all <p> elements on the page will be center-aligned, with a red
text color:
CSS selectors are used to "find" (or select) the HTML elements
you want to style.
In this example all HTML elements with class="center" will be red and
Example center-aligned:
The CSS rule below will be applied to the HTML element with .center {
id="para1": text-align: center;
color: red;
}
#para1 {
text-align: center; You can also specify that only specific HTML elements should be
color: red; affected by a class.
}
Example
Note: An id name cannot start with a number!
In this example only <p> elements with class="center" will be red and
center-aligned:
p.center {
text-align: center;
The CSS class Selector color: red;
}
The class selector selects HTML elements with a specific class
attribute. HTML elements can also refer to more than one class.
h1 {
<p class="center large">This paragraph refers to two classes.</p> text-align: center;
color: red;
}
p{
The CSS Universal Selector text-align: center;
color: red;
The universal selector (*) selects all HTML elements on the page. }
The CSS rule below will affect every HTML element on the page:
Example
*{
text-align: center; In this example we have grouped the selectors from the code above:
color: blue;
}
h1, h2, p {
text-align: center;
color: red;
}
The CSS Grouping Selector
The grouping selector selects all the HTML elements with the
same style definitions.
All CSS Simple Selectors External styles are defined within the <link> element, inside the
<head> section of an HTML page:
There are three ways of inserting a style sheet: element,element, div, p Selects all <div> elements and all
.. <p> elements
External CSS
Internal CSS
Inline CSS DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
External CSS <body>
With an external style sheet, you can change the look of an entire <h1>This is a heading</h1>
website by changing just one file! <p>This is a paragraph.</p>
Example 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. The internal style is defined inside the <style> element, inside
the head section.
Here is how the "mystyle.css" file looks:
Example
"mystyle.css" Internal styles are defined within the <style> element, inside the
<head> section of an HTML page:
body {
background-color: lightblue;
<!DOCTYPE html>
}
<html>
<head>
h1 {
<style>
color: navy;
body {
margin-left: 20px;
background-color: linen;
}
}
h1 {
Note: Do not add a space between the property value (20) and the unit color: maroon;
(px): margin-left: 40px;
Incorrect (space): margin-left: 20 px; }
Correct (no space): margin-left: 20px; </style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
Internal CSS
An internal style sheet may be used if one single HTML page has </body>
a unique style. </html>
Multiple Style Sheets
To use inline styles, add the style attribute to the relevant Assume that an external style sheet has the following style for the
element. The style attribute can contain any CSS property. <h1> element:
Example h1 {
color: navy;
}
Inline styles are defined within the "style" attribute of the relevant
element:
Then, assume that an internal style sheet also has the following style
for the <h1> element:
<!DOCTYPE html>
<html>
<body> h1 {
color: orange;
}
<h1 style="color:blue;text-align:center;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
</html>
Example
Tip: An inline style loses many of the advantages of a style sheet (by
mixing content with presentation). Use this method sparingly. If the internal style is defined after the link to the external style sheet,
the <h1> elements will be "orange":
<head> 1. Inline style (inside an HTML element)
<link rel="stylesheet" type="text/css" href="mystyle.css"> 2. External and internal style sheets (in the head section)
<style>
h1 {
3. Browser default
color: orange;
} So, an inline style has the highest priority, and will override
</style> external and internal styles and browser defaults.
</head>
CSS Comments
Example
CSS comments are not displayed in the browser, but they can
However, if the internal style is defined before the link to the external
help document your source code.
style sheet, the <h1> elements will be "navy":
<head>
<style> CSS Comments
h1 {
color: orange; Comments are used to explain the code, and may help when you
} edit the source code at a later date.
</style>
<link rel="stylesheet" type="text/css" href="mystyle.css"> Comments are ignored by browsers.
</head>
A CSS comment is placed inside the <style> element, and starts
with /* and ends with */:
Cascading Order
Example
What style will be used when there is more than one style
specified for an HTML element?
/* This is a single-line comment */
p{
All the styles in a page will "cascade" into a new "virtual" style
color: red;
sheet by the following rules, where number one has the highest
}
priority:
You can add comments wherever you want in the code: <!DOCTYPE html>
<html>
<head>
Example <style>
p{
color: red; /* Set text color to red */
p{ }
color: red; /* Set text color to red */ </style>
} </head>
<body>
Comments can also span multiple lines:
<h2>My Heading</h2>
HTML and CSS Comments Colors are specified using predefined color names, or RGB, HEX,
HSL, RGBA, HSLA values.
From the HTML tutorial, you learned that you can add comments
to your HTML source by using the <!--...--> syntax.
Hello World
Example
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
RGB Value
RGBA Value
Shades of gray are often defined using equal values for all the 3
light sources:
Where rr (red), gg (green) and bb (blue) are hexadecimal values
between 00 and ff (same as decimal 0-255).
HEX Value
3 Digit HEX Value
In CSS, a color can be specified using a hexadecimal value in the
form:
Sometimes you will see a 3-digit hex code in the CSS source.
#rrggbb
The 3-digit hex code is a shorthand for some 6-digit hex codes.
The 3-digit hex code has the following form: HSL stands for hue, saturation, and lightness.
#rgb
The 3-digit hex code can only be used when both the values (RR, In CSS, a color can be specified using hue, saturation, and
GG, and BB) are the same for each component. So, if we have lightness (HSL) in the form:
#ff00cc, it can be written like this: #f0c.
hsl(hue, saturation, lightness)
Here is an example:
Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is
green, and 240 is blue.
Example
Saturation is a percentage value. 0% means a shade of gray, and
100% is the full color.
body {
background-color: #fc9; /* same as #ffcc99 */ Lightness is also a percentage. 0% is black, 50% is neither light
} or dark, 100% is white
p{
color: #b58; /* same as #bb5588 */
}
Saturation
Saturation can be described as the intensity of a color. means 50% light (neither dark nor light) and 100% means full
lightness (white).
100% is pure color, no shades of gray.
50% is 50% gray, but you can still see the color. Example
Example
Shades of Gray
Example
Lightness
The lightness of a color can be described as how much light you HSLA Value
want to give the color, where 0% means no light (black), 50%
HSLA color values are an extension of HSL color values with an
alpha channel - which specifies the opacity for a color.