0% found this document useful (0 votes)
14 views37 pages

Introduction To Cascading Style Sheets (CSS)

The document provides an introduction to Cascading Style Sheets (CSS), explaining its purpose in styling HTML and XML web pages. It covers various methods of incorporating CSS, including inline, internal, and external styles, along with examples of CSS properties such as colors, fonts, borders, and margins. Additionally, it discusses CSS selectors, the cascading order of styles, and the importance of comments in CSS code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views37 pages

Introduction To Cascading Style Sheets (CSS)

The document provides an introduction to Cascading Style Sheets (CSS), explaining its purpose in styling HTML and XML web pages. It covers various methods of incorporating CSS, including inline, internal, and external styles, along with examples of CSS properties such as colors, fonts, borders, and margins. Additionally, it discusses CSS selectors, the cascading order of styles, and the importance of comments in CSS code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Employment through Skill

Sindh Technical Education & Vocational Training Authority (STEVTA)


GOVT: INSTITUTE OF BUSNIESS & COMMERCIAL EDUCATION (G.I.B.C.E), KHAIRPUR
Address: Beside Govt: Comprehensive H.S.S near Therhi Phattak, Khairpur
Phone: 0243-684089 E-mail: gibce.khp@gmail.com

INTRODUCTION TO CASCADING STYLE SHEETS (CSS)


HTML Styles – CSS:
CSS stands for Cascading Style Sheets.
CSS is a styling language used to control the layout and appearance of web pages written HTML
or XML (Extensible Markup Language).
CSS saves a lot of work. It can control the layout of multiple web pages all at once.

What is CSS?
CSS is used to format the layout of a webpage. With CSS, you can control the color, font, the
size of text, the spacing between elements, how elements are positioned and laid out, what
background images or background colors are to be used, different displays for different devices
and screen sizes, and much more!
CSS is a simple, declarative language that:
1. Separates presentation from structures (HTML)
2. Control layout, visual styling, and user experience.
3. Enhances web page aesthetics and usability

Using CSS:
CSS can be added to HTML documents in 3 ways:
Inline - by using the style attribute inside HTML elements
Internal - by using a <style> element in the <head> section
External - by using a <link> element to link to an external CSS file
The most common way to add CSS, is to keep the styles in external CSS files. However, in this
tutorial we will use inline and internal styles, because this is easier to demonstrate, and easier for
you to try it yourself.

Inline CSS:
An inline CSS is used to apply a unique style to a single HTML element.
An inline CSS uses the style attribute of an HTML element.
The following example sets the text color of the <h1> element to blue, and the text color of
the <p>element to red:
Example:
<h1 style="color:blue;">A Blue Heading</h1>
<p style="color:red;">A red paragraph.</p>

Internal CSS:
An internal CSS is used to define a style for a single HTML page.
An internal CSS is defined in the <head> section of an HTML page, within a <style> element.

1
The following example sets the text color of ALL the <h1> elements (on that page) to blue, and
the text color of ALL the <p> elements to red. In addition, the page will be displayed with a
"powderblue" background color:

Example:
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

External CSS:
An external style sheet is used to define the style for many HTML pages.
To use an external style sheet, add a link to it in the <head> section of each HTML page:

Example:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

The external style sheet can be written in any text editor. The file must not contain any HTML
code, and must be saved with a .css extension.

Here is what the "styles.css" file looks like:

2
"styles.css":
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p{
color: red;
}

CSS Colors, Fonts and Sizes:


Here, we will demonstrate some commonly used CSS properties. You will learn more about
them later.

The CSS color property defines the text color to be used.


The CSS font-family property defines the font to be used.
The CSS font-size property defines the text size to be used.

