IT Elective I - Cascading Style Sheets
IT Elective I - Cascading Style Sheets
CSS controls the style and layout of multiple Web pages all at once.
The CSS syntax is made up of three parts: a selector, a property and a value:
selector {property:value}
Selector
The HTML element/tag you wish to define
Property
The attribute you wish to change
Note: If the value is multiple words, put quotes around the value.
More than one property
p {text-align:center;color:red}
p
{
text-align:center;
color:black;
font-family:arial
}
Grouping
You can group selectors. Separate each selector with a comma.
h1,h2,h3,h4,h5,h6
{
color:green
}
The Class Selector
Let you define different styles for the same type of HTML element.
p.right {text-align:right}
p.center {text-align:center}
To apply more than one class per given element, the syntax is:
.center {text-align:center}
#green {color:green}
The style rule below will match the p element that has an id with a value of "par1":
p#par1
{
text-align:center;
color:red
}
Class vs ID Selector
Use a class tag if:
/*This is a comment*/
p
{
text-align:center;
/*This is another comment*/
color:black;
font-family:arial
}
Ex. 1
CSS File
body {background-color: yellow}
h1 {font-size: 36pt}
h2 {color: blue}\
p {margin-left: 50px}
HTML File
<html>
<head>
<link rel="stylesheet" type="text/css" href="ex1.css" />
</head>
<body>
<h1>This header is 36 pt</h1>
<h2>This header is blue</h2>
<p>This paragraph has a left margin of 50 pixels</p>
</body>
</html
Ex. 2
CSS File
HTML File
<html>
<head>
<link rel="stylesheet" type="text/css" href="ex2.css" />
</head>
<body>
<h1>This is a header 1</h1>
<hr />
<p>You can see that the style sheet formats the text</p>
<p><a href="http://www.w3schools.com" target="_blank">This is a link</a></p>
</body>
</html>
How to Insert a Style Sheet
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
An internal style sheet should be used when a single document has a unique style.
You define internal styles in the head section by using the <style> tag,.
<head>
<style type="text/css">
hr {color:sienna}
p {margin-left:20px}
body {background-image:url("images/back40.gif")}
</style>
</head>
Inline Styles
External:
Internal: The color is inherited from the external style sheet and
the text-alignment and the font-size is replaced
h3 by the internal style sheet.
{
text-align:right;
font-size:20pt
}
CSS Background
<style type="text/css">
body
{
background-color:yellow;
}
h1
{
background-color:green;
}
p
{
background-color:rgb(255,0,255);
}
</style>
Set an image as the background
<style type="text/css">
Body
{
background-image:url('paper.gif')
}
</style>
How to repeat a background image only vertically
<style type="text/css">
body
{
background-image:url('paper.gif');
background-repeat:repeat-y;
}
</style>
<style type="text/css">
body
{
background-image:url('paper.gif');
background-repeat:repeat-x;
}
</style>
How to display a background image only one time
<style type="text/css">
body
{
background-image:url('paper.gif');
background-repeat:no-repeat;
}
</style>
<style type="text/css">
body
{
background-image: url('smiley.gif');
background-repeat: no-repeat;
background-attachment:fixed;
background-position: 30% 20%;
}
</style>
How to position a background image using pixels
<style type="text/css">
body
{
background-image: url('smiley.gif');
background-repeat: no-repeat;
background-attachment:fixed;
background-position: 50px 100px;
}
</style>
How to set a fixed background image
<style type="text/css">
body
{
background-image:url('smiley.gif');
background-repeat:no-repeat;
background-attachment:fixed
}
</style>
All the background properties in one declaration
<style type="text/css">
body
{
background: #00ff00 url('smiley.gif') no-repeat fixed center;
}
</style>
All CSS Background Properties
Property Description Values
background A shorthand property background-color
for setting all background-image
background properties background-repeat
in one declaration background-attachment
background-position
The color property is used to set the color of the text. The color can be set by:
Body
{
color:blue
}
H1
{
color:#00ff00
}
H2
{
color:rgb(255,0,0)
}
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
h1
{
text-align:center
}
p.date
{
text-align:right
}
p.main
{
text-align:justify
}
Text Decoration
a
{
text-decoration:none
}
h1 {text-decoration:overline}
h2 {text-decoration:line-through}
h3 {text-decoration:underline}
h4 {text-decoration:blink}
Text Transformation
The text-transform property is used to specify uppercase
and lowercase letters in a text.
p.uppercase
{
text-transform:uppercase
}
p.lowercase
{
text-transform:lowercase
}
p.capitalize
{
text-transform:capitalize
}
Text Indentation
p
{
text-indent:50px
}
H
{
letter-spacing: -3px
}
H4
{
letter-spacing: 0.5cm
}
Space between lines
p.small
{
line-height: 90%
}
p.big
{
Line-height: 200%
}