HTML Lists

« Previous

Next Chapter »

 

HTML can have Unordered Lists, Ordered Lists, or Description Lists:

 

Unordered HTML List

  • The first item
  • The second item
  • The third item
  • The fourth item

Ordered HTML List

  1. The first item
  2. The second item
  3. The third item
  4. The fourth item

HTML Description List

The first item

Description of item

The second item

Description of item

 

 

 

Unordered HTML Lists

An unordered list starts with the <ul> tag. Each list item starts with the <li> tag.

The list items will be marked with bullets (small black circles).

Unordered List:

<ul>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ul>


Try it Yourself »

 

 

Unordered HTML Lists - The Style Attribute

A style attribute can be added to an unordered list, to define the style of the marker:

Style

Description

list-style-type:disc

The list items will be marked with bullets (default)

list-style-type:circle

The list items will be marked with circles

list-style-type:square

The list items will be marked with squares

list-style-type:none

The list items will not be marked

 

Disc:

<ul style="list-style-type:disc">
  <li>Coffee</li>
  <li>Tea
  <li>Milk</li>
</ul>


Try it Yourself »

 

Circle:

<ul style="list-style-type:circle">
  <li>Coffee</li>
  <li>Tea
  <li>Milk</li>
</ul>


Try it Yourself »

 

Square:

<ul style="list-style-type:square">
  <li>Coffee</li>
  <li>Tea
  <li>Milk</li>
</ul>


Try it Yourself »

 

None:

<ul style="list-style-type:none">
  <li>Coffee</li>
  <li>Tea
  <li>Milk</li>
</ul>


Try it Yourself »

 

 

Using a type attribute <ul type="disc">, instead of <ul style="list-style-type:disc">, also works.
But in HTML5, the type attribute is not valid in unordered lists, only in ordered list.

 

Ordered HTML Lists

An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.

The list items will be marked with numbers.

Ordered List:

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


Try it Yourself »

 

 

Ordered HTML Lists - The Type Attribute

A type attribute can be added to an ordered list, to define the type of the marker:

Type

Description

type="1"

The list items will be numbered with numbers (default)

type="A"

The list items will be numbered with uppercase letters

type="a"

The list items will be numbered with lowercase letters

type="I"

The list items will be numbered with uppercase roman numbers

type="i"

The list items will be numbered with lowercase roman numbers

 

Numbers:

<ol type="1">
  <li>Coffee</li>
  <li>Tea
  <li>Milk</li>
</ol>


Try it Yourself »

 

Upper Case:

<ol type="A">
  <li>Coffee</li>
  <li>Tea
  <li>Milk</li>
</ol>


Try it Yourself »

 

Lower Case:

<ol type="a">
  <li>Coffee</li>
  <li>Tea
  <li>Milk</li>
</ol>


Try it Yourself »

 

Roman Upper Case:

<ol type="I">
  <li>Coffee</li>
  <li>Tea
  <li>Milk</li>
</ol>


Try it Yourself »

 

Roman Lower Case:

<ol type="i">
  <li>Coffee</li>
  <li>Tea
  <li>Milk</li>
</ol>


Try it Yourself »

 

 

HTML Description Lists

A description list, is a list of terms, with a description of each term.

The <dl> tag defines a description list.

The <dt> tag defines the term (name), and the <dd> tag defines the data (description).

Description List:

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


Try it Yourself »

 

 

Nested HTML Lists

List can be nested (lists inside lists).

Nested Lists:

<ul>
  <li>Coffee</li>
  <li>Tea
    <ul>
      <li>Black tea</li>
      <li>Green tea</li>
    </ul>
  </li>
  <li>Milk</li>
</ul>


Try it Yourself »

 

List items can contain new list, and other HTML elements, like images and links, etc.

 

Horizontal Lists

HTML lists can be styled in many different ways with CSS.

One popular way, is to style a list to display horizontally:

Horizontal List:

<!DOCTYPE html>
<html>

<head>
<style>
ul#menu li {
    display:inline;
}
</style>
</head>

<body>

<h2>Horizontal List</h2>

<ul id="menu">
  <li>Apples</li>
  <li>Bananas</li>
  <li>Lemons</li>
  <li>Oranges</li>
</ul> 

</body>
</html>


Try it Yourself »

With a little extra style, you can make it look like a menu:

 

 

 

New Style:

ul#menu {
    padding: 0;
}

ul#menu li {
    display: inline;
}

ul#menu li a {
    background-color: black;
    color: white;
    padding: 10px 20px;
    text-decoration: none;
    border-radius: 4px 4px 0 0;
}

ul#menu li a:hover {
    background-color: orange;
}


Try it Yourself »

 

 

Chapter Summary

  • Use the HTML <ul> element to define an unordered list
  • Use the HTML style attribute to define the bullet style
  • Use the HTML <ol> element to define an ordered list
  • Use the HTML type attribute to define the numbering type
  • Use the HTML <li> element to define a list item
  • Use the HTML <dl> element to define a description list
  • Use the HTML <dt> element to define the description term
  • Use the HTML <dd> element to define the description data
  • Lists can be nested inside lists
  • List items can contain other HTML elements
  • Use the CSS property display:inline to display a list horizontally

 

 

Try-It-Yourself Summary

An Unordered HTML List

An Unordered HTML List with Disc Bullets

An Unordered HTML List with Circle Bullets

An Unordered HTML List with Square Bullets

An Unordered HTML List Without Bullets

An Ordered HTML List

An Ordered HTML List with Numbers

An Ordered HTML List with Letters

An Ordered HTML List with Lowercase Letters

An Ordered HTML List with Roman Numbers

An Ordered HTML List with Lowercase Roman Numbers

A Description List

A Nested HTML List

A Nested HTML List 2

A Horizontal List

A Horizontal Menu List

 

Test Yourself Exercises

Exercise 1 »    Exercise 2 »    Exercise 3 »    Exercise 4 »    Exercise 5 »    Exercise 6 »

 

HTML List Tags

Tag

Description

<ul>

Defines an unordered list

<ol>

Defines an ordered list

<li>

Defines a list item

<dl>

Defines a description list

<dt>

Defines the term in a description list

<dd>

Defines the description in a description list

 

 

HTML Block Elements

« Previous

Next Chapter »

 

London

London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.

Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.

Example

<div style="background-color:black; color:white; margin:20px; padding:20px;">

<h2>London</h2>

<p>
London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.
</p>

</div>


Try it Yourself »

 

 

HTML Block Elements and Inline Elements

Most HTML elements are defined as block level elements or inline elements.

Block level elements normally start (and end) with a new line, when displayed in a browser.

Examples: <h1>, <p>, <ul>, <table>

Inline elements are normally displayed without line breaks.

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 other HTML elements.

The <div> element has no special meaning. It has no required attributes, but style and class are common.

Because it is a block level element, the browser will display line breaks before and after it.

When used together with CSS, the <div> element can be used to style blocks of content.

 

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. It has no required attributes, but style and class are common.

Unlike <div>, which is formatted with line breaks, the <span> element does not have any automatic formatting.

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

Example

<h1>My <span style="color:red">Important</span>Heading</h1>


Try it Yourself »

 

 

HTML Grouping Tags

Tag

Description

<div>

Defines a section in a document (block-level)

<span>

Defines a section in a document (inline)

 

 

HTML Classes

« Previous

Next Chapter »

 

Classing HTML elements, makes it possible to define CSS styles for classes of elements.

Equal styles for equal classes, or different styles for different classes.

 

London

London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.

Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.

Example