Example:
Use of CSS color, font-family and font-size properties:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
font-family: verdana;
font-size: 300%;
}
p{
color: red;
font-family: courier;
font-size: 160%;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

3
CSS Border:
The CSS border property defines a border around an HTML element.

Example:
Use of CSS border property:
p{
border: 2px solid powderblue;
}

CSS Padding:
The CSS padding property defines a padding (space) between the text and the border.

Example:
Use of CSS border and padding properties:
p{
border: 2px solid powderblue;
padding: 30px;
}

CSS Margin:
The CSS margin property defines a margin (space) outside the border.

Example:
Use of CSS border and margin properties:
p{
border: 2px solid powderblue;
margin: 50px;
}

Link to External CSS:


External style sheets can be referenced with a full URL or with a path relative to the current web
page.

Example:
This example uses a full URL to link to a style sheet:
<link rel="stylesheet" href="https://www.google.com/html/styles.css">

Example:
This example links to a style sheet located in the html folder on the current web site:
<link rel="stylesheet" href="/html/styles.css">

Example:
This example links to a style sheet located in the same folder as the current page:
<link rel="stylesheet" href="styles.css">

4
CSS Syntax:
A CSS rule 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.

Example:
In this example all <p> elements will be center-aligned, with a red text color:

p{
color: red;
text-align: center;
}

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 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.

5
The CSS element Selector:
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;
}

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":
#para1 {
text-align: center;
color: red;
}

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:
.center {
text-align: center;
color: red;
}

Example:
In this example only <p> elements with class="center" will be red and center-aligned:

p.center {
text-align: center;
color: red;
}

Example:
In this example the <p> element will be styled according to class="center" and to class="large":
<p class="center large">This paragraph refers to two classes.</p>

6
The CSS Universal Selector:
The universal selector (*) selects all HTML elements on the page.

Example:
The CSS rule below will affect every HTML element on the page:
*{
text-align: center;
color: blue;
}

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;
}

It 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;
}

7
How To Add CSS:
When a browser reads a style sheet, it will format the HTML document according to the
information in the style sheet.

Three Ways to Insert CSS:


There are three ways of inserting a style sheet:
 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.
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>
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.
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 unit (px):
Incorrect (space): margin-left: 20 px;
Correct (no space): margin-left: 20px;

8
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>

<h1 style="color:blue;text-align:center;">This is a heading</h1>


<p style="color:red;">This is a paragraph.</p>

</body>
</html>

9
Multiple Style Sheets:
If some properties have been defined for the same selector (element) in different style sheets, the
value from the last read style sheet will be used.
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>

Example:
However, if the internal style is defined before the link to the external style sheet, the <h1>
elements will be "navy":
<head>
<style>
h1 {
color: orange;
}
</style>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</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:
 Inline style (inside an HTML element)
 External and internal style sheets (in the head section)
 Browser default

So, an inline style has the highest priority, and will override external and internal styles and browser
defaults.

10
CSS Comments:
CSS comments are not displayed in the browser, but they can help document your source code.
Comments are used to explain the code, and may help when you edit the source code at a later
date.
Comments are ignored by browsers.

A CSS comment is placed inside the <style> element, and starts with /* and ends with */:

Example:
/* This is a single-line comment */
p{
color: red;
}
You can add comments wherever you want in the code:

Example:
p{
color: red; /* Set text color to red */
}
Even in the middle of a code line:

Example:
p{
color: /*red*/blue;
}
Comments can also span multiple lines:

Example:
/* This is
a multi-line
comment */

p{
color: red;
}

11
HTML and CSS Comments:
From the HTML tutorial, you learned that you can add comments to your HTML source by using
the <!----…….----> syntax.
In the following example, we use a combination of HTML and CSS comments:
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red; /* Set text color to red */
}

</style>
</head>
<body>

<h2>My Heading</h2>

<!-- These paragraphs will be red -->


<p>Hello World!</p>
<p>This paragraph is styled with CSS.</p>
<p>CSS comments are not shown in the output.</p>

</body>
</html>

12
CSS Colors:
Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values.

CSS Color Names:


In CSS, a color can be specified by using a predefined color name:

CSS/HTML supports 140 standard color names.

CSS Background Color:


You can set the background color for HTML elements:

Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod
tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis

Example:
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>

CSS Text Color:


You can set the color of text:

Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod
tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut
aliquip ex ea commodo consequat.

Example:
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>

13
CSS Border Color:
You can set the color of borders:

Hello World
Hello World
Hello World
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>

