Lab4
Lab4
Lab # 04
Cascading Style Sheets
Objectives:
In this lab you will learn about adding styles to your webpage and DHTML concepts.
Theory:
What is CSS?
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation semantics (the
look and formatting) of a document written in a markup language. Its most common application is to style
web pages written in HTML.
CSS is designed primarily to enable the separation of document content (written in HTML or a similar
markup language) from document presentation, including elements 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 pages to share formatting, and reduce complexity and
repetition in the structural content.
Advantages of CSS
-CSS saves time
-Pages Load Faster
-Easy Maintenance
-Superior Styles to HTML
-Multiple Device Compatibility
-Global Web Standards
-Offline Browsing
-Platform Independence
CSS Syntax
CSS has a simple syntax and uses a number of English keywords to specify the names of various style
properties.
A style sheet consists of a list of rules. Each rule or rule-set consists of one or more selectors, and a
declaration block. A declaration-block consists of a list of declarations in braces. Each declaration itself
consists of a property, a colon (:), and a value. If there are multiple declarations in a block, a semi-colon
(;) must be inserted to separate each declaration. In CSS, selectors are used to declare which part of the
markup a style applies to, a kind of match expression. Selectors may apply to all elements of a specific
type, to elements specified by attribute, or to elements depending on how they are placed relative to, or
nested within, others in the document tree.
Hamdard Institute of Engineering & Technology, Hamdard University
SharaeMadinat al-Hikmah, Hakim Mohammad Said Road, Karachi - 74600, Pakistan
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Example:
p {color:red;text-align:center;}
OR
p
{
color:red;
text-align:center;
}
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
An external style sheet can be written in any text editor. The file should not contain any html tags. Your
style sheet should be saved with a .css extension. An example of a style sheet file is shown below:
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
Inline Styles
An inline style loses many of the advantages of style sheets by mixing content with presentation. Use this
method sparingly! To use inline styles you use the style attribute in the relevant tag. The style attribute
can contain any CSS property. The example shows how to change the color and the left margin of a
paragraph:
The id Selector
The id selector is used to specify a style for a single, unique element. The id selector uses the id attribute
of the HTML element, and is defined with” #". The style rule below will be applied to the element with
id="para1".
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head> Center-aligned heading
<style>
.center Center-aligned paragraph.
{
text-align:center;
color:red;
}
</style>
</head>
<body>
<h1 class="center">Center-aligned heading</h1>
<p class="center">Center-aligned paragraph.</p>
</body>
</html>
You can also specify that only specific HTML elements should be affected by a class.In the example
below, all p elements with class="center" will be center-aligned:
<!DOCTYPE html>
<html>
<head> This heading will not be affected
<style>
p.center This paragraph will be center-aligned.
{
text-align:center; Simple paragraph tag
}
</style>
</head>
<body>
<h1 class="center">This heading will not be
affected</h1>
<p class="center">This paragraph will be center-
aligned.</p>
<p>Simple paragraph tag</p>
</body>
</html>
For example, an external style sheet has these properties for the h3 selector:
h3
{
color:red;
text-align:left;
font-size:8pt;
}
And an internal style sheet has these properties for the h3 selector:
h3
{
text-align:right;
font-size:20pt;
}
If the page with the internal style sheet also links to the external style sheet the properties for h3 will be:
color:red;
text-align:right;
font-size:20pt;
The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by
the internal style sheet.
Generally all the styles will "cascade" into a new "virtual" style sheet by the following rules, where
number four has the highest priority:
1. Browser default
2. External style sheet
3. Internal style sheet (in the head section)
4. Inline style (inside an HTML element)
Hamdard Institute of Engineering & Technology, Hamdard University
SharaeMadinat al-Hikmah, Hakim Mohammad Said Road, Karachi - 74600, Pakistan
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
So, an inline style (inside an HTML element) has the highest priority, which means that it will override a
style defined inside the <head> tag, or in an external style sheet, or in a browser (a default value).
CSS Styling
1. CSS Background
Property Description
background Sets all the background properties in one declaration
background-
Sets whether a background image is fixed or scrolls with the rest of the page
attachment
background-color Sets the background color of an element
background-image Sets the background image for an element
background-position Sets the starting position of a background image
background-repeat Sets how a background image will be repeated
2. CSS Text
Property Description
color Sets the color of text
direction Specifies the text direction/writing direction
letter-spacing Increases or decreases the space between characters in a text
line-height Sets the line height
text-align Specifies the horizontal alignment of text
text-decoration Specifies the decoration added to text
text-indent Specifies the indentation of the first line in a text-block
text-shadow Specifies the shadow effect added to text
text-transform Controls the capitalization of text
unicode-bidi
vertical-align Sets the vertical alignment of an element
white-space Specifies how white-space inside an element is handled
word-spacing Increases or decreases the space between words in a text
3. CSS Fonts
Property Description
font Sets all the font properties in one declaration
font-family Specifies the font family for text
font-size Specifies the font size of text
font-style Specifies the font style for text
font-variant Specifies whether or not a text should be displayed in a small-caps font
font-weight Specifies the weight of a font
4. CSS Links
Hamdard Institute of Engineering & Technology, Hamdard University
SharaeMadinat al-Hikmah, Hakim Mohammad Said Road, Karachi - 74600, Pakistan
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
The four links states are:
When setting the style for several link states, there are some order rules:
Property Description
background-color The background-color property specifies the background color for links
text-decoration The text-decoration property is mostly used to remove underlines from links
5. CSS Lists
The CSS list properties allow you to:
Property Description
list-style Sets all the properties for a list in one declaration
list-style-image Specifies an image as the list-item marker
list-style-position Specifies if the list-item markers should appear inside or outside the content
flow
list-style-type Specifies the type of list-item marker
Tasks:
Write a Html code to generate output as given below using inline CSS and internal CSS
A)
B)
Task2: Write a code and apply CSS on all kind of links, explained in this Manual.
Task3:Write a code and use All CSS styling (fonts,color,background etc) in this lab
manual.
Task4create a simple Html page and use at least 5 different CSS Selector
Note: Attach a snap shot of every above mentioned task and bring colored copy of browser
display.