CSS Surrounding Element Properties
In this article, I show you how to determine the padding, border and margin of an HTML element. I use CSS to explain this.
Basics of CSS2 - Part 5
Introduction
This is part 5 of my series, Basics of CSS2. In this part of the series I talk about the
padding, border and margin of an element. In the previous part I talked about the CSS
Box, where these surrounding features where introduced. You should read that part
before reading this one.
Look at the BODY element of a typical web page critically; you would realize that it has
margin areas. It might also have borders, if the web site designer gave it borders
(however, this is hardly done for the BODY element). DIV elements, Paragraph
elements, inline elements (e.g. SPAN) all have padding, border and margin areas. With
many of the elements, in the default situation, the padding and/or border and/or margin
may not be present.
Note: If you cannot see the code or if you think anything is missing (broken link, image
absent, part of code missing), just contact me at forchatrans@yahoo.com. That is, contact
me for the slightest problem you have about what you are reading.
The Padding Property
You can determine the width of the padding area of an element. The following example
illustrates this for the BODY and a DIV element:
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
body {padding:100px}
div {padding:75px; width:50%; border:solid 2px blue}
This is the Body element with 100px padding.
This is the DIV element with 75px padding.
Try the above code. The padding width of the BODY element is 100px; that for the DIV
is 75px. I chose large numbers for emphasis. Right now, do not worry about the other
things you see in the DIV's CSS rule. The unit of pixel has been used. If we had used the
Percentage for the DIV, it would have been relative (measured with respect to) the width
of the containing element, BODY. Note: the BODY element does not really have a
containing element, while the other elements (DIV, Paragraph, SPAN, etc.) do.
So to give padding to an element, use the property name "padding", followed by a colon,
and then value and unit, in the CSS rule. If you do not give an element a padding value,
the browser chooses one for you. What the browser chooses is usually OK. The problem
is that different browsers may choose different values, for the same property and for the
same element.
The Border Property
To give the border property, you need the property name, border, followed by a colon and
then a three-in-one value. The three-in-one value consists of the border-style, the width of
the border and the color of the border. The individual values of the three-in-one are
separated by spaces.
The above code illustrates this. The DIV element has a blue border. In the three-in-one,
the border-style is solid, the border-width is 2px and the border-color is blue.
Border-styles and their values as given in the CSS2 specification are as follows:
none
No border. This value forces the computed value of 'border-width' to be '0'.
hidden
Same as 'none', except in terms of border conflict resolution for table elements (see later).
dotted
The border is a series of dots.
dashed
The border is a series of short line segments.
solid
The border is a single line segment.
double
The border is two solid lines. The sum of the two lines and the space between them
equals the value of 'border-width'.
groove
The border looks as though it were carved into the canvas (surface).
ridge
The opposite of 'grove': the border looks as though it were coming out of the canvas.
inset
The border makes the entire box look as though it were embedded in the canvas.
outset
The opposite of 'inset': the border makes the entire box look as though it were coming out
of the canvas.
Note: if you do not want a border, you will not use the three-in-one value. You will use
either the style value alone set to "none", or the width value alone, set to 0px.
The Margin Property
The margin is given in a similar way like padding. It could be something like
margin:3px
or
margin:0px
You use 0px when you do not want any margin. The following code shows a margin-
width of 10px for the BODY element. Try the code and in this case do not worry if the
result is not the way you expected; browser interpretation in this case is not very good.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
body {margin:10px; border: solid 2px black}
This is the Body element with a margin-width of 10px.
Properties and their values chosen by Browsers
If you do not give a padding and/or border and/or margin value, for any element, the
browser chooses one for you. The problem is that the value chosen for a particular
property and element differs with browsers.
That is it, for this part of the series. We continue in the next part.
Chrys
-
Electric Heating Pads Are Used For Different Ailments
| By ja_schmidt | in General
Electric heating pads are owned by many women today. At home, many electric heating pads are kept in the closet by ...
-
Why I made the switch to cloth menstrual pads
| By MamaBear | in General
My story of why I made the full switch to cloth menstrual pads. It was easier than I thought....
-
How to Choose the Best Sleeping Pad at the Best Price
| By patti80 | in Camping & Outdoor Skills
Are you in the market for a new sleeping pad and not know which one to choose? Do you need a light weight model, or...
-
Beauty Views: Neutrogena Microderm Cleaning Pads Illuminating
| By MellaView | in Skin care
How well do Neutrogena's Microderm Cleaning Pads Illuminating work? Are They Worth The Buy?...
-
Earth Therapeutics Orange Eye Pads
| By MellaView | in Skin care
How well do Earth Therapeutics Orange Eye Pads work? Are they worth buying?...
-
XA Transaction - Solution for Transaction More Than One Database | By H4d1 | in Programming
Have you ever think that it's too difficult for making database transaction in two different places (or databases) ...
-
Javascript functions for : trim, right trim, left trim, no Apostrophe, is Empty , is Digit , VarChar To Number , is integer , check Is Zero , Get Que | By xxris | in Programming
Javascript functions for : trim, right trim, left trim, no Apostrophe, is Empty , is Digit , VarChar To Number , i...
-
How to access and use a Window's command line | By MaxwellPayne | in Programming
Learn about the Window's command line in DOS and how to use it....
-
How to Learn to Program Your Computer | By dsj8760 | in Programming
This article is about learning to program a computer. It is a general article giving tips on how to learn about pro...
-
Jailbroken iPhones get RickRolled | By explorer | in Programming
First iPhone worm, attacks via SSH and does the classic rick roll gag on the user....
-
Database N-to-N Relationships | By Chrys | in Programming
In this part of the tutorial we look at one-to-one, one-to-many and many-to-many relationships....
-
Database Associations Overview | By Chrys | in Programming
In this part of the series, we look at Database Associations Overview....
-
Database Table Data Types | By Chrys | in Programming
In this part of the series we look at database Data Types....
-
Database Table Keys | By Chrys | in Programming
In this part of the series we look at what is known as a key....
-
Database Design | By Chrys | in Programming
A database is a set of related tables. This is part 1, division 1 of a series I have on database....







No comments yet.