CSS Borders:
The CSS border properties allow you to specify the style, width, and color of an element's
border.

I have borders on all sides.

I have a red bottom border.

I have rounded borders.

CSS Border Style:


The border-style property specifies what kind of border to display.

The following values are allowed:


dotted - Defines a dotted border
dashed - Defines a dashed border
solid - Defines a solid border
double - Defines a double border
groove - Defines a 3D grooved border. The effect depends on the border-color value
ridge - Defines a 3D ridged border. The effect depends on the border-color value
inset - Defines a 3D inset border. The effect depends on the border-color value
outset - Defines a 3D outset border. The effect depends on the border-color value
none - Defines no border
hidden - Defines a hidden border

The border-style property can have from one to four values (for the top border, right border,
bottom border, and the left border).

14
Example:
Demonstration of the different border styles:
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}

Result:

CSS Border Width:


The border-width property specifies the width of the four borders.
The width can be set as a specific size (in px, pt, cm, em, etc) or by using one of the three pre-
defined values: thin, medium, or thick:

Example:
Demonstration of the different border widths:

p.one {
border-style: solid;
border-width: 5px;
}

15
p.two {
border-style: solid;
border-width: medium;
}

p.three {
border-style: dotted;
border-width: 2px;
}

p.four {
border-style: dotted;
border-width: thick;
}

Specific Side Widths:


The border-width property can have from one to four values (for the top border, right border,
bottom border, and the left border):

Example:
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 */
}

16
CSS Border Color:
The border-color property is used to set the color of the four borders.

The color can be set by:


name - specify a color name, like "red"
HEX - specify a HEX value, like "#ff0000"
RGB - specify a RGB value, like "rgb(255,0,0)"
HSL - specify a HSL value, like "hsl(0, 100%, 50%)"
transparent

Example:
Demonstration of the different border colors:
p.one {
border-style: solid;
border-color: red;
}

p.two {
border-style: solid;
border-color: green;
}

p.three {
border-style: dotted;
border-color: blue;
}

Result:

Specific Side Colors:


The border-color property can have from one to four values (for the top border, right border,
bottom border, and the left border).

Example:
p.one {
border-style: solid;
border-color: red green blue yellow; /* red top, green right, blue bottom and yellow left */
}

CSS Box Model:


17
All HTML elements can be considered as boxes.
In CSS, the term "box model" is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists of:
content, padding, borders and margins. The image below illustrates the box model:

Explanation of the different parts:


 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.

Example:
Demonstration of the box model:

width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;

CSS Outline:
An outline is a line drawn outside the element's border.

18
An outline is a line that is drawn around elements, OUTSIDE the borders, to make the element
"stand out".

CSS has the following outline properties:


 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.

CSS Outline Style:


The outline-style property specifies the style of the outline, and can have one of the following
values:
dotted - Defines a dotted outline
dashed - Defines a dashed outline
solid - Defines a solid outline
double - Defines a double outline
groove - Defines a 3D grooved outline
ridge - Defines a 3D ridged outline
inset - Defines a 3D inset outline
outset - Defines a 3D outset outline
none - Defines no outline
hidden - Defines a hidden outline

The following example shows the different outline-style values:

Example:
Demonstration of the different outline styles:

19
p.dotted {outline-style: dotted;}
p.dashed {outline-style: dashed;}
p.solid {outline-style: solid;}
p.double {outline-style: double;}
p.groove {outline-style: groove;}
p.ridge {outline-style: ridge;}
p.inset {outline-style: inset;}
p.outset {outline-style: outset;}

Result:

CSS Outline Width:


The outline-width property specifies the width of the outline, and can have one of the following
values:
 thin (typically 1px)
 medium (typically 3px)
20
 thick (typically 5px)
 A specific size (in px, pt, cm, em, etc)
The following example shows some outlines with different widths:

Example:
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:

21
Example:
p{
margin: 30px;
border: 1px solid black;
outline: 1px solid red;
outline-offset: 15px;
}
The following example shows that the space between an element and its outline is transparent:

Example:
p{
margin: 30px;
background: yellow;
border: 1px solid black;
outline: 1px solid red;
outline-offset: 15px;
}

