WD Unit 3 (A)
WD Unit 3 (A)
What is CSS?
Here we will show one HTML page displayed with four different stylesheets. Click
on the "Stylesheet 1", "Stylesheet 2", "Stylesheet 3", "Stylesheet 4" links below
to see the different styles:
<html>
<head>
<style>
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
}
</style>
</head>
<body>
<h1>My First CSS Example</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS Solved a Big Problem
HTML was NEVER intended to contain tags for formatting a web page!
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2
specification, 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.
With an external stylesheet file, you can change the look of an entire website by
changing just one file!
CSS Syntax
CSS Syntax
Multiple CSS declarations are separated with semicolons, and declaration blocks
are surrounded by curly braces.
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
CSS Selectors
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.
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:
p {
text-align: center;
color: red;
}
To select an element with a specific id, write a hash (#) character, followed by
the id of the element.
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
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.
<!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.
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
p.large {
font-size: 300%;
}
</style>
</head>
<body>
</body>
</html>
The universal selector (*) selects all HTML elements on the page.
<!DOCTYPE html>
<html>
<head>
<style>
*{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
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;
}
h1, h2, p {
text-align: center;
color: red;
}
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.
"mystyle.css"
body {
background-color: lightblue;
}
h1 {
color: navy;
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.
<!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.
Inline styles are defined within the "style" attribute of the relevant
element:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Assume that an external style sheet has the following style for the <h1>
element:
h1 {
color: navy;
}
Then, assume that an internal style sheet also has the following style for the
<h1> element:
h1 {
color: orange;
}
Example
If the internal style is defined after the link to the external style sheet, the
<h1> elements will be "orange":
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
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:
So, an inline style has the highest priority, and will override external and internal
styles and browser defaults.
<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>
CSS Comments
Comments are used to explain the code, and may help when you edit the source
code at a later date.
A CSS comment is placed inside the <style> element, and starts with /* and ends
with */:
<!DOCTYPE html>
<html>
<head>
<style>
/* This is a single-line comment */
p{
color: red;
}
</style>
</head>
<body>
<p>Hello World!</p>
<p>This paragraph is styled with CSS.</p>
<p>CSS comments are not shown in the output.</p>
</body>
</html>
Example
p {
color: red; /* Set text color to red */
}
Example
/* This is
a multi-line
comment */
p {
color: red;
}
CSS Backgrounds
CSS background-color
body {
background-color: lightblue;
}
Opacity / Transparency
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: green;
}
div.first {
opacity: 0.1;
}
div.second {
opacity: 0.3;
}
div.third {
opacity: 0.6;
}
</style>
</head>
<body>
<h1>Transparent Boxes</h1>
<div class="first">
<h1>opacity 0.1</h1>
</div>
<div class="second">
<h1>opacity 0.3</h1>
</div>
<div class="third">
<h1>opacity 0.6</h1>
</div>
<div>
<h1>opacity 1 (default)</h1>
</div>
</body>
</html>
Note: When using the opacity property to add transparency to the background
of an element, all of its child elements inherit the same transparency. This can
make the text inside a fully transparent element hard to read.
CSS Backgrounds
The CSS background properties are used to add background effects for
elements.
background-color
background-image
background-repeat
background-attachment
background-position
background (shorthand property)
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("abc.gif");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Note: When using a background image, use an image that does not disturb the
text.
The background image can also be set for specific elements, like the <p>
element:
p {
background-image: url("xyz.gif");
}
CSS background-repeat
Some images should be repeated only horizontally or vertically, or they will look
strange, like this:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("ab.png");
background-repeat: repeat-y;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Here, a background image is repeated only horizontally!</p>
</body>
</html>
CSS background-position
The background-position property is used to specify the position of the
background image.
body {
background-image: url("abc.png");
background-repeat: no-repeat;
background-position: right top;
}
CSS background-attachment
The background-attachment property specifies whether the background image
should scroll or be fixed (will not scroll with the rest of the page):
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
margin-right: 200px;
/* background-attachment: fixed; */
}
</style>
</head>
<body>
</body>
</html>
Example
Specify that the background image should scroll with the rest of the page:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: scroll;
}
Instead of writing:
body {
background-color: #ffffff;
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
Use the shorthand property to set the background properties in one
declaration:
body {
background: #ffffff url("img_tree.png") no-repeat fixed right top;
}
When using the shorthand property the order of the property values is:
background-color
background-image
background-repeat
background-attachment
background-position
CSS Borders
CSS Border Style
The border-style property can have from one to four values (for the top border,
right border, bottom border, and the left border).
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: dotted;
border-width: 2px;
}
p.four {
border-style: dotted;
border-width: thick;
}
p.one {
border-style: solid;
border-width: 5px 20px; /* 5px top and bottom, 20px on the sides */
}
p.two {
border-style: solid;
border-width: 20px 5px; /* 20px top and bottom, 5px on the sides */
}
p.three {
border-style: solid;
border-width: 25px 10px 4px 35px; /* 25px top, 10px right, 4px bottom
and 35px left */
}
CSS Border Color
p.one {
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: green;
}
p.three {
border-style: dotted;
border-color: blue;
}
p.one {
border-style: solid;
border-color: red green blue yellow; /* red top, green right, blue
bottom and yellow left */
}
HEX Values
The color of the border can also be specified using a hexadecimal value (HEX):
Example
p.one {
border-style: solid;
border-color: #ff0000; /* red */
}
RGB Values
Or by using RGB values:
Example
p.one {
border-style: solid;
border-color: rgb(255, 0, 0); /* red */
}
HSL Values
Example
p.one {
border-style: solid;
border-color: hsl(0, 100%, 50%); /* red */
}
In CSS, there are also properties for specifying each of the borders (top, right,
bottom, and left):
p {
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
border-style: dotted;
o all four borders are dotted
To shorten the code, it is also possible to specify all the individual border
properties in one property.
The border property is a shorthand property for the following individual border
properties:
border-width
border-style (required)
border-color
p {
border: 5px solid red;
}
p {
border: 2px solid red;
border-radius: 5px;
}
CSS Margins
The CSS margin properties are used to create space around elements, outside of
any defined borders.
With CSS, you have full control over the margins. There are properties for
setting the margin for each side of an element (top, right, bottom, and left).
margin-top
margin-right
margin-bottom
margin-left
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 1px solid black;
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
background-color: lightblue;
}
</style>
</head>
<body>
<h2>Using individual margin properties</h2>
<div>This div element has a top margin of 100px, a right margin of 150px, a
bottom margin of 100px, and a left margin of 80px.</div>
</body>
</html>
The element will then take up the specified width, and the remaining space will
be split equally between the left and right margins.
div {
width: 300px;
margin: auto;
border: 1px solid red;
}
div {
border: 1px solid red;
margin-left: 100px;
}
p.ex1 {
margin-left: inherit;
}
CSS Padding
The CSS padding properties are used to generate space around an element's
content, inside of any defined borders.
With CSS, you have full control over the padding. There are properties for
setting the padding for each side of an element (top, right, bottom, and left).
padding-top
padding-right
padding-bottom
padding-left
The padding property is a shorthand property for the following individual padding
properties:
padding-top
padding-right
padding-bottom
padding-left
div {
padding: 25px 50px 75px 100px;
}
The height and width properties are used to set the height and width of an
element.
The height and width properties do not include padding, borders, or margins. It
sets the height/width of the area inside the padding, border, and margin of the
element.
CSS height and width Values
The height and width properties may have the following values:
auto - This is default. The browser calculates the height and width
length - Defines the height/width in px, cm, etc.
% - Defines the height/width in percent of the containing block
initial - Sets the height/width to its default value
inherit - The height/width will be inherited from its parent value
div {
height: 200px;
width: 50%;
background-color: powderblue;
}
Note: Remember that the height and width properties do not include padding,
borders, or margins! They set the height/width of the area inside the padding,
border, and margin of the element!
Setting max-width
The max-width can be specified in length values, like px, cm, etc., or in percent
(%) of the containing block, or set to none (this is default. Means that there is
no maximum width).
The problem with the <div> above occurs when the browser window is smaller
than the width of the element (500px). The browser then adds a horizontal
scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling of
small windows.
Tip: Drag the browser window to smaller than 500px wide, to see the difference
between the two divs!
Note: If you for some reason use both the width property and the max-
width property on the same element, and the value of the width property is
larger than the max-width property; the max-width property will be used (and
the width property will be ignored).
The CSS box model is essentially a box that wraps around every HTML element.
It consists of: margins, borders, padding, and the actual content. The image
below illustrates the box model:
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define space
between elements.
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
CSS Outline
outline-style
outline-color
outline-width
outline-offset
outline
Note: Outline differs from borders! Unlike border, the outline is drawn outside
the element's border, and may overlap other content. Also, the outline is NOT a
part of the element's dimensions; the element's total width and height is not
affected by the width of the outline.
The outline-style property specifies the style of the outline, and can have one
of the following values:
p.ex1 {
border: 1px solid black;
outline-style: solid;
outline-color: red;
outline-width: thin;
}
p.ex2 {
border: 1px solid black;
outline-style: solid;
outline-color: red;
outline-width: medium;
}
p.ex3 {
border: 1px solid black;
outline-style: solid;
outline-color: red;
outline-width: thick;
}
p.ex4 {
border: 1px solid black;
outline-style: solid;
outline-color: red;
outline-width: 4px;
}
CSS Outline Offset
The outline-offset property adds space between an outline and the
edge/border of an element. The space between an element and its outline is
transparent.
The following example specifies an outline 15px outside the border edge:
p {
margin: 30px;
border: 1px solid black;
outline: 1px solid red;
outline-offset: 15px;
}
CSS Text
Text Color
The color property is used to set the color of the text. The color is specified by:
Look at CSS Color Values for a complete list of possible color values.
The default text color for a page is defined in the body selector.
body {
color: blue;
}
h1 {
color: green;
}
h1 {
background-color: black;
color: white;
}
div {
background-color: blue;
color: white;
}
text-align
text-align-last
direction
unicode-bidi
vertical-align
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
The following example shows center aligned, and left and right aligned text (left
alignment is default if text direction is left-to-right, and right alignment is default
if text direction is right-to-left):
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
div {
text-align: justify;
}
p.a {
text-align-last: right;
}
p.b {
text-align-last: center;
}
p.c {
text-align-last: justify;
}
Text Direction
The direction and unicode-bidi properties can be used to change the text
direction of an element:
p {
direction: rtl;
unicode-bidi: bidi-override;
}
Vertical Alignment
img.a {
vertical-align: baseline;
}
img.b {
vertical-align: text-top;
}
img.c {
vertical-align: text-bottom;
}
img.d {
vertical-align: sub;
}
img.e {
vertical-align: super;
}
text-decoration-line
text-decoration-color
text-decoration-style
text-decoration-thickness
text-decoration
Tip: You can combine more than one value, like overline and underline to
display lines both over and under a text.
h1 {
text-decoration-line: overline;
}
h2 {
text-decoration-line: line-through;
}
h3 {
text-decoration-line: underline;
}
p {
text-decoration-line: overline underline;
}
Note: It is not recommended to underline text that is not a link, as this often
confuses the reader.
h1 {
text-decoration-line: overline;
text-decoration-color: red;
}
h2 {
text-decoration-line: line-through;
text-decoration-color: blue;
}
h3 {
text-decoration-line: underline;
text-decoration-color: green;
}
p {
text-decoration-line: overline underline;
text-decoration-color: purple;
}
text-decoration-line (required)
text-decoration-color (optional)
text-decoration-style (optional)
text-decoration-thickness (optional)
A Small Tip
All links in HTML are underlined by default. Sometimes you see that links are
styled with no underline. The text-decoration: none; is used to remove the
underline from links, like this:
a {
text-decoration: none;
}
Text Transformation
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
Text Spacing
In this chapter you will learn about the following properties:
text-indent
letter-spacing
line-height
word-spacing
white-space
Text Indentation
The text-indent property is used to specify the indentation of the first line of a
text:
p {
text-indent: 50px;
}
Letter Spacing
h1 {
letter-spacing: 5px;
}
h2 {
letter-spacing: -2px;
}
Line Height
The line-height property is used to specify the space between lines:
p.small {
line-height: 0.8;
}
p.big {
line-height: 1.8;
}
Word Spacing
The word-spacing property is used to specify the space between the words in a
text.
p.one {
word-spacing: 10px;
}
p.two {
word-spacing: -2px;
}
White Space
The white-space property specifies how white-space inside an element is
handled.
p {
white-space: nowrap;
}
<html>
<head>
<style>
p {
white-space: nowrap;
}
</style>
</head>
<body>
<h1>Using white-space</h1>
<p>
This is some text that will not wrap.
This is some text that will not wrap.
This is some text that will not wrap.
This is some text that will not wrap.
This is some text that will not wrap.
This is some text that will not wrap.
This is some text that will not wrap.
This is some text that will not wrap.
This is some text that will not wrap.
</p>
<p>Try to remove the white-space property to see the difference!</p>
</body>
</html>
Text Shadow
The text-shadow property adds shadow to text.
In its simplest use, you only specify the horizontal shadow (2px) and the vertical
shadow (2px):
h1 {
text-shadow: 2px 2px 5px red;
}
h1 {
color: white;
text-shadow: 2px 2px 4px #000000;
}
h1 {
text-shadow: 0 0 3px #ff0000, 0 0 5px #0000ff;
}