0% found this document useful (0 votes)
5 views

Lab4

This document is a lab manual for a course on Cascading Style Sheets (CSS) at Hamdard Institute of Engineering & Technology. It covers the basics of CSS, including its syntax, advantages, methods of insertion, and various selectors, as well as tasks for students to practice CSS styling. The manual emphasizes the importance of separating content from presentation and provides examples and guidelines for using CSS effectively.

Uploaded by

faheem.ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab4

This document is a lab manual for a course on Cascading Style Sheets (CSS) at Hamdard Institute of Engineering & Technology. It covers the basics of CSS, including its syntax, advantages, methods of insertion, and various selectors, as well as tasks for students to practice CSS styling. The manual emphasizes the importance of separating content from presentation and provides examples and guidelines for using CSS effectively.

Uploaded by

faheem.ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY

Hamdard Institute of Engineering & Technology


Hamdard University

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

Ways to Insert CSS


There are three ways of inserting a style sheet:

 External style sheet


 Internal style sheet
 Inline style

External Style Sheet


An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you
can change the look of an entire Web site by changing one file. Each page must link to the style sheet
using the <link> tag. The <link> tag goes inside the head section:

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

Internal Style Sheet


An internal style sheet should be used when a single document has a unique style. You define internal
styles in the head section of an HTML page, by using the <style> tag, like this:

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
<head>
<style>
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
</style>
</head>

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:

<p style="color:sienna;margin-left:20px">This is a paragraph.</p>

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

<!DOCTYPE html> Hello World!


<html>
<head> This paragraph is not affected by the style.
<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>

The class Selector


The class selector is used to specify a style for a group of elements. Unlike the id selector, the class
selector is most often used on several elements. This allows you to set a particular style for many HTML

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
elements with the same class. The class selector uses the HTML class attribute, and is defined with a "."In
the example below, all HTML elements with class="center" will be center-aligned.

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

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

Multiple Style Sheets


If some properties have been set for the same selector in different style sheets, the values will be inherited
from the more specific style sheet.

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.

Multiple Styles Will Cascade into One


Cascading order
What style will be used when there is more than one style specified for an HTML element?

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:

1. a:link - a normal, unvisited link


2. a:visited - a link the user has visited
3. a:hover - a link when the user mouse over it
4. a:active - a link the moment it is clicked

When setting the style for several link states, there are some order rules:

 a:hover MUST come after a:link and a:visited


 a:active MUST come after a:hover

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:

 Set different list item markers for ordered lists


 Set different list item markers for unordered lists
 Set an image as the list item marker

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

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

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.

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

Hamdard Institute of Engineering & Technology, Hamdard University


SharaeMadinat al-Hikmah, Hakim Mohammad Said Road, Karachi - 74600, Pakistan

You might also like