At the end of this Tutorial you will be able to:
You can view a finished version of the web page you will create in this Tutorial by clicking the link below. The finished sample will open in a new tab of your web browser.
On a web page, every element can be considered as a rectangle or more simply as a ‘box’
An element could be a heading, text paragraph, image, video or whatever. HTML elements consist of some content inside HTML markup tags.
The following are all examples of HTML elements. They have both content and markup tags.
The words box model are used when describing the layout and visual design of HTML elements in a web page.
You can see an example of the box model in action by inspecting the home page from of AIB bank website. Notice how various page elements are grouped into rectangular blocks.
To sub-divide web page content into a layout of boxes, the HTML tag pair most commonly by web designers is the <div> ... </div> tag.
The above AIB home page contains 364 div elements.
Each of these grouping div elements and the smaller elements they contain follows the rules of the ‘box model’.
Your first step is to download the files you need for this and the next two Tutorials.
Display the sandbox-divs.html web page in your Chrome or Mozilla Firefox Developer Edition browser. It should look as shown below.
You can now delete the following two items from your websites folder:
You are now ready to work with your downloaded sample files.
For the <div> element in the web page, let’s add a selector, and a background-color property and value.
/* Div selector with properties and values */ div { background-color: deeppink; }
The <div> element now has a deep pink background colour. Notice that no other part of the web page is affected. Only the <div> and its content.
Now we can move on to explore the three important div properties: padding, border and margins.
Padding is the spacing between the content of an element and its four edges – top, bottom, left and right. You can think of padding as an element’s inside spacing.
Padding does not affect the spacing between an element and any other elements around it. See the examples below.
Padding is not just for div elements. You can also add padding around text and buttons.
div { background-color: deeppink; padding-top: 16px; padding-right: 16px; padding-bottom: 16px; padding-left: 16px; }
The <div> element in your web page now has some padding between its content and the four edges of the div.
In the above simple example, all four padding values have the same value of 16px. So you can rewrite the four padding style rules into one CSS shorthand rule as follows.
div { background-color: deeppink; padding: 16px; }
But sometimes padding values may be different on different sides of an element. For example copy-and-paste the following to your CSS stylesheet.
div { background-color: deeppink; padding-top: 12px; padding-right: 32px; padding-bottom: 14px; padding-left: 16px; }
That’s a lot of typing!
Is there a shorthand way of entering four different padding values for an element?
In CSS shorthand format, you could rewrite these four style rules on a single line as follows.
padding: 12px 32px 14px 16px;
But which one these four values refers for the top edge of the element? Or for the left edge?
Web designers rely on a memory trick or mnemonic to help them remember which edges of an element are set by four padding values written on a single line.
The mnemonic is the word TROUBLE.
Or:
T(op), R(ight), OU B(ottom) and L(eft) E.
In your stylesheet, replace any text you have entered for the div selector with the following, more simple values. And then save the file.
div { background-color: deeppink; padding: 16px; }
Next, let’s explore the border property of a <div> element.
Optionally, you can add borders to a <div> or other element. A border has three sub-properties as follows:
div { background-color: deeppink; padding: 12px; border-style: solid; border-color: green; border-top-width: 4px; border-right-width: 4px; border-bottom-width: 4px; border-left-width: 4px; }
div { background-color: deeppink; padding: 12px; border-style: solid; border-color: green; border-width: 4px; }
div { padding: 12px; border: 4px solid green; }
Consider the following example, where you give a box a fixed width of 300px. The box also has a padding on all four sides of 10px and a border width of 2px.
How wide is the box?
By default, the web browser will calculate and display the box width as follows:
300px + two sides of padding (20px) + two sides of borders (4px) = 324px
To ensure that the width of a box is exactly as you set it, include this property and setting in the web browser resets at the top of your CSS stylesheet file.
box-sizing: border-box;
Margins are the spacing between an element and other elements around it. You can think of margin as an element’s outside spacing.
The greater the margin values of an element, the further it ‘pushes’ or ‘shoves’ other elements away from it. If div elements on the same web page had no margins, they would be no space between them. See the example below.
Let’s add some margins to our sample div element.
div { background-color: deeppink; margin: 32px 0 32px 0; padding: 12px; border: solid 4px green; }In the above example, the top and bottom margins each have a value of 32px. And the left and right margins are zero. As a general rule web designing web pages with <div> elements, you will most commonly set bottom margins, then top margins and rarely left and right margins.
Unlike padding values and border width values, margins can have negative values.
For example, copy-and-paste the following to line to the bottom of the div declaration block in your stylesheet and save the file.
margin-left: -600px;
View the result in your web browser. It should look similar to that shown below.
You can now remove this negative margin.
Next, add the following new line to the bottom of the h1 declaration in your stylesheet and save the file.
margin-top: -120px;
Your web page should now look as follows.
You will find negative margin values a useful feature when designing the layout of web pages.
You can now remove this negative margin property and value, and resave your stylesheet.
Modern web pages typically have many dozens of <div> elements within them.
To design such modern web pages, you need to assign what are called classes to your <div> elements.
In effect, a <div> with a class is a version of a <div>.
Other HTML elements can have classes too.
You assign classes to <div> element in a web page by giving them class names. Below you can see some examples below of class names assigned to opening <div> tags in an HTML file.
Here are the rules about class names:
Next, you will apply styles to <div> elements with different class names.
<div class="bono"> <h3>Bono</h3> <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eius corporis obcaecati perferendis fuga quidem ad alias debitis excepturi ipsa laudantium sapiente.</p> </div> <div class="edge"> <h3>Edge</h3> <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eius corporis obcaecati perferendis fuga quidem ad alias debitis excepturi ipsa laudantium sapiente.</p> </div> <div class="adam"> <h3>Adam</h3> <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eius corporis obcaecati perferendis fuga quidem ad alias debitis excepturi ipsa laudantium sapiente.</p> </div> <div class="larry"> <h3>Larry</h3> <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eius corporis obcaecati perferendis fuga quidem ad alias debitis excepturi ipsa laudantium sapiente.</p> </div>
div.bono { background-color: #4081ec; padding: 12px; margin-bottom: 40px; width: 220px } div.edge { background-color: #ffbb33; padding: 12px; margin-bottom: 40px; width: 220px } div.adam { background-color: #f73345; padding: 12px; margin-bottom: 40px; width: 220px } div.larry { background-color: #e0e0e0; padding: 12px; margin-bottom: 40px; width: 220px }
.bono h3, .bono p, .adam h3, .adam p { color: #fff } .edge h3, .edge p, .larry h3, .larry p { color: #000 }
Click sandbox-divs.html to view a finished sample of this web page in a new tab of your web browser.
In the box model, every HTML element can be viewed as containing the following items:
For padding, borders and margins, you can use the same value for all four box edges, or assign different values to each individual edge.
Your web page is now published on GitHub at a web address similar to the following, where username is the username you have chosen for your GitHub account:
https://username.github.io/websites/divs-sandbox.html
It may take a few minutes for your uploaded files to appear on GitHub.
Introduction to the CSS basic box model
From the Mozilla Developer Network
CSS box model
From Interneting is Hard
CSS 101: The box model
by Nesha Zoric on dev.to
The CSS box model: How to use it correctly
by Nick Schäferhoff on Torque Mag
Video: Learn CSS box model in eight minutes
From Web Dev Simplified
Video: CSS box model tutorial
From W3Schools
Video: The CSS box model – margin, borders and padding explained
By Kevin Powell
Return to Contents.