Chapter04 CSS
Chapter04 CSS
What is CSS ?
• CSS stands for “Cascading Style Sheets”.
• Cascading: refers to the procedure that determines which style will apply
to a certain section, if you have more than one style rule.
• Style: how you want a certain section of your page to look. You can set the
HTML webpage with color, font, layout, margin, etc for things like tables,
paragraphs, and headings.
• Sheets: the “sheets” are like templates, or a set of rules, for determining
how the webpage will look.
• So, CSS (all together) is a styling language – a set of rules to tell browsers
how HTML webpage should look like. look.
Why do we need 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.
• Style rule has two parts: (1) selector and (2) declaration.
Selector {
declaration; Example : h1 // Selector (HTML tags)
} {
color: red; // Declaration
text-align : center;
}
Declaration = {property: value;}
<body>
<p style=“text-align: center; font-weight: bold; color: yellow;” > What was I thinking? </p>
<h1 style=“color:red;font-size:50px;”> The heading-1 style is formatted using CSS </h1>
</body>
What was I thinking?
The heading-1 style is formatted using CSS
Note :
• Placed inside the HTML tags.
• Specific to a single instance of an html tag on a page.
2. Internal Style Sheet
• Applicable to an entire HTML document.
• Styles are defined within the <style> </style> tag,
which is placed in the header of the html file
(i.e., within <head> and </head>).
<html>
<head>
<title>Title</title>
<style type="text/css">
<!--[STYLE INFORMATION GOES
HERE] -->
</style>
</head>
<body>
[DOCUMENT BODY GOES HERE]
</body>
</html> 7
Internal Style Sheet Example
<html>
<head>
<title>My Wonderful Example</title>
<style type=“text/css”>
body {
text-align: left;
font-family: verdana;
}
</style>
</head>
<body>
………………..
</body>
</html>
3. External (Linked) Styles
• External style sheet
• Styles are saved in a separate file, with the extension .css
• This single stylesheet can be used to define the look of multiple
pages.
In TextPad,Notepad, etc.… Save this text
file as
p {font-family: verdana, sans-serif; filename.css
font-size: 12pt; color: red}
9
Linked (External)
• To apply the stylesheet “filename.css“ to an HTML
document, call it in from the header:
<html>
<head>
<link rel="stylesheet" href=“filename.css"
type="text/css">
</head>
<body>
…………………………
</body>
</html>
10
Inheritance / Cascading Precedence: which
style prevails when several are present?
11
Grouping selectors
• Separate selectors with a comma
– Example:
h1,h2,h3,h5,h6 { color: green }
(each header will be green)
• Separate selectors with a space
– Example:
p li { color: red }
(only items within a list and a paragraph tag will be red)
12
The class Selector
• With a class selector you can define different styles for the same
type of HTML element
• Examples:
First define the class:
p.right {text-align: right; color: red; font-style: italic}
p.blue {text-align: center; color:blue}
Then use the class in your HTML code :
<p class="right"> This paragraph will be right-aligned, italic, and red.
</p>
<p class=“blue"> This paragraph will be center-aligned and blue. </p>
13
The class Selector
• You can also omit the tag name in the selector to define a
style that will be used by all HTML elements that have this
class.
• Example:
.poem {text-align: center; font-style:italic}
14
The class Selector
• Example (continued)
Both elements below will follow the rules in the
".poem“ class:
15
Class Example
<style>
p {font-family: sans-serif; font-size: 10pt; background-color:green}
h1 {font-family: serif; font-size: 30pt}
h2 {font-family: serif; font-size: 24pt}
16
Applying styles to portions of a
document:
• <span>
– “Wraps” a portion of text into a unit, but doesn't
cause a line break. Allows styles to be applied to an
'elemental' region (such as a portion of a
paragraph).
Example :
• <div>
– A division tag: to “package” a block of document
into one unit. It defines a block element.
– Causes a line break, like <br> and <p>.
– Example : 17
Font formatting properties
• Font-Properties : Values
– font-family : Arial, Times, Courier
– font-style : normal, italic
– font-variant : normal, small-caps
– Font-weight : normal, bold
– font-size : xx-small, x-small, small, medium, large
– Line-height : numerical values (gap between two lines)
More about font-family
32
Examples
img { border-style: ridge;
border-width: 20px;
border-color:red green
blue purple}
h1 {background-color:
#CC66FF;
width: 50%; H1,50% ,purple
padding: 20px} background 33
CSS comments /*…*/
/* This is a comment.
It can span many lines in the CSS file. */
p {
color: red; background-color: aqua;
}
CSS
34