Text Alignment and Text Direction:


In this chapter you will learn about the following properties:
 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.
A text can be left or right aligned, centered, or justified.
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):

Example:
h1 {
text-align: center;
}

h2 {

22
text-align: left;
}

h3 {
text-align: right;
}

When the text-align property is set to "justify", each line is stretched so that every line has equal
width, and the left and right margins are straight (like in magazines and newspapers):

Text Align Last:


The text-align-last property specifies how to align the last line of a text.

Example:
Align the last line of text in three <p> elements:
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:

Example:
p{
direction: rtl;
unicode-bidi: bidi-override;
}

Vertical Alignment:
The vertical-align property sets the vertical alignment of an element.

Example:
Set the vertical alignment of an image in a text:

23
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;
}

CSS Text Decoration:


In this chapter you will learn about the following properties:
 text-decoration-line
 text-decoration-color
 text-decoration-style

24
 text-decoration-thickness
 text-decoration

Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration: overline;
}

h2 {
text-decoration: line-through;
}

h3 {
text-decoration: underline;
}

p.ex {
text-decoration: overline underline;
}
</style>
</head>
<body>

<h1>Overline text decoration</h1>


<h2>Line-through text decoration</h2>
<h3>Underline text decoration</h3>
<p class="ex">Overline and underline text decoration.</p>

<p><strong>Note:</strong> It is not recommended to underline text that is not a link, as this


often confuses the reader.</p>

</body>
</html>

Add a Decoration Line to Text:


The text-decoration-line property is used to add a decoration line to text.

Tip: You can combine more than one value, like over line and underline to display lines both
over and under a text.

25
Specify a Color for the Decoration Line

The text-decoration-color property is used to set the color of the decoration line.

Example:
<!DOCTYPE html>
<html>
<head>
<style>
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;
}
</style>
</head>
<body>

<h1>Overline text decoration</h1>


<h2>Line-through text decoration</h2>
<h3>Underline text decoration</h3>
<p>Overline and underline text decoration.</p>

</body>
</html>

Specify a Style for the Decoration Line:


The text-decoration-style property is used to set the style of the decoration line.
Example:
<!DOCTYPE html>
<html>
<head>

26
<style>
h1 {
text-decoration-line: underline;
text-decoration-style: solid; /* this is default */
}

h2 {
text-decoration-line: underline;
text-decoration-style: double;
}
h3 {
text-decoration-line: underline;
text-decoration-style: dotted;
}
p.ex1 {
text-decoration-line: underline;
text-decoration-style: dashed;
}
p.ex2 {
text-decoration-line: underline;
text-decoration-style: wavy;
}
p.ex3 {
text-decoration-line: underline;
text-decoration-color: red;
text-decoration-style: wavy;
}
</style>
</head>
<body>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<p class="ex1">A paragraph.</p>
<p class="ex2">Another paragraph.</p>
<p class="ex3">Another paragraph.</p>
</body>
</html>

Specify the Thickness for the Decoration Line:


The text-decoration-thickness property is used to set the thickness of the decoration line.

Example:
<!DOCTYPE html>
<html>

27
<head>
<style>
h1 {
text-decoration-line: underline;
text-decoration-thickness: auto; /* this is default */
}

h2 {
text-decoration-line: underline;
text-decoration-thickness: 5px;
}

h3 {
text-decoration-line: underline;
text-decoration-thickness: 25%;
}

p{
text-decoration-line: underline;
text-decoration-color: red;
text-decoration-style: double;
text-decoration-thickness: 5px;
}
</style>
</head>
<body>

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<p>A paragraph.</p>

</body>
</html>

Text Transformation:
The text-transform property is used to specify uppercase and lowercase letters in a text.

It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter
of each word:

Example:
28
<!DOCTYPE html>
<html>
<head>
<style>
p.uppercase {
text-transform: uppercase;
}

p.lowercase {
text-transform: lowercase;
}

p.capitalize {
text-transform: capitalize;
}
</style>
</head>
<body>

<h1>Using the text-transform property</h1>

