Professional Documents
Culture Documents
SYLLABUS
UNIT – III
Cascading Style Sheets: Introducing CSS, Where you can Add CSS
Rules. CSS Properties: Controlling Text, Text Formatting, Text
Pseudo Classes, Selectors, Lengths, Introducing the Box Model.
More Cascading Style Sheets: Links, Lists, Tables, Outlines, The
:focus and :activate Pseudo classes Generated Content,
Miscellaneous Properties, Additional Rules, Positioning and Layout
wit, Page Layout CSS , Design Issues.
3.1 Introduction of CSS
CSS stands for Cascading Style Sheets. It is a simple design
language intended to simplify the process of making web pages
presentable. CSS handles the look and feel part of a web page.
Using CSS, you can control the color of the text, the style of fonts,
the spacing between paragraphs, how columns are sized and laid
out, what background images or colors are used, as well as a
variety of other effects.
CSS works with HTML and other Markup Languages (such as
XHTML and XML) to control the way the content is presented.
Cascading Style Sheets is a means to separate the appearance of a
webpage from the content of a webpage.
3.1.1 Definition
Cascading Style Sheets (CSS) is a simple mechanism used to format
the layout of Web Pages and adding style (e.g., fonts, colors,
spacing...) to web documents that previously could only be defined
in a page's HTML. CSS describes how HTML elements are to be
displayed on screen, paper, or in other media. It can control the
layout of multiple web pages all at once.
3.1.2 Advantages
The advantages of CSS are:
CSS saves time - You can write CSS once and then reuse the
same sheet in multiple HTML pages.
Pages load faster – Increases Download Speed
Easy maintenance - To make a global change, all the elements in
all the web pages will be updated automatically.
Superior styles to HTML – It is better look to your HTML page in
comparison to HTML attributes.
Multiple Device Compatibility - Style sheets allow content to be
optimized for more than one type of device.
Global web standards - Now HTML attributes are being
deprecated and it is being recommended to use CSS
3.1.3 What is the “Cascade” part of CSS?
The cascade part of CSS means that more than one style sheet can
be attached to a document, and all of them can influence the
presentation. For example, a designer can have a global style sheet
for the whole site, but a local one for say, controlling the link color
and background of a specific page. Or, a user can use own style
sheet if s/he has problems seeing the page, or if s/he just prefers a
certain look.
3.2 CSS Syntax
A CSS style rule is made of three parts:
1. Selector: A selector is an HTML tag at which a style will be
applied. This could be any tag like <h1>, <p> or <table> etc.
2. Property: A property is a type of attribute of HTML tag. Put
simply, all the HTML attributes are converted into CSS properties.
They could be color, border, bgcolor etc.
3. Value: Values are assigned to properties. For example, color
property can have the value either red or #F1F1F1 etc.
The format or syntax of CSS is:
Selector {property:
If the value of a property is more than one word, put quotes around
that value: body { font-family: "sans serif"; } If you wish to specify
more than one property, you must use a semi-colon to separate
each property. This rule defines a paragraph that will have blue text
that is centered.
You can group selectors. Separate each selector with a comma. The
example below groups headers 1, 2, and 3 and makes them all
yellow. h1, h2, h3 { color: yellow}
CSS Pseudo-classes
Syntax
selector:pseudo-class {
property: value;
}
CSS Selectors:
CSS selectors are used to "find" (or select) the HTML elements you
want to style.
Example
Here, all <p> elements on the page will be center-aligned, with a red
text color:
p{
text-align: center;
color: red;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: red;
}
</style>
</head>
<body>
<p>Every paragraph will be affected by the style.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
OUTPUT:
Me too!
And me!
The CSS id Selector
Example
The CSS rule below will be applied to the HTML element with
id="para1":
#para1 {
text-align: center;
color: red;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>
OUTPUT:
Hello World!
Example
.center {
text-align: center;
color: red;
}
You can also specify that only specific HTML elements should be
affected by a class.
Example
p.center {
text-align: center;
color: red;
}
Example
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">Red and center-aligned heading</h1>
<p class="center">Red andcenter-aligned paragraph.</p>
</body>
</html>
OUTPUT:
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;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
*{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1>Hello world!</h1>
<p>Every element on the page will be affected by the style.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
OUTPUT:
Hello world!
Me too!
And me!
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;
}
Example
In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body>
</html>
OUTPUT:
Hello World!
Smaller heading!
This is a paragraph
• 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!
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>
OUTPUT:
This is a heading
This is a heading
This is a paragraph.
This is a paragraph.
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;
}
Note: Do not add a space between the property value and the unit
(such as margin-left: 20 px;). The correct way is: 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>
OUTPUT:
This is a heading
This is a paragraph.
Inline CSS
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>
OUTPUT:
This is a heading
This is a paragraph.
Text Formatting:
CSS Text
CSS has a lot of properties for formatting text.
TEXT FORMATTING
Text Color
The color property is used to set the color of the text. The color is
specified by:
The default text color for a page is defined in the body selector.
Example
body {
color: blue;
}
h1 {
color: green;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: blue;
}
h1 {
color: green;
}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<p>This is an ordinary paragraph. Notice that this text is blue. The
default text color for a page is defined in the body selector.</p>
<p>Another paragraph.</p>
</body>
</html>
OUTPUT:
This is heading 1
Another paragraph.
Example
body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: white;
}
div {
background-color: blue;
color: white;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: white;
}
div {
background-color: blue;
color: white;
}
</style>
</head>
<body>
<h1>This is a Heading</h1>
<p>This page has a grey background color and a blue text.</p>
<div>This is a div.</div>
</body>
OUTPUT:
This is a Heading
This is a div.
Text Alignment:
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 {
text-align: left;
}
h3 {
text-align: right;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
</style>
</head>
<body>
<h1>Heading 1 (center)</h1>
<h2>Heading 2 (left)</h2>
<h3>Heading 3 (right)</h3>
<p>The three headings above are aligned center, left and right.</p>
</body>
</html>
OUTPUT:
Heading 1 (center)
Heading 2 (left)
Heading 3 (right)
The three headings above are aligned center, left and right.
Text Direction:
Example
p{
direction: rtl;
unicode-bidi: bidi-override;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
p.ex1 {
direction: rtl;
unicode-bidi: bidi-override;
}
</style>
</head>
<body>
<p>This is the default text direction.</p>
<p class="ex1">This is right-to-left text direction.</p>
</body>
</html>
OUTPUT:
This is the default text direction.
This is right-to-left text direction.
Vertical Alignment:
Example
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:
Example
a{
text-decoration: none;
}
Example
h2 {
text-decoration: overline;
}
h3 {
text-decoration: line-through;
}
h4 {
text-decoration: underline;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
h2 {
text-decoration: overline;
}
h3 {
text-decoration: line-through;
}
h4 {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Some different text decorations</h1>
<h2>Overline text decoration</h2>
<h3>Line-through text decoration</h3>
<h4>Underline text decoration</h4>
<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>
OUTPUT:
Text Transformation
Example
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
PROGRAM:
<!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>
OUTPUT:
Text Indentation
Example
p{
text-indent: 50px;
}
Letter Spacing
h2 {
letter-spacing: -2px;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
p{
white-space: nowrap;
</style>
</head>
<body>
<h1>Using white-space</h1>
<p>
</p>
</body>
</html>
OUTPUT:
Using white-space
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.
Text Shadow
In its simplest use, you only specify the horizontal shadow (2px)
and the vertical shadow (2px):
Text shadow effect!
Example
h1 {
text-shadow: 2px 2px;
}
Example
h1 {
text-shadow: 2px 2px red;
}
Example
h1 {
text-shadow: 2px 2px 5px red;
}
h1 {
color: white;
text-shadow: 2px 2px 4px #000000;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: red;
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
</body>
</html>
OUTPUT:
Text-shadow effect!
CSS Box Model:
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: margins, borders, padding, and the
actual content. The image below illustrates the box model:
Explanation of the different parts:
Example
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
padding: 50px;
margin: 20px;
</style>
</head>
<body>
<p>The CSS box model is essentially a box that wraps around every
HTML element. It consists of: borders, padding, margins, and the
actual content.</p>
</body>
</html>
OUTPUT:
The CSS box model is essentially a box that wraps around every
HTML element. It consists of: borders, padding, margins, and the
actual content.
Example
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
</style>
</head>
<body>
<h2>Calculate the total width:</h2>
<imgsrc="klematis4_big.jpg" width="350" height="263"
alt="Klematis">
<div>The picture above is 350px wide. The total width of this
element is also 350px.</div>
</body>
</html>
OUTPUT:
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
➢ Links,
➢ Lists,
➢ Tables,
➢ Outlines,
Links:
Styling Links
Links can be styled with any CSS property (e.g. color, font-
family, background, etc.).
Example
a{
color: hotpink;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
a{
color: hotpink;
}
</style>
</head>
<body>
<h2>Style a link with a color</h2>
<p><b><a href="default.asp" target="_blank">This is a
link</a></b></p>
</body>
</html>
OUTPUT:
Style a link with a color
This is a link
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* selected link */
a:active {
color: blue;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
a:hover {
color: hotpink;
/* selected link */
a:active {
color: blue;
</style>
</head>
<body>
</html>
OUTPUT:
Styling a link depending on state
This is a link
Note: a:hover MUST come after a:link and a:visited in the CSS
definition in order to be effective.
Text Decoration
Example
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: underline;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
a:link {
text-decoration: none;
a:visited {
text-decoration: none;
a:hover {
text-decoration: underline;
a:active {
text-decoration: underline;
}
</style>
</head>
<body
</body>
</html>
OUTPUT:
This is a link
Note: a:hover MUST come after a:link and a:visited in the CSS
definition in order to be effective.
Example
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
}
a:hover {
background-color: lightgreen;
}
a:active {
background-color: hotpink;
}
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
a:hover {
background-color: lightgreen;
a:active {
background-color: hotpink;
</style>
</head>
<body>
</body>
</html>
OUTPUT:
This is a link
Note: a:hover MUST come after a:link and a:visited in the CSS
definition in order to be effective.
Link Buttons
Example
a:link, a:visited {
background-color: #f44336;
color: white;
padding: 14px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a:hover, a:active {
background-color: red;
}
CSS List:
Unordered Lists:
o Coffee
o Tea
o Coca Cola
▪ Coffee
▪ Tea
▪ Coca Cola
Ordered Lists:
1. Coffee
2. Tea
3. Coca Cola
I. Coffee
II. Tea
III. Coca Cola
• unordered lists (<ul>) - the list items are marked with bullets
• ordered lists (<ol>) - the list items are marked with numbers or
letters
Example
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
Note: Some of the values are for unordered lists, and some for
ordered lists.
An Image as The List Item Marker
Example
ul {
list-style-image: url('sqpurple.gif');
}
Example
ul.a {
list-style-position: outside;
}
ul.b {
list-style-position: inside;
}
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
Example
ul {
list-style: square inside url("sqpurple.gif");
}
We can also style lists with colors, to make them look a little more
interesting.
Anything added to the <ol> or <ul> tag, affects the entire list, while
properties added to the <li> tag will affect the individual list items:
Example
ol {
background: #ff9999;
padding: 20px;
}
ul {
background: #3399ff;
padding: 20px;
}
ol li {
background: #ffe5e5;
padding: 5px;
margin-left: 35px;
}
ul li {
background: #cce5ff;
margin: 5px;
}
Result:
1. Coffee
2. Tea
3. Coco cola
• Coffee
• Tea
• Coca Cola
Table Borders
The example below specifies a black border for <table>, <th>, and
<td> elements:
Firstname Lastname
Peter Griffin
Lois Griffin
Example
table, th, td {
border: 1px solid black;
}
Full-Width Table
The table above might seem small in some cases. If you need a table
that should span the entire screen (full-width), add width: 100% to
the <table> element:
Firstname Lastname
Peter Griffin
Lois Griffin
Example
table {
width: 100%;
}
Double Borders
Notice that the table in the examples above have double borders.
This is because both the table and the <th> and <td> elements have
separate borders.
Firstname Lastname
Peter Griffin
Lois Griffin
Example
table {
border-collapse: collapse;
}
Firstname Lastname
Peter Griffin
Lois Griffin
CSS Outline
An outline is a line drawn outside the element's border.
CSS Outline
• outline-style
• outline-color
• outline-width
• outline-offset
• outline
The outline-style property specifies the style of the outline, and can
have one of the following values:
Example
Result:
Example
input:focus {
background-color: yellow;
}
Definition and Usage
The :focus selector is used to select the element that has focus.
Version: CSS2
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
input:focus {
background-color: yellow;
}
</style>
</head>
<body>
<p>Click inside the text fields to see a yellow background:</p>
<form>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
</form>
</body>
</html>
OUTPUT:
First name:
Last name:
Example
a:active {
background-color: yellow;
}
The :active selector is used to select and style the active link.
Tip: The :active selector can be used on all elements, not only links.
Tip: Use the :link selector to style links to unvisited pages,
the :visited selector to style links to visited pages, and
the :hover selector to style links when you mouse over them.
Note: :active MUST come after :hover (if present) in the CSS
definition in order to be effective!
PROGRAM:
<!DOCTYPE html>
<html>
<head>
<style>
a:active {
background-color: yellow;
}
</style>
</head>
<body>
<ahref="https://www.w3schools.com">w3schools.com</a>
<ahref="http://www.wikipedia.org">wikipedia.org</a>
<p><b>Note:</b> The :active selector styles the active link.</p>
</body>
</html>
OUTPUT:
w3schools.com wikipedia.org
CSS Miscellaneous:
In this tutorial we'll learn about few more interesting CSS features.
Resizing Elements
You can make an element resizable horizontally, vertically or on
both directions with the CSS3 resize property. However, this
property is typically used to remove the default resizable behavior
form the <textarea> form control.
Example
p, div, textarea{
width: 300px;
min-height: 100px;
overflow: auto;
border: 1px solid black;
}
p{
resize: horizontal;
}
div{
resize: both;
}
textarea{
resize: none;
}
Example
p, div{
margin: 50px;
height: 100px;
background: #000;
outline: 2px solid red;
}
p{
outline-offset: 10px;
}
div{
outline-offset: -15px;
}
CSS Layout - The position Property
• static
• relative
• fixed
• absolute
• sticky
Elements are then positioned using the top, bottom, left, and right
properties. However, these properties will not work unless
the position property is set first. They also work differently
depending on the position value.
position: static;
Static positioned elements are not affected by the top, bottom, left,
and right properties.
Example
div.static {
position: static;
border: 3px solid #73AD21;
}
position: relative;
Example
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
position: fixed;
A fixed element does not leave a gap in the page where it would
normally have been located.
Notice the fixed element in the lower-right corner of the page. Here
is the CSS that is used:
Example
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
This <div> element has position: fixed;
position: absolute;
Example
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
position: sticky;
In this example, the sticky element sticks to the top of the page
(top: 0), when you reach its scroll position.
Example
div.sticky {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;}
Example
Bottom Left
Top Left
Top Right
Bottom Right
Centered
CSS Website Layout
Website Layout
Header
Navigation Menu
Footer
Header
A header is usually located at the top of the website (or right below
a top navigation menu). It often contains a logo or the website
name:
Example
.header {
background-color: #F1F1F1;
text-align: center;
padding: 20px;
}
PROGRAM:
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS Website Layout</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1">
<style>
body {
margin: 0;
}
/* Style the header */
.header {
background-color: #f1f1f1;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="header">
<h1>Header</h1>
</div>
</body>
</html>
Output:
Header
Design Issues: