Internet Programming Internet Programming: (Cascading Style Sheets)
Internet Programming Internet Programming: (Cascading Style Sheets)
Internet Programming
Hassan Khan
1
12/1/2009
CSS Syntax
2
12/1/2009
Example
z selector {property: value}
body {color: black}
p {font-family: "sans serif"}
z p {text-align:center; color:red}
zp { text-align: center;
color: black;;
font-family: arial
}
To make the style definitions more readable, you
can describe one property on each line.
Grouping
3
12/1/2009
Class Selector
z With the class selector you can define different styles for
the same type of HTML element
element.
p.right {text-align: right}
p.center {text-align: center}
HTML
<p class="right">This paragraph will be right-aligned. </p>
<p
p class="center">This p
paragraph
g p will be center-aligned.</p>
g p
Class Selector
z You can also omit the tag name in the selector to define
a style that will be used by all HTML elements that have
a certain class.
.center {text-align: center}
All HTML elements with class="center" will be center-
aligned.
4
12/1/2009
z iinput[type="text"]
[ " "] {background-color:
{b k d l blue}
bl }
The style rule below will match all input elements that
have a type attribute with a value of "text“.
The id Selector
You can also define styles for HTML elements with the id
selector The id selector is defined as a #
selector. #.
5
12/1/2009
CSS Comments
z Comments are used to explain your code.
p { text-align: center;
/* This is another comment */
color: black;
font-family: arial
}
6
12/1/2009
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
Example
HTML
<html>
<head>
<title>Inline style sheets</title>
<link href="first
href= first.css
css" rel=
rel="stylesheet"></link>
stylesheet ></link>
</head>
<body>
<p>This is sample paragraph</p>
<h1>Sample heading</h1>
<i>this is sample text</i>
</body>
</html>
CSS
h1{ background-color:lightgreen}
body{ background-image:url("Blue hills.jpg");
background-repeat:none;
background-position:center;
background-attachment:fixed}
7
12/1/2009
Note: old browser that does not support styles, will ignore the <style>
tag, but the content of the <style> tag will be displayed on the page.
Inline Styles
z Use this method sparingly, such as when a style is to be
applied to a single occurrence of an element
element.