Monday, August 13, 2012

HTML Layouts


Web page layout is very important to make your website look good.
Design your webpage layout very carefully.


Try it Yourself - Examples

Web page layout using <div> elements
How to add layout using <div> elements.

Web page layout using <table> elements
How to add layout using <table> elements.


Website Layouts

Most websites have put their content in multiple columns (formatted like a magazine or newspaper).
Multiple columns are created by using <div> or <table> elements. CSS are used to position elements, or to create backgrounds or colorful look for the pages.
lampEven though it is possible to create nice layouts with HTML tables, tables were designed for presenting tabular data - NOT as a layout tool!.


HTML Layouts - Using <div> Elements

The div element is a block level element used for grouping HTML elements.
The following example uses five div elements to create a multiple column layout, creating the same result as in the previous example:

Example

<!DOCTYPE html>
<html>
<body>

<div id="container" style="width:500px">

<div id="header" style="background-color:#FFA500;">
<h1 style="margin-bottom:0;">Main Title of Web Page</h1></div>

<div id="menu" style="background-color:#FFD700;height:200px;width:100px;float:left;">
<b>Menu</b><br />
HTML<br />
CSS<br />
JavaScript</div>

<div id="content" style="background-color:#EEEEEE;height:200px;width:400px;float:left;">
Content goes here</div>

<div id="footer" style="background-color:#FFA500;clear:both;text-align:center;">
Copyright © W3Schools.com</div>

</div>

</body>
</html>

Try it yourself »

The HTML code above will produce the following result:

Main Title of Web Page

Menu
HTML
CSS
JavaScript
Content goes here
Copyright © W3Schools.com


HTML Layouts - Using Tables

A simple way of creating layouts is by using the HTML <table> tag.
Multiple columns are created by using <div> or <table> elements. CSS are used to position elements, or to create backgrounds or colorful look for the pages.
lampUsing tables is not the correct use of the <table> element. The purpose of the <table> element is to display tabular data.
The following example uses a table with 3 rows and 2 columns - the first and last row spans both columns using the colspan attribute:

Example

<!DOCTYPE html>
<html>
<body>

<table width="500" border="0">
<tr>
<td colspan="2" style="background-color:#FFA500;">
<h1>Main Title of Web Page</h1>
</td>
</tr>

<tr valign="top">
<td style="background-color:#FFD700;width:100px;text-align:top;">
<b>Menu</b><br />
HTML<br />
CSS<br />
JavaScript
</td>
<td style="background-color:#EEEEEE;height:200px;width:400px;text-align:top;">
Content goes here</td>
</tr>

<tr>
<td colspan="2" style="background-color:#FFA500;text-align:center;">
Copyright © W3Schools.com</td>
</tr>
</table>

</body>
</html>

Try it yourself »

The HTML code above will produce the following result:

Main Title of Web Page

Menu
HTML
CSS
JavaScript
Content goes here
Copyright © W3Schools.com


HTML Layout - Useful Tips

Tip: The biggest advantage of using CSS is that, if you place the CSS code in an external style sheet, your site becomes MUCH EASIER to maintain. You can change the layout of all your pages by editing one file. To learn more about CSS, study our CSS tutorial.

Tip: Because advanced layouts take time to create, a quicker option is to use a template. Search Google for free website templates (these are pre-built website layouts you can use and customize).

HTML Layout Tags

TagDescription
<div>Defines a section in a document
<span>Defines a section in a document

HTML Blocks


HTML elements can be grouped together with <div> and <span>

HTML Block Elements

Most HTML elements are defined as block level elements or as inline elements.
Block level elements normally start (and end) with a new line when displayed in a browser.
Examples: <h1>, <p>, <ul>, <table>


HTML Inline Elements

Inline elements are normally displayed without starting a new line.
Examples: <b>, <td>, <a>, <img>


The HTML <div> Element

The HTML <div> element is a block level element that can be used as a container for grouping other HTML elements.

The <div> element has no special meaning. Except that, because it is a block level element, the browser will display a line break before and after it.
When used together with CSS, the <div> element can be used to set style attributes to large blocks of content.

Another common use of the <div> element, is for document layout. It replaces the "old way" of defining layout using tables. Using tables is not the correct use of the <table> element. The purpose of the <table> element is to display tabular data.