<!DOCTYPE html>
<html>
<head>
<style>
.cities {
    background-color:black;
    color:white;
    margin:20px;
    padding:20px;

</style>
</head>

<body>

<div class="cities">
<h2>London</h2>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.
</p>
</div> 

</body>
</html>


Try it Yourself »

 

 

Classing Block Elements

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

Classing <div> elements, makes it possible to define equal styles for equal <div> elements:

 

London

London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.

Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.

Paris

Paris is the capital and most populous city of France.

Situated on the Seine River, it is at the heart of the Île-de-France region, also known as the région parisienne.

Within its metropolitan area is one of the largest population centers in Europe, with over 12 million inhabitants.

Tokyo

Tokyo is the capital of Japan, the center of the Greater Tokyo Area, and the most populous metropolitan area in the world.

It is the seat of the Japanese government and the Imperial Palace, and the home of the Japanese Imperial Family.

The Tokyo prefecture is part of the world's most populous metropolitan area with 38 million people and the world's largest urban economy.

Example

<!DOCTYPE html>
<html>
<head>
<style>
.cities {
    background-color:black;
    color:white;
    margin:20px;
    padding:20px;

</style>
</head>

<body>

<div class="cities">
<h2>London</h2>
<p>London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
</div>

<div class="cities">
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</div

<div class="cities">
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
and the most populous metropolitan area in the world.</p>
</div>

</body>
</html>


Try it Yourself »

 

 

Classing Inline Elements

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

Classing <span> elements makes it possible to design equal styles for equal <span> elements.

Example

<!DOCTYPE html>
<html>
<head>
<style>
  span.red {color:red;}
</style>
</head>
<body>

<h1>My <span class="red">Important</span> Heading</h1>

</body>
</html>


Try it Yourself »

 

 

 

 

 

 

 

END

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

HTML Layouts

« Previous

Next Chapter »

 

Websites often display content in multiple columns (like a magazine or newspaper).

 

City Gallery

London
Paris
Tokio

London

London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.

Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.

Copyright © W3Schools.com

 

 

HTML Layout Using <div> Elements

 

The <div> element is often used as a layout tool, because it can easily be positioned with CSS.

This example uses 4 <div> elements to create a multiple column layout:

Example

<body>

<div id="header">
<h1>City Gallery</h1>
</div>

<div id="nav">
London<br>
Paris<br>
Tokyo<br>
</div>

<div id="section"">
<h1>London</h1>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</p>
<p>
Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.
</p>
</div>

<div id="footer">
Copyright © W3Schools.com
</div>

</body>


Try it yourself »

 

The CSS:

<style>
#header {
    background-color:black;
    color:white;
    text-align:center;
    padding:5px;
}
#nav {
    line-height:30px;
    background-color:#eeeeee;
    height:300px;
    width:100px;
    float:left;
    padding:5px; 
}
#section {
    width:350px;
    float:left;
    padding:10px; 
}
#footer {
    background-color:black;
    color:white;
    clear:both;
    text-align:center;
    padding:5px; 
}
</style>

 

 

Website Layout Using HTML5

HTML5 offers new semantic elements that define different parts of a web page:

 

header

Defines a header for a document or a section

nav

Defines a container for navigation links

section

Defines a section in a document

article

Defines an independent self-contained article

aside

Defines content aside from the content (like a sidebar)

footer

Defines a footer for a document or a section

details

Defines additional details

summary

Defines a heading for the details element

 

This example uses <header>, <nav>, <section>, and <footer> to create a multiple column layout:

Example

<body>

<header>
<h1>City Gallery</h1>
</header>

<nav>
London<br>
Paris<br>
Tokyo<br>
</nav>

<section>
<h1>London</h1>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</p>
<p>
Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.
</p>
</section>

<footer>
Copyright © W3Schools.com
</footer>

</body>


Try it yourself »

 

The CSS

<style>
header {
    background-color:black;
    color:white;
    text-align:center;
    padding:5px; 
}
nav {
    line-height:30px;
    background-color:#eeeeee;
    height:300px;
    width:100px;
    float:left;
    padding:5px; 
}
section {
    width:350px;
    float:left;
    padding:10px; 
}
footer {
    background-color:black;
    color:white;
    clear:both;
    text-align:center;
    padding:5px; 
}

 

 

HTML Layout Using Tables

 

The <table> element was not designed to be a layout tool.
The purpose of the <table> element is to display tabular data.

Layout can be achieved using the <table> element, because table elements can be styled with CSS:

Example

<body>

<table class="lamp">
<tr>
  <th>
    <img src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px">
  </th>
  <td>
    The table element was not designed to be a layout tool.
  </td>
</tr>
</table>

</body>


Try it yourself »

 

The CSS

<style>
table.lamp {
    width:100%;
    border:1px solid #d4d4d4;
}
table.lamp th, td {
    padding:10px;
}
table.lamp td {
    width:40px;
}
</style>

 

 

 

Try it Yourself - Examples

Layout using <div> elements
How to add layout using <div> elements.

Layout using semantic elements
How to add layout using <div> elements.

Layout using <table> elements
How to add layout using <table> elements.

 

 

HTMLForms and Input

芦 Previous

Next Chapter 

 

HTML Forms are used to select different kinds of user input.

 

 

Try it Yourself - Examples

Create text fields
How to create text fields. The user can write text in a text field.

Create password field
How to create a password field.

(You can find more examples at the bottom of this page)