Cascading: Style
Cascading: Style
Cascading: Style
CSS
CSS is an acronym for Cascading Style Sheets.
CSS is a style language that defines layout of
HTML documents
CSS covers fonts, colours, margins, lines,
height, width, background images, advanced
positions
control the style and layout of multiple Web
pages all at once.
2
CSS
HTML can be (mis-)used to add layout to
websites.
But CSS offers more options and is more
accurate and sophisticated.
CSS is supported by all browsers today.
CSS format
CSS how to
There are three ways of inserting a style sheet:
Inline style sheet
Internal style sheet
External style
Benefit
12
Try it yourself
13
Try it yourself
.css file
body{
background-color:#d0e4fe;
}
h1{
color:orange;
text-align:center;
}
p{
font-family:"Times New Roman";
font-size:20px;
}
a:link {color:#FF0000;} /* unvisited link */
a:visited {color:#00FF00;} /* visited link */
a:hover {color:#FF00FF;} /* mouse over link */
a:active {color:#0000FF;} /* selected link */
14
CSS ID
CSS allows you to specify
your own selectors called
"id" and "class".
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 a "#".
<html>
<head>
<style type="text/css">
#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>
15
CSS Class
The class Selector
<html>
<head>
<style type="text/css">
.center
{
text-align:center;
}
</style>
</head>
<body>
<h1 class="center">Center-aligned
heading</h1>
<p class="center">Center-aligned
paragraph.</p>
</body>
</html>
16
CSS Class
The class Selector
You can also specify
that only specific
HTML elements should
be affected by a class.
<html>
<head>
<style type="text/css">
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>
17
18
CSS Text
Text Color
19
CSS Text
<!DOCTYPE html>
<html>
<head>
<style>
body {color:red;}
h1 {color:#00ff00;}
p.ex {color:rgb(0,0,255);}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<p>This is an ordinary paragraph. Notice that this text is red. The default text-color
for a page is defined in the body selector.</p>
<p class="ex">This is a paragraph with class="ex". This text is blue.</p>
</body>
</html>
20
CSS Text
Text Alignment
The text-align property is used to set the horizontal
alignment of a text.
<html>
<head>
<style>
h1 {text-align:center;}
p.date {text-align:right;}
p.main {text-align:justify;}
</style>
</head>
<body>
<h1>CSS text-align Example</h1>
<p class="date">May, 2009</p>
<p class="main">Bangladesh is identified as a Next Eleven economy. According to the United Nations in
2010, the country is making major strides in human development, including significant progress in
the areas of gender equity, universal primary education, women empowerment, reducing population
growth, food production, health and renewable energy.</p>
</body>
</html>
21
CSS Text
Text Decoration
The text-decoration property is used to set or remove
decorations from text.
<html>
<head>
<style>
h1 {text-decoration:overline;}
h2 {text-decoration:line-through;}
h3 {text-decoration:underline;}
h4 {text-decoration:none;}
</style>
</head>
<body>
<h1>This is heading 1</h1><h2>This is heading 2</h2>
<h3>This is heading 3</h3> <h4>This is heading 4</h4>
</body>
</html>
22
CSS Text
Text Transformation
The text-transform property is used to specify
uppercase and lowercase letters in a text.
<html>
<head>
<style>
p.uppercase {text-transform:uppercase;}
p.lowercase {text-transform:lowercase;}
p.capitalize {text-transform:capitalize;}
</style>
</head>
<body>
<p class="uppercase">This is some text.</p>
<p class="lowercase">This is some text.</p>
<p class="capitalize">This is some text.</p>
</body>
</html>
23
CSS Text
Text Indentation
The text-indent property is used to specify the
indentation of the first line of a text.
<html>
<head>
<style>
p {text-indent:50px;}
</style>
</head>
<body>
<p> Bangladesh is identified as a Next Eleven economy. According to
the United Nations in 2010, the country is making major strides in human
development, including significant progress in the areas of gender equity,
universal primary education, women empowerment, reducing population
growth, food production, health and renewable energy
</p>
</body>
</html>
24
CSS Font
25
Positioning of elements
Absolute positioning
Relative positioning
To position an element relatively, the
property position is set as relative. The difference
between absolute and relative positioning is how the
position is being calculated.
The position for an element which is relatively
positioned is calculated from the original position in
the document. That means that you move the element
to the right, to the left, up or down.
26
Example
#Pic1 { position:relative; left: 350px; bottom:
150px; }
#Pic2 { position:relative; left: 150px; bottom:
500px; }
#box1 { position:absolute; top: 50px; left:
50px; }
#box2 { position:absolute; top: 50px; right:
50px; }
27
30
31
CSS Links
Styling Links
Links can be styled with any CSS property (e.g.
color, font-family, background, etc.).
In addition, links can be styled differently
depending on what state they are in.
The four links states are:
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
33
Box Model
All HTML elements can be considered as boxes. In CSS, the
term "box model" is used when talking about design and
layout.
The CSS box model is essentially a box that wraps around
HTML elements, and it consists of: margins, borders, padding,
and the actual content.
The box model allows us to place a border around elements
and space elements in relation to other elements.
34
Box Model
35
Box Model
Explanation of the different parts:
Margin - Clears an area around the border. The margin
does not have a background color, it is completely
transparent
Border - A border that goes around the padding and
content. The border is affected by the background
color of the box
Padding - Clears an area around the content. The
padding is affected by the background color of the box
Content - The content of the box, where text and
images appear
36
Box Model
Margin is on the outside of block elements
while padding is on the inside.
Use margin to separate the block from things
outside it
Use padding to move the contents away from the
edges of the block.
37
Box Model
The total width of an element should be calculated like this:
Total element width = width + left padding + right padding + left
border + right border + left margin + right margin
38
Box Model
Border
Border Style
Border Width
Border Color
Border - Individual sides
Border - Shorthand property
39
Box Model
Margin
The margin clears an area around an element (outside the
border). The margin does not have a background color, and is
completely transparent.
The top, right, bottom, and left margin can be changed
independently using separate properties. A shorthand margin
property can also be used, to change all margins at once.
40
Box Model
Margin
41
Box Model
Padding
The padding clears an area around the content (inside the
border) of an element. The padding is affected by the
background color of the element.
The top, right, bottom, and left padding can be changed
independently using separate properties. A shorthand
padding property can also be used, to change all paddings at
once.
42
Box Model
Padding
The padding clears an area around the content (inside the
border) of an element. The padding is affected by the
background color of the element.
The top, right, bottom, and left padding can be changed
independently using separate properties. A shorthand
padding property can also be used, to change all paddings at
once.
43
Example
Body
{
margin-top: 100px;
margin-right: 40px;
margin-bottom: 10px;
margin-left: 70px;
}
body { margin: 100px 40px 10px 70px; }
44
Example
p { margin: 5px 50px 5px 50px; }
H1
{ background: yellow; padding: 20px 20px
20px 80px; }
h2
{ background: orange; padding-left:120px; }
45
Float
An element can be floated to the right or to
left by using the property float. That is to say
that the box with its contents either floats to
the right or to the left in a document (or the
containing box)
46
Example
#column1 { float:left; width: 33%; }
#column2 { float:left; width: 33%; }
#column3 { float:left; width: 33%; }
47
clear
The clear property is used to control how the
subsequent elements of floated elements in a
document shall behave.
By default, the subsequent elements are
moved up to fill the available space which will
be freed when a box is floated to a side.
The property clear can assume the
values left, right, both or none.
48
Example
To avoid the text from floating up next to the
element, we can add the following to our CSS:
#picture { float:left; width: 100px; }
. stopfloat { clear:both; }
49
50