The HTML <span> Element

The HTML <span> element is an inline element that can be used as a container for text.
The <span> element has no special meaning.

When used together with CSS, the <span> element can be used to set style attributes to parts of the text.

HTML Grouping Tags

TagDescription
<div>Defines a div
<span>Defines a span

HTML Lists


The most common HTML lists are ordered and unordered lists:

HTML Lists

An ordered list:

  1. The first list item
  2. The second list item
  3. The third list item

An unordered list:

  • List item
  • List item
  • List item

Try-It-Yourself Examples

Unordered list
How to create an unordered list in an HTML document.

Ordered list
How to create an ordered list in an HTML document.
(You can find more examples at the bottom of this page).

HTML Unordered Lists

An unordered list starts with the <ul> tag. Each list item starts wit
h the <li> tag.
The list items are marked with bullets (typically small black circles).
<ul>
<li>Coffee</li>
<li>Milk</li>
</ul>

How the HTML code above looks in a browser:
  • Coffee
  • Milk

HTML Ordered Lists

An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.
The list items are marked with numbers.

<ol>
<li>Coffee</li>
<li>Milk</li>
</ol>

How the HTML code above looks in a browser:
  1. Coffee
  2. Milk

HTML Definition Lists

A definition list is a list of items, with a description of each item.
The <dl> tag defines a definition list.

The <dl> tag is used in conjunction with <dt> (defines the item in the list) and <dd> (describes the item in the list):

<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>

How the HTML code above looks in a browser:
Coffee
- black hot drink
Milk
- white cold drink

Basic Notes - Useful Tips

Tip: Inside a list item you can put text, line breaks, images, links, other lists, etc.

More Examples

Different types of ordered lists
Demonstrates different types of ordered lists.

Different types of unordered lists
Demonstrates different types of unordered lists.

Nested list
Demonstrates how you can nest lists.

Nested list 2
Demonstrates a more complicated nested list.

Definition list
Demonstrates a definition list.

HTML List Tags

TagDescription
<ol>Defines an ordered list
<ul>Defines an unordered list
<li>Defines a list item
<dl>Defines a definition list
<dt>Defines an item in a definition list
<dd>Defines a description of an item in a definition list

HTML Tables


HTML Tables

Apples44%
Bananas23%
Oranges13%
Other10%


Try it Yourself - Examples
Tables
How to create tables in an HTML document.

Table borders
How to specify different table borders.
(You can find more examples at the bottom of this page).

HTML Tables

Tables are defined with the <table> tag.
A table is divided into rows (with the <tr> tag), and each row is divided into data cells (with the <td> tag). td stands for "table data," and holds the content of a data cell. A <td> tag can contain text, links, images, lists, forms, other tables, etc.

Table Example

<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
How the HTML code above looks in a browser:

row 1, cell 1row 1, cell 2
row 2, cell 1row 2, cell 2


HTML Tables and the Border Attribute

If you do not specify a border attribute, the table will be displayed without borders. Sometimes this can be useful, but most of the time, we want the borders to show.

To display a table with borders, specify the border attribute:

<table border="1">
<tr>
<td>Row 1, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>


HTML Table Headers

Header information in a table are defined with the <th> tag.
All major browsers display the text in the <th> element as bold and centered.

<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>

How the HTML code above looks in your browser:

Header 1Header 2
row 1, cell 1row 1, cell 2
row 2, cell 1row 2, cell 2


More Examples

Tables without borders
How to create tables without borders.

Table headers
How to create table headers.

Table with a caption
How to add a caption to a table.

Table cells that span more than one row/column
How to define table cells that span more than one row or one column.

Tags inside a table
How to display elements inside other elements.

Cell padding
How to use cellpadding to create more white space between the cell content and its borders.

Cell spacing
How to use cellspacing to increase the distance between the cells.

The frame attribute
How to use the "frame" attribute to control the borders around the table.

HTML Table Tags

TagDescription
<table>Defines a table
<th>Defines a table header
<tr>Defines a table row
<td>Defines a table cell
<caption>Defines a table caption
<colgroup>Defines a group of columns in a table, for formatting
<col />Defines attribute values for one or more columns in a table
<thead>Groups the header content in a table
<tbody>Groups the body content in a table
<tfoot>Groups the footer content in a table