Web-Engineering: CSS - Cascading Style Sheet
Web-Engineering: CSS - Cascading Style Sheet
Web-Engineering: CSS - Cascading Style Sheet
When tags like <font>, and color attributes were added to the
HTML 3.2 specification, it started a nightmare for web developers.
Development of large web sites, where fonts and color information
were added to every single page, became a long and expensive
process.
To solve this problem, the World Wide Web Consortium (W3C)
created CSS.
In HTML 4.0, all formatting could be removed from the HTML
document, and stored in a separate CSS file.
All browsers support CSS today.
CSS Syntax and CSS Types
A CSS rule has two main parts:
1. a selector
2. one or more declarations
A CSS declaration always ends with a semicolon and declaration
groups are surrounded by curly brackets.
<style type=“text/css”>
h1 { color: red}
h3 { color: blue}
</style>
</head>
<body>
</body>
</html>
Note:- Every time you use the h1 or h3 tag the color attribute will also be applied to the
text of h1 or h3 tag, on the same web page.
Type of CSS – Internal Style Sheet ….
<html>
<head>
<style>
body {background-color:yellow;}
h1 {font-size:36pt;}
h2 {color:blue;}
p {margin-left:50px;}
</style>
</head>
<body>
<h2>This header is blue</h2>
<h1>This header is 36 pt</h1>
<h2>Again This header is blue</h2>
<p>This paragraph has a left margin of 50 pixels</p>
</body>
</html>
Type of CSS – External Style Sheet
External style sheet is ideal when the same style is applied
to manage pages.
With an external style sheet you can change the
appearance and layout of all the pages in a Web site, just
by editing one single file.
Each web 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>
<html>
<head>
<link rel="stylesheet" type="text/css“ href="mystyle.css">
</head>
<body>
<h2>This header is blue</h2>
<h1>This header is 36 pt</h1>
<h2>Again This header is blue</h2>
<p>This paragraph has a left margin of 50 pixels</p>
</body>
</html>
Using Multiple Style Sheet
What style will be used when there is more than one style
specified for an HTML element?
Generally speaking we can say that 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)
{ text-align:center; }
</style>
</head>
<body>
<h1 class="center">Center-aligned heading</h1>
<p class="center">Center-aligned paragraph.</p>
</body>
</html>
Output of Code
CSS - class Selectors Example 2
You can also specify that only specific HTML elements
should be affected by a class.
<html>
<head>
<style>
p.center
{ text-align:center; }
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be center-aligned.</p>
</body>
</html>