Web Design-Lecture 8-CSS BoxModel
Web Design-Lecture 8-CSS BoxModel
Hoshang Qasim
E-mail: [email protected]
2021
Introduction to Box Model
2
Introduction to Box Model
• 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.
3
• Content - The content of the box, where text and images appear.
Introduction to Box Model
4
they are the space outside of the actual element.
The border properties
• enable you to specify how the border of the box representing an element should look.
p{
border-style : solid;
border-color : #ff0000;
5
border-width : 4px;
}
The border properties
border-bottom-style
border-right-style
border-top-style
border-bottom-color border-left-style
border-right-color
border-top-color
border-bottom-width
border-left-color
border-right-width
border-top-width
border-left-width
6
The border properties
7
The border Example
<head>
<style type="text/css">
h1{border-style : solid;
border-color : #ff0000;
border-width : 4px;
border-bottom-color:green;
border-right-color :yellow;
border-top-color :pink;
border-left-color :gray;
border-bottom-style :dotted;
border-top-style :dashed;
border-bottom-width :15px;
}
</style>
</head>
<body>
8 <h1>testing for margin position</h1>
</body>
border properties
Shorthands
#menu {
border: 1px solid red;
9
}
border properties
<html>
<head>
<style>
div {
border: 3px solid red;
margin: 25px 50px 75px 100px;
background-color: orange;
}
</style>
</head>
<body>
<h2>Margin shorthand property</h2>
<div>The border property allows us to define all border
properties in one declaration.The properties that can be
set, are:
</div>
</body>
10
</html>
border properties
Border Radius
• The border-radius property is used to define how rounded border corners are.
• The curve of each corner is defined using one or two radii, defining its shape:
circle or ellipse.
• We can set different border radius for each corner using the properties: border-
top-left-radius, border-top-right-radius, border-bottom-right-radius and border-
bottom-left-radius.
11
border properties
Border Radius
<html>
<head>
<style>
#raduis {
border-radius: 50px;
border: 5px solid blue;
padding: 50px;
width: 150px;
height: 100px;
}
</style>
</head>
<body>
<div id="raduis">Border Raduis</div>
12 </body>
</html>
Border Radius Shorthands
<div id="a"></div><div id="b"></div><div id="c"></div>
<div id="d"></div><div id="e"></div><div id="f"></div>
13
The margin property
• It is possible to specify all the margin properties in one property. This is called a
shorthand property.
• It is possible to specify all the margin properties in one property. This is called a
shorthand property.
16
The margin property Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 5px solid red;
margin: 50px;
background-color: silver;
}
</style>
</head>
<body>
<html>
<head>
<style type="text/css">
p
{
Border:5px solid;
margin:250px;
margin-left:250px;
margin-top:200px;
}
</style>
</head>
<body>
<p>testing for margin position</p>
</body>
18 </html>
The padding property
• specify how much space should appear between the content of an element and its border:
padding-top:25px;
padding-bottom:25px;
padding-right:50px;
padding-left:50px;
20
padding Shorthand property
padding:50px;
21
padding Shorthand property
<html>
<head>
<style>
div {
border: 3px solid blue;
padding: 10px 25px 50px 50px;
background-color: lightblue;
}
</style>
</head>
<body>
22
</body>
</html>
Absolute length
Absolute length units represents a physical measurement. They are useful
when the physical properties of the output medium are known, such as for
print layout.
• mm One millimeter.
• cm One centimeter (10 millimeters).
• in One inch (2.54 centimeters).
• pt One point (1/72nd of an inch). body {font-size: 18pt;
p {padding: 25px;
}
24
Percentage
The percentage CSS data type represents a percentage value. A
percentage consists of a number followed by the percentage sign %. There
is no space between the symbol and the number.
div {
width: 50%;
}
Many CSS properties (width, margin, padding, fontsize,..) can take percentage values
25
to define a size as relative to its parent object.
Length units
Unit Description
26
Length units
P{ width = 25%;
}
27
float property
• take an element out of normal flow and place it as far to the left or right of a containing
box as possible.
• Whenever you specify a float property on an element
Value purpose
left The box is floated to the left of the containing element, and the content of the
containing element flows to the right of it .
right The box is floated to the right of the containing element, and the content of
the containing element flows to the left of it .
none The box is not floated and remains where it would have been positioned in
normal flow
28 inherit The box takes the same property as its containing element .
float property
img{
width:auto;
float:left;
margin-right: 10px;
}
29
float property
<html>
<head>
<style>
float {
float: left;
font-size: 400%;
font-family: algerian, courier;
line-height: 40%;
}
</style>
</head>
<body>
<p><float>T</float>his is float testing.
his is float testing.his is float testing.
his is float testing.his is float testing.
his is float testing.</p>
30
</body>
</html>