<p class="uppercase">This text is transformed to uppercase.</p>


<p class="lowercase">This text is transformed to lowercase.</p>
<p class="capitalize">This text is capitalized.</p>

</body>
</html>

CSS Text Spacing:


In this chapter you will learn about the following properties:
 text-indent
 letter-spacing
 line-height
 word-spacing
29
 white-space

Text Indentation:
The text-indent property is used to specify the indentation of the first line of a text:

Example:
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-indent: 50px;
}
</style>
</head>
<body>

<h1>Using text-indent</h1>

<p>In my younger and more vulnerable years my father gave me some advice that I've been
turning over in my mind ever since. 'Whenever you feel like criticizing anyone,' he told me, 'just
remember that all the people in this world haven't had the advantages that you've had.'</p>

</body>
</html>

Letter Spacing:
The letter-spacing property is used to specify the space between the characters in a text.
The following example demonstrates how to increase or decrease the space between characters:
Example:
<!DOCTYPE html>
<html>
<head>
<style>

30
h2 {
letter-spacing: 5px;
}

h3 {
letter-spacing: -2px;
}
</style>
</head>
<body>
<h1>Using letter-spacing</h1>
<h2>This is heading 1</h2>
<h3>This is heading 2</h3>
</body>
</html>

Line Height:
The line-height property is used to specify the space between lines:
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p.small {
line-height: 0.7;
}

p.big {
line-height: 1.8;
}
</style>
</head>
<body>

<h1>Using line-height</h1>

<p>
This is a paragraph with a standard line-height.<br>
The default line height in most browsers is about 110% to 120%.<br>
</p>

<p class="small">
This is a paragraph with a smaller line-height.<br>
This is a paragraph with a smaller line-height.<br>
</p>

31
<p class="big">
This is a paragraph with a bigger line-height.<br>
This is a paragraph with a bigger line-height.<br>
</p>

</body>
</html>

Word Spacing:
The word-spacing property is used to specify the space between the words in a text.
The following example demonstrates how to increase or decrease the space between words:
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
word-spacing: 10px;
}

p.two {
word-spacing: -2px;
}
</style>
</head>
<body>

<h1>Using word-spacing</h1>
<p>This is a paragraph with normal word spacing.</p>
<p class="one">This is a paragraph with larger word spacing.</p>
<p class="two">This is a paragraph with smaller word spacing.</p>
</body>
</html>

White Space:
The white-space property specifies how white-space inside an element is handled.
This example demonstrates how to disable text wrapping inside an element:
Example:
<!DOCTYPE html>
<html>
<head>
<style>

32
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>

CSS 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):
Text-shadow effect!
Example:
<!DOCTYPE html>
<html>

33
<head>
<style>
h1 {
text-shadow: 2px 2px;
}
</style>
</head>
<body>

<h1>Text-shadow effect!</h1>

</body>
</html>

Next, add a color (red) to the shadow:


Text shadow effect!

Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-shadow: 2px 2px red;
}
</style>
</head>
<body>

<h1>Text-shadow effect!</h1>

</body>
</html>

Then, add a blur effect (5px) to the shadow:

Text shadow effect!

Example:
<!DOCTYPE html>
<html>
<head>
<style>

34
h1 {
text-shadow: 2px 2px 5px red;
}
</style>
</head>
<body>

<h1>Text-shadow effect!</h1>

</body>
</html>

More Text Shadow Examples:


Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: white;
text-shadow: 2px 2px 4px #000000;
}
</style>
</head>
<body>

<h1>Text-shadow effect!</h1>

</body>
</html>

Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-shadow: 0 0 3px #FF0000;
}
</style>

35
</head>
<body>
<h1>Text-shadow with red neon glow!</h1>
</body>
</html>

Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-shadow: 0 0 3px #FF0000, 0 0 5px #0000FF;
}
</style>
</head>
<body>
<h1>Text-shadow with red and blue neon glow!</h1>
</body>
</html>

Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: white;
text-shadow: 1px 1px 2px black, 0 0 25px blue, 0 0 5px darkblue;
}
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
</body>
</html>

CSS Fonts

36
37

You might also like