In this tutorial we will be creating a 2-column web site using CSS.
First, we need to create the four (4) main containers that will be used as the "framework" for our web page.
<body>
<header>Header goes here...</header>
<div id="sidebar">Menu goes here...</div>
<div id="main_content">Main Content goes here...</div>
<footer>Footer goes here...</footer>
</body>
Now that we have created the four main containers, let's turn our attention on styling these containers with CSS using a style element.
SPECIAL NOTE: The background color for the sidebar and main_content divs are TEMPORARY. They are used so that you can SEE the structure of these div elements better.
<style>
header{
width: 960px;
height: 100px;
background-color: blue;
color: white;}
</style>
</head>
header{
width: 960px;
height: 100px;
background-color: blue;
color: white;}
#sidebar{
width: 200px;
height: 450px;
background-color:aqua;}
#sidebar{
width: 200px;
height: 450px;
background-color:aqua;}
#main_content{
width: 760px;
height: 450px;
background-color:aquamarine;}
#main_content{
width: 760px;
height: 450px;
background-color:aquamarine;}
footer{
width: 960px;
height: 50px;
background-color: blue;
color: white;}
Currently, the sidebar and main_content containers are not positioned as we would like it. We will float them so that they "RISE to the occasion" in the Design View.
#sidebar{
width: 200px;
height: 450px;
background-color:aqua;
float: left;}
#sidebar{
width: 200px;
height: 450px;
background-color: aqua;
float: left;
position: absolute;}
#sidebar{
width: 200px;
height: 450px;
background-color: aqua;
float: left;position: absolute;left: 618px;top: 411px;}
#main_content{
width: 760px;
height: 450px;
background-color:aquamarine;
float:left;}
Because the footer is hidden behind the sidebar and the main_content containers, we need to bring it back down to the bottom of the page. To do this, we will use the clear property which allows you to "clear" one or more columns that has been taken out of the normal flow of the document.
footer{width: 960px;
height: 50px;
background-color: blue;
color: white;
clear: both;}
Let's style the containers farther and add style rules for the text containers as well.
<style> body{
font-family:Arial;
}
header h1{
margin-left: 33%;
font-size: 40px;
padding-top: 30px;
}
#sidebar, #main_content, footer{
padding: 20px;
}
Let's see how we can "contain" the four (4) major containers by wrapping them with a div tag and giving it an ID of container.
<body>
<div id="container">
<header>
<h1>Cornelius' Portfolio</h1>
</header>
<div id="sidebar">Menu goes here...</div>
<div id="main_content">Main Content goes here...</div>
<footer>Footer goes here...</footer>
</div>
</body>
#container {width: 960px;
height: 600px;
background-color:beige;}
</style>
Because the padding increased the containers width and height, the corresponding containers have to be "adjusted" to compensate for the extra "padding."
#sidebar{width: 160px; // 40px subtracted
height: 450px;
background-color:aqua;
float: left;
}
#main_content{width: 720px; // 40px subtracted
height: 450px;
background-color:aquamarine;
float:left;
}
footer{width: 920px; // 40px subtracted
height: 10px; // 40px subtracted
background-color: blue;
color: white;
clear: both;}
#main_content{width: 700px; // Subtract 20 px
height: 450px;
background-color:aquamarine;
float:left;
margin-left: 20px; // Add a margin-left property to add 20 px to main_content container to push it off of the sidebar
}
Currently, the page is positioned at the top-left corner of the browser. We would like to have it centered on the page at the top vertically.
#container {width: 960px;
height: 600px;
background-color:beige;
margin: 0 auto;}
header{width: 960px;
height: 100px;
background-color: blue;
color: white;
border-bottom: 4px solid red;}
...
footer{width: 920px;
height: 10px;
background-color: blue;
color: white;
clear: both;
border-top: 4px solid red;}
footer{
width: 920px; height: 10px; background-color: blue; color: white; clear: both; border-top: 4px solid red; text-align:center;}
First, we will start by creating a simple list of paragraphs.
<body>
<p>Home</p>
<p>About Me</p>
<p>Portfolio</p>
<p>Contact Me</p>
<body>
<ul>NOTE: Notice how the "p" elements were converted to "li" elements and wrapped within a "ul" element.
<li>Home</li>
<li>About Me</li>
<li>Portfolio</li> <li>Contact Me</li>
</ul>
Next, let's convert the list to a list of links by applying an anchor tag <a> to the text in each of the list item (highlighted in bold).
NOTE: We could have named the first line home.html; however, it is best practice to name the "home page" index.html.
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about_me.html">Products</a></li>
<li><a href="portfolio.html">Services</a></li>
<li><a href="contact_me.html">Contact Us</a></li>
</ul>
<ul id="nav">
Now, let's address the links themselves by creating a contextual selector (e.g., #nav li) to target the anchor element (<a>) and the style the link themselves:
/* ------ Style list items ----- */
#nav li{
background-color: blue;
list-style-type: none;}
</style>
/* ------ STLYLE LINKS ---------- */
#nav a {
text-decoration: none;
color:white;
padding: 0 15px;
line-height: 2.1;
font-weight:bold;
border-bottom: 1px solid white;
text-align:center;}
</style>
/* ------ STLYLE LINKS ---------- */NOTES:
#nav a {
text-decoration: none;
color:white;
padding: 0 15px;
line-height: 2.1;
font-weight:bold;
border-bottom: 1px solid white;
text-align:center; display:block;}
</style>
Now, let's enhance our menu by creating a hover effect when the user moves the mouse of a menu item. To create a hover effect, we will apply a hover pseudo-class to the <a> tag inside the nav area
/* --------- STLYLE LINKS HOVER ---------- */
#nav a:hover {
background-color: #006;}
</style>
/* --------- STLYLE LINKS HOVER ---------- */
#nav a:hover {
background-color: #006;}
border-left: 5px solid red; }
</style>
/* ------ Style list items ----- */
#nav li{background-color: blue;
list-style-type: none;
border-radius: 30px;
margin-bottom: 3px;}
/* ------ STLYLE LINKS ---------- */
#nav a {
text-decoration: none;
color:white;
padding: 0 15px;
line-height: 2.1;
font-weight:bold;border-bottom: 1px solid white;
text-align:center;
display:block;}
/* --------- STLYLE LINKS HOVER ---------- */
#nav a:hover {
background-color: #006;border-left: 5px solid red;
border-radius: 30px;}
An "active" id can be add to a link to let the user know which page is currently active.
/* ---------- ADD ACTIVE LINK ---------- */ .active {background-color: #006; color: white;} </style>
<li id="active"><a href="index.html">Home</a></li>
Since the menu items will be shared by multiple pages in your site or used for another site, you can externalize it so that it can be shared among all of the other pages. In this way, if you need to make any changes, you would only have to update the external CSS file and the changes will be AUTOMATICALLY reflected. in all pages that it is attached to.
/* ------ Style list items ----- */
#nav li{background-color: blue;
list-style-type: none;
border-radius: 30px;
margin-bottom: 3px;
}
/* ------ STLYLE LINKS ---------- */
#nav a {
text-decoration: none;
color:white;
padding: 0 15px;
line-height: 2.1;
font-weight:bold;
border-bottom: 1px solid white;
text-align:center;
display:block;}
/* --------- STLYLE LINKS HOVER ---------- */
#nav a:hover {
background-color: #006;
/* border-left: 5px solid red;*/
border-radius: 30px;
}
/* -----Stylize ul element ------- */
#nav{
width:140px;}
/* ---------- ADD ACTIVE LINK ---------- */
#nav #active {
background-color: #006;
}
/* ---------- ADD ACTIVE LINK ---------- */
.active {background-color: #006; color: white;}
<link href="css/main.css" rel="stylesheet"/>
<style>
Now that the first page has been created. We will use it to create the other pages.