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.
Introduction:-
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document
written in a markup language.
CSS is designed primarily to enable the separation of presentation and content, including aspects such as
the layout, colors, and fonts.
This separation can improve content accessibility, provide more flexibility and control in the specification
of presentation characteristics, enable multiple HTML pages to share formatting by specifying the relevant
CSS in a separate.
css file, and reduce complexity and repetition in the structural content.
It can also display the web page differently depending on the screen size or viewing device. Readers can
also specify a different style sheet, such as a CSS file stored on their own computer, to override the one the
author specified.
Advantages of CSS :-
CSS saves time − You can write CSS once and then reuse same sheet in multiple HTML pages. You
can define a style for each HTML element and apply it to as many Web pages as you want.
Pages load faster − If you are using CSS, you do not need to write HTML tag attributes every time.
Just write one CSS rule of a tag and apply it to all the occurrences of that tag. So less code means
faster download times.
Easy maintenance − To make a global change, simply change the style, and all elements in all the
web pages will be updated automatically.
Superior styles to HTML − CSS has a much wider array of attributes than HTML, so you can give a
far 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. By using the same HTML document, different versions of a website can be presented for
handheld devices such as PDAs and cell phones or for printing.
Global web standards − Now HTML attributes are being deprecated and it is being recommended to
use CSS. So its a good idea to start using CSS in all the HTML pages to make them compatible to
future browsers.
Offline Browsing − CSS can store web applications locally with the help of an offline catche.Using
of this, we can view offline websites.The cache also ensures faster loading and better overall
performance of the website.
Platform Independence − The Script offer consistent platform independence and can support latest
browsers as well.
1.Types of CSS:-
There are three types of cascading style sheet(css) are listed below:
1. Inline Style Sheet
2. Internal Style Sheet / Embedded Style Sheet and
3. External Style Sheet
1.Inline Style Sheet:-
Inline style sheets are declared within individual tags and affect those tags only.
Inline style sheets are declared with the style attribute.
Every CSS change has to be made in every tag that has the inline style applied to it.
The inline style is good for one an individual CSS change that you do not use repeatedly through the site.
Syntax: <element style=”declarations”>the text appears here</element>
Example: <p style=”text-align:center; font-size:40; text-decoration:underline;”> Cascading Style Sheet</p>
//inline.html
<html> Output:-
<head>
<title>Inline Style Sheet</title>
</head>
<body>
Cascading Style Sheet
<p style="text-align:center;text-
decoration:underline;font-size:40;">Cascading Inline Style Sheet
Style Sheet</p>
<h3 style="color:orange;">Inline Style
Sheet</h3>
</body>
</html>
2.Internal Style Sheet / Embedded Style Sheet:-
An internal style sheet holds the CSS code for the webpage in the head section of the particular file.
This makes it easy to apply styles like classes or id’ in order to reuse the code.
The downside(body section) of using an internal style sheet is that changes to the internal style sheet only
effect the page the code is inserted into.
The <style> tag specifies the content type of a stylesheet with its type attribute which should be set to
"text/css".
Syntax: Example:
<head>
<head> <style type=”text/css”>
<style type="text/css"> h1{
styles go here text-align:center;
</style> text-transform:uppercase;
</head> }
p{
font-family:algerian;
font-style:italic;
//internal.html:-
}
<html> </style>
<head> </head>
<title>Internal Style Sheet </title>
<style type="text/css">
h1{
text-align:center;
text-transform:uppercase;
}
Output:-
p{
font-family:algerian;
font-style:italic;
}
</style> USING INTERNAL STYLE SHEET
</head>
<body> First Para
<h1>Using Internal Style Sheet</h1>
<p>First Para</p>
<p>Second para</p> Second para
</body>
</html>
3.External Style Sheet:-
Use an external stylesheet when you want to apply one style to many pages.
If you make one change in an external stylesheet, the change is universal on all the pages where the
stylesheet is used.
An external stylesheet is declared in an external file with a .css extension.
It is called by pages whose interface it will affect.
External stylesheets are called using the <link> tag which should be placed in the head section of an
HTML document. This tag takes three attributes.
Attributes of the <link> tag:
rel - When using an external stylesheet on a webpage, this attribute takes the value "stylesheet"
type - When using an external stylesheet on a webpage, this attribute takes the value "text/css"
href - Denotes the name and location of the external stylesheet to be used.
Syntax:-
<head>
<link rel=”stylesheet” type=”text/css” href=”filename.css”>
</head>
//external.html //style.css
<html> h1{
<head> text-align:center;
<title>External Style Sheet</title> font-weigt:bold;
<link rel="stylesheet" type="text/css" }
href="style.css"> p{
</head> font-size:20;
<body> font-style:oblique;
<h1>Introduction</h1> color:blue;
<p>ESS is defines the css file }
seperately.</p>
<p>It includes many html document.</p>
</body> Output:-
</html>
Introduction
ESS is defines the css file seperately.
It includes many html document.
2.Explain Defining your own styles in CSS.
Defining styles:-
Styles are defined by simple rules. A style can contain as many rules as we want and as with processing
HTML, if something doesn’t makes sense it can be ignored.
Cascading Styles:
Conventionally, styles are cascaded.
This means that we do not have to use a single set of styles inside a document. We can import as many
styles sheets as we like.
This is useful if we define a set of organizational can take place in stylesheets, this means that the first is
overriding can also happen by defining styles within the body of the page.
Rules: A style rule has two parts: a selector and a set of declarations
1. The Selector is used to create a link between the rule and the HTML tag. Selectors can be placed into
classes so that a tag can be formatted in a variety of ways.
2. The Declaration has two parts: a property and a value. Property and value must be separated with colons
and terminated with semicolon( ; ).
Syntax:- selector {
property : value;
property : value;
……………….;
}
Example:
body{
background-color:blue;
}
h1{
text-align:center;
text-transform:uppercase;
}
p{
font-family:algerian;
font-style:italic;
}
Classes:- You may be wondering if it is possible to give an HTML element multiple looks with CSS. For
example, sometimes you want the font to be large and white color, while other times you would prefer the
font to be small and black color. CSS allows you to do just that with the use of classes.
Example 1:-
CSS Code:
p.first { color:blue; }
p.second { color:red;}
HTML Code:
<html>
<head>
<title> Using classes </title>
</head>
<body>
<p class=”first”>This is a paragraph that uses the p.first CSS code</p>
<p class=”second”>This is a paragraph that uses the p.second CSS code!</p>
</body>
</html>
User Stylesheet are an exciting feature of CSS. In CSS, the presentation of a document is controlled by the
combination of user and author style preference. This mechanism is needed(text only) to allow CSS to
describe fully(and the extend) the behavior of browsers.
Users can define their own styles sheets to format pages based on their preferences. In below example, user
defines different declaration for own styles (using class ).
Example 2:-
<html>
<head>
<title>Using Class</title>
<style type="text/css">
.head{
text-align:center;
color:brown;
text-decoration:underline;
}
.para{ <body>
color:green; <h1 class="head">Define you own style using class</h1>
font-size:40; <p class="para">Some of the questions are listed:</p>
<p class="question">What is your name?</p>
font-family:algerian;
<p class="answer">My name is Krishna.</p>
}
<p class="question">What is your Qualification?</p>
.question{
<p class="answer">My Qualification is Bachelor of Computer
color:#00ff00;
Science.</p>
font-size:30;
</body>
}
.answer{ </html>
color:#0000ff;
font-size:25;
text-transform:uppercase;
}
</style>
</head>
Output:-
Define you own style using class
Some of the questions are
listed:
What is your name?
MY NAME IS KRISHNA.
What is your Qualification?
MY QUALIFICATION IS BACHELOR OF COMPUTER
SCIENCE
3.Properties and Values in CSS:-
The following are the properties and its values used in styles:
Property Meaning Values
1. text-align Specifies the horizontal alignment of text. text-align: left | center |right
2. text-decoration This text decoration can be done by specifying text-decoration: underline |
underline,overline and line-through overline | line-through
3. text-transform Controls the capitalization of text text-transform: uppercase |
lowercase | capitalize
4. font-family Specifies the font family for text font-family: aria | algerian |
verdana | Calibri
5. font-size The size of the text can be given( in pixels(px), font-size: 20px | 50% | 80in
percentage(%), inches(in))
6. font-style Specifies the font style for text font-style: normal | italic |
oblique
7. font-weight Specifies the weight of a font font-weight: normal | bold | 900
8. color Sets the color of text (either by name or color: red | #00ff00 |
hexadecimal or rgb colors) rgb(0,0,255)
9. background-color Specifies the background color of an element background-color: orange
10.height Sets the height of an element height: auto | 50 | 25%
11.width Sets the width of an element Width: auto | 50px | 30%
12.margin Set the margin for all sides as top, right, bottom margin: 35px
and left
13. background-image Specifies one or more background image for an background-image:
element path(“paper.gif”)
14. border-color Sets the color of the four borders border-color: red
15. border-style Sets the style of the four border Border-style: dashed | solid |
double | groove
16. direction Specifies the text direction / writing direction direction: rtl | ltr default : ltr
17. text-decoration-color Specifies the color of the text-decoration text-decoration-color : red
18. white-space Specifies how white-space inside an element is white-space: nowrap
handled
19. visibility Specifies whether or not an element is visible Visibility: visible | hidden
20. z-index Sets the stack order of a positioned element z-index: -1
4. Discuss about Formatting Blocks of Information:
All the HTML elements can be categorized into two categories
a) Block Level Elements b) Inline Elements
a) Block Level Elements:
Block level elements appear on the screen as if they have a line break before and
after them.
For example, the <p>, <h1>, <h2>, <h3>, <h4>, <h5>, <h6>, <ul>, <ol>, <dl>, <pre>, <hr/>, <blockquote>
and <address> elements are all block level elements. They all start on their own new line, and anything that
follows them appears on its own new line.
b) Inline Elements:
Inline elements, on the other hand, can appear within sentences and do not have to
appear on a new line of their own. The <b>, <i>, <u>, <em>, <strong>, <sup>, <sub>, <big>, <small>, <li>,
<ins>, <del>, <code>, <cite>, <dfn>, <kbd>, and <var> elements are all inline elements.
Grouping HTML Elements:
There are two important tags which we use very frequently to group various other HTML tags
i) <div> tag and ii) <span> tag
i) <div> tag:
This is very important block level tag which plays a big role in grouping various other
HTML tags and applying CSS on group of elements. Even now <div> tag can be used to create webpage
layout where we define different parts (Left, Right, Tot etc.) of the page using <div> tag. This tag does not
provide any visual change on the block but this has more meaning when it is used with CSS.
Example:
<html> <div style="color:red">
<head> <!-- First group of tags--> Output:
<title>HTML div tag</title> <h4>This is Second group</h4>
</head> <p>List of Fruits</p>
<body> <ul>
<div style="color:green"> <li>Apple</li>
<!-- First group of tags--> <li>Mango</li>
<h4>This is first group</h4> <li>Banana</li>
<p>List of vegetables</p>
<ul> </ul>
<li>Tomatto</li> </div>
<li>Potato</li>
<li>Radish</li> </body>
</ul> </html>
(ii) <span> tag:
The HTML <span> is an inline element and it can be used to group inline - elements in an
HTML document. This tag also does not provide any visual change on the block but has more meaning when
it is used with CSS.
The difference between the <span> tag and the <div> tag is that the <span> tag is used with
inline elements whereas the <div> tag is used with block – level elements.
Example: Output:
<html>
<head>
<title>HTML div tag</title>
</head>
<body>
<p> This is <span style="color:red">red</span> and this is
<span style="color:green">green</span></p>
</body>
</html>
5. Write about Layers.
With CSS, it is possible to work with layers; pieces of HTML that are placed on top on the
regular page with pixel precision (accuracy).
Layer Basics:
First Look at this example: Layer 1 on TOP
<html>
<head>
<title>Layers</title>
</head>
<body>
<div style="position:relative;font-size:50px;color:red;z-index:2;">LAYER 1</div>
<div style="position:relative;font-size:80px;top:-50;px;left:5;color:green;z-index:1;">LAYER 2</div>
</body>
</html>
Layer 2 on TOP:
<html>
<head>
<title>Layers</title>
</head>
<body>
<div style="position:relative;font-size:50px;color:red;z-index:3;">LAYER 1</div>
<div style="position:relative;font-size:80px;top:-50;px;left:5;color:green;z-index:4;">LAYER 2</div>
</body>
</html>
To create a layer all you need to do is assign the position attribute to your style. The position
can be either absolute (or) relative.
The position itself is defined with the top and left properties. Finally, which layer is on top is
defined with the z-index attribute.
Relative versus Absolute Positioning:
You can either position your layer calculated from the upper left corner (absolute) or
calculated from the position where the layer itself is inserted (relative).
Defining the Position:
While the position property indicates the out spring of our coordinate system, the left and top
properties defines the exact position of our layer.
You can enter both positive and negative values for these properties – thus it is possible to
place content higher up and further to the left on the page than the logical position in the HTML code where
the layer itself is defined.
Both left and top properties can be dynamically changed with JavaScript. Now, it is possible
to move things around on the screen even after the page has finished loading.
Position in the Stack – the Z-index:
Picture a game of 52 cards. If the ace of spades was at the bottom we did say it had z-index:1;
If the queen of hearts was at the top we did say she had z-index:52; The z-index can be dynamically changed
with JavaScript.
You could create several “pages” on top of each other all on the same page. When the user
clicks a link it will simply move the layer with the desired info on top rather than load a new page.
Visible versus Hidden Layers:
A final property is the visibility property that will allow you to create invisible layers.
Dynamic HTML is possible to change the visibility of layer according to certain events.
The most common use of this is to create menus that pop out. The trick behind these menus is to
create all submenus as invisible layers.
When a mouse-over is detected on a link the according layer becomes visible.
Valid values for the visibility property are: visible and hidden.
Example:
<div style=”position:relatve;visibility:hidden;”>Hello</div>