Introduction, Using Styles, Simple Examples, Your Own Styles, Properties and Values in Styles, Style Sheet, Formatting Blocks of Information, Layers
Introduction, Using Styles, Simple Examples, Your Own Styles, Properties and Values in Styles, Style Sheet, Formatting Blocks of Information, Layers
Introduction, Using Styles, Simple Examples, Your Own Styles, Properties and Values in Styles, Style Sheet, Formatting Blocks of Information, Layers
UNIT – II
Cascading Style Sheets
Introduction, using Styles, simple examples, your own styles, properties and values in styles, style sheet,
formatting blocks of information, layers.
……………………………………………………………………………………………………………………………
1. Introduction
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
External style sheets are stored in CSS files
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.
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
Page 1
I BSC VI SEM Web Technologies
Inline CSS
For Inline CSS every style content is in HTML elements. It is used for a limited section. Whenever our requirements are very small we
can use inline CSS.
It will affect only single elements. In HTML we require that various HTML tag's views are different so then we use inline
Cascading Style Sheets. There are disadvantage of inline Cascading Style Sheets. It must be specified on every HTML tag. There is
very much time consumed by that and it is not the best practice for a good programmer and the code will be quite large and very
complex.
Inline CSS examples are given below:
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;">This is a Blue Heading</h1>
</body>
</html>
Internal CSS
In internal CSS the style of CSS is specified in the <head> section. This is internal CSS, it affects all the elements in the body
section. Internal CSS is used in the condition when we want a style to be used in the complete HTML body. For that we can use
style in the head tag.
This style performs an action in the entire HTML body.
<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
In External CSS we create a .css file and use it in our HTML page as per our requirements. Generally external Cascading Style Sheets
are used whenever we have many of HTML attributes and we can use them as required; there is no need to rewrite the CSS style again
and again in a complete body of HTML that inherits the property of the CSS file.
Example:
Page 2
I BSC VI SEM Web Technologies
styles.css
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p{
color: red;
HTML Program
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
4. Defining your own styles
Styles are defined by simple rules. A style can contain as many rules as you want and with processing HTML.
1. Cascading styles:
Conventionally styles are cascaded (overridden). This means that you do not have to use just a single set of rules inside a
document, you can import as many styles as you like.
2. Style Rule:
A style rule has two parts: The selector and a set of declarations. The selector is used to create a link between the rule and the HTML
tag.
The declaration part has two parts: a property and a value. Selectors can be placed into classes so that a tag can be formatted in
variety of ways. Declarations must be separated using colons and terminated using semicolon.
3. Classes
A class is defined a set of styles which can be applied as user choice. Choices can be applied to a single type of element or may be
anonymous and hence applicable to any element.
If you want to apply a style to a particular element use the following syntax .
Selector.classname{property: value; property: value}
<selector class=”classname”>
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be red and center-aligned.</p>
</body>
</html>
4. Anonymous classes
Sometimes you want to apply a piece of formatting too many different elements within a page but not necessarily to the entire page.
Such classes are called anonymous classes. It has the following syntax
Page 3
I BSC VI SEM Web Technologies
.classname
{
Property: value;
Property: value;
}
<selector1 class=”classname”>..........................<selector1>
<selector2 class=”classname”>...........................<selector2>
Example:
<!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 and center-aligned paragraph.</p>
</body>
</html>
Including style sheets
We can include style sheets in our HTML page in two way (i) by linking (ii) by importing
(i) By linking
We can link our .css file in the head section of a HTML page using the following syntax
<link rel=”style sheet” href=”URL” type=”text/css” media=”screen”>
The href is hyperlink to your style sheet. rel tells to the browser what type of link you’re using.
type tells to the browser what type of document you’re including, the ‘type’ statement gives the relevant MIME type.
Finally it is useful, although not compulsory, to tell the browser how the document will be used. HTML Specifies a Varity of ways of
using a document a document, including screen viewing, printing and as a presentations. Use the ‘media’ attribute to describe the
type of use.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" media=”screen”>
<style>
h1 {
color: orange;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>The style of this document is a combination of an external stylesheet, and internal style</p>
</body></html>
(ii) By importing
[<style type=”text/css”>]
<!--@import url(url); --> </style>
These lines are both needed if you intended to use more than one style sheet. The first sheet is included as if it were the only one; any
further style sheets have to be imported.
Example:
<link rel="stylesheet" type="text/css" href="mystyle.css" media=”screen”>
<style type=”text/css”>
Page 4
I BSC VI SEM Web Technologies
Page 5
I BSC VI SEM Web Technologies
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
Example:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 320px; padding:
10px; border: 5px solid
Page 6
I BSC VI SEM Web Technologies
gray; margin: 0;
}
</style>
</head>
<body>
<h2>Calculate the total width:</h2>
<img src="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>
v) Units and URLs
a) lengths
These can be either absolute or relative. A relative length can be either positive or negative, which is indicated by preceding the
value with an optional + or –
Relative units that can be used are: em, ex, px Allowable
absolute units are: in, cm, mm, pt, pc
b) URLs can be used in style sheets just as they can in HTML documents. The format of the URL reference is url(location)
6. Formatting Blocks of Information
A webpage may consist of more than one block. We can format all the blocks with single style or separate style for each and every
block. This can be achieved in three ways
1. Using style class
2. Using <div> tag
3. Using <span> tag
1. Using class
A class is defined a set of styles which can be applied as user choice. Choices can be applied to a single type of element or may be
anonymous and hence applicable to any element.
If you want to apply a style to a particular element use the following syntax Selector.classname{property: value;
property: value}
<selector class=”classname”>
<!DOCTYPE html>
<html>
<head>
<style> p.a
{
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="a">This heading will not be affected</h1>
<p class="a">This paragraph will be red and center-aligned.</p>
</body>
</html>
Anonymous classes
Sometimes you want to apply a piece of formatting too many different elements within a page but not necessarily to the entire page.
Instead of defining styles to individual elements we are creating a style class that can be applicable any number of tags in your HTML
document. Such classes are called anonymous classes. It has the following syntax
.classname
{
Property: value;
Property: value;
}
<selector1 class=”classname”>..........................<selector1>
<selector2 class=”classname”>...........................<selector2>
Example:
<!DOCTYPE html>
Page 7
I BSC VI SEM Web Technologies
<html>
<head>
<style>
.a {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="a">Red and center-aligned heading</h1>
<p class="a">Red and center-aligned paragraph.</p>
</body>
</html>
2. Using <div> tag
An element in an HTML document is either a block element or an inline element. A block would be something like a
paragraph, while an inline might be something like text, a figure or an individual character i.e., part of block. Each of these can be
manipulated separately, rather than applying the formatting to the element itself, a <div> ….. </div> pair of tags are wrapped around
the elements. Any formatting that needs adding is placed inside the <div> tag. The <div> tag has the following syntax:
<div class=”any element”>
<p> ……. </p>
<p>.................</p>
<hr>
</div>
Example:
<html>
<head>
<title> div tag css demo </title>
<head>
<style type=”text / css”>
.a
{
color: white;
background-color: 009900;
margin: 2px;
font-size: 25px;
}
</style>
</head>
<body>
<div class “a”>
<h3> this is heading inside division </h3>
<p> The div tag is known as Division tag. The Div tag is used in HTML to make divisions of content in the web page like (text,
images, header, footer, navigation bar etc).</p>
</div>
</body>
</html>
3. Using <span> tag:
The <span> tag has very similar properties to the <div> tag. If you want to group text
without using elements use a <span> tag. It has the following syntax
<span [id=”…..”][class=”…..” | style=”…..”]>……. </span>
What is the difference between <span> and <div>?
Span as an in-line element and a div as a block level element.
Span:
The span element basically occupies only the amount of space required by the element , it will not take the whole space/width of the
web page .
Second thing is span doesn't breaks the line , i.e. all the spans will be shown in single line in a web page as shown in the
Page 8
I BSC VI SEM Web Technologies
screenshot below.
Div:
The div element occupies all the width available to it on the web page and every time a new div element is made it breaks the line
and starts in a new line.
<html>
<head>
<title>Span tag</title>
</head>
<body>
<p>web technologies
<span style="color:green;background-color:red;">Web Technology</span></p>
<p>Web Technologies
<div style="color:red;background-color:yellow;">Web Technology</div></p>
</body>
</html>
6. Layers in CSS
The page layout that a browser creates results from layering text and images on the top of each other. This lets web designers use
images as the backgrounds of their pages and then place further images and text over them. By extending the idea slightly we can
place text items and images on top of each other in multiple layers.
Netscape has extended the HTML standard by adding the layer tag. The layer tag is browser specific and it use leads to confusion with
the more general idea of layers. The following are the properties and values of layers.
Z-index: n
The browser maintains a stack of layers of content.
Position: absolute | relative
The placement of the layer may be absolute or relative. The default is absolute.
Left: n, Top: n
The location of the division in pixels. We can specify the position of their top-left corner.
Width: n, Height: m
The size of division in pixels defaults to the document of space needed to display the content
of the division.
Example:
<html>
<head><title>layers</title></head>
<body>
<div style="left:100px;
top:300px;
position:absolute;
color:magenta;
background-color:yellow;
font-size:thin-groove;">
<p>some more text here</p>
</div>
<div style="left:100px;
top:260px;
position:absolute;
color:magenta;
background-color:green;
font-size:thin-groove;">
<p>some more text here</p>
</div>
<div style="left:100px;
top:230px;
position:absolute;
color:magenta;
background-color:red;
Page 9
I BSC VI SEM Web Technologies
font-size:thin-groove;">
<p>some more text here</p>
</div>
</body>
</html>
Unit-II
Important Questions
Short Questions:
1. Write about advantages and disadvantages of CSS.
2. Briefly Explain about Properties and values in CSS.
3. Explain difference between Span and div
Essay Questions:
1. What is CSS? Why use CSS? CSS Syntax? Various types of CSS?
2. Explain about formatting blocks of information?
3. Expalin about layers in CSS in detail write with Example Program?
Page 10