HTML All Exercises & Assignments

All HTML exercises in one list: headings, text formatting, links, images, lists, tables, forms, a navigation menu and a page layout with semantic elements.

Write an HTML program to display hello world.

Description: Write an HTML program that displays “Hello, World!” on a web page.

Hint:

  • Use the basic page structure: doctype, html, head and body.
  • Put the text inside the body, in a heading or paragraph tag.

HTML program to display Hello World

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Hello World</title>
</head>
<body>
  <h1>Hello, World!</h1>
</body>
</html>

Output in the browser:

Output in the browser

Hello, World!

How it works

  • <!DOCTYPE html> tells the browser that this is an HTML5 page.
  • The <title> is shown on the browser tab.
  • The <h1> inside the body is the visible heading.

Learn the structure in HTML Page Structure.

Write a program to create a webpage to print values 1 to 5

Description: Create a web page that prints the values 1 to 5. Each value should be on a new line.

Hint:

  • Use one <p> tag for each value, or the <br> tag for a line break.

HTML program to print 1 to 5

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Numbers</title>
</head>
<body>
  <p>1</p>
  <p>2</p>
  <p>3</p>
  <p>4</p>
  <p>5</p>
</body>
</html>

Output in the browser:

Output in the browser

1

2

3

4

5

How it works

  • Every <p> tag is a paragraph, and the browser puts each paragraph on a new line.
  • You can also write 1<br>2<br>3<br>4<br>5 inside one paragraph. The <br> tag gives a line break.
  • If you want a numbered list, use <ol>. See HTML Lists.

Write a program to create a webpage to print your city name in red color.

Description: Create a web page that prints the name of your city in red colour.

Hint:

  • Use the style attribute with the CSS color property.
  • The old <font color="red"> tag is not valid in HTML5.

HTML program to print a city name in red

<!DOCTYPE html>
<html lang="en">
<head>
  <title>My City</title>
</head>
<body>
  <h1 style="color: red;">Delhi</h1>
</body>
</html>

Output in the browser:

Output in the browser

Delhi

How it works

  • The style attribute holds CSS for this element only.
  • color: red; changes the text colour to red. You can also use a hex code like #ff0000.

Learn more in HTML Styles.

Write a program to print a paragraph with different font and color.

Description: Create a web page with a paragraph of 4 sentences. Each sentence should have a different font and colour.

Hint:

  • Put the paragraph in a <p> tag.
  • Wrap each sentence in a <span> and use font-family and color in the style attribute.

HTML program for a paragraph with different font and colour

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Fonts and colours</title>
</head>
<body>
  <p>
    <span style="font-family: Arial; color: red;">This sentence is in Arial and red.</span>
    <span style="font-family: 'Times New Roman'; color: green;">This one is in Times New Roman and green.</span>
    <span style="font-family: Verdana; color: blue;">This one is in Verdana and blue.</span>
    <span style="font-family: 'Courier New'; color: purple;">The last one is in Courier New and purple.</span>
  </p>
</body>
</html>

Output in the browser:

Output in the browser

This sentence is in Arial and red. This one is in Times New Roman and green. This one is in Verdana and blue. The last one is in Courier New and purple.

How it works

  • <span> is an inline tag, so all sentences stay in the same paragraph.
  • Each span has its own font-family and color.
  • Old tutorials use the <font> tag for this. It is removed from HTML5, so we use CSS.

See HTML Div and Span.

Write an HTML program to create a page about yourself

Description: Create a web page with your name as the heading, a short paragraph about you, and a list of your hobbies.

Hint:

  • Use <h1> for the name, <p> for the paragraph and <ul> for the hobbies.

HTML program for a page about yourself

<!DOCTYPE html>
<html lang="en">
<head>
  <title>About Me</title>
</head>
<body>
  <h1>Robin Kumar</h1>
  <p>I am a student and I am learning web development.</p>
  <h2>My hobbies</h2>
  <ul>
    <li>Reading</li>
    <li>Cricket</li>
    <li>Music</li>
  </ul>
</body>
</html>

Output in the browser:

Output in the browser

Robin Kumar

I am a student and I am learning web development.

My hobbies

  • Reading
  • Cricket
  • Music

How it works

  • The h1 is the main heading and the h2 is a sub heading.
  • Each hobby is a <li> inside the <ul>.

Related: HTML Headings and HTML Lists.

Write an HTML program to display all six heading levels

Description: Create a web page that shows the headings from h1 to h6.

Hint:

  • Write one tag for each level, with a text like “Heading 1”.

HTML program for heading tags

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Headings</title>
</head>
<body>
  <h1>Heading 1</h1>
  <h2>Heading 2</h2>
  <h3>Heading 3</h3>
  <h4>Heading 4</h4>
  <h5>Heading 5</h5>
  <h6>Heading 6</h6>
</body>
</html>

Output in the browser:

Output in the browser

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

How it works

  • h1 is the biggest and most important heading, and h6 is the smallest.
  • Use only one h1 on a real page.

Read HTML Headings.

Write an HTML program for text formatting (bold, italic, subscript, superscript)

Description: Create a web page that shows important text, emphasised text, highlighted text, the formula of water (H2O) and the square of a number (5 squared).

Hint:

  • Use strong, em, mark, sub and sup tags.

HTML program for text formatting

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Text Formatting</title>
</head>
<body>
  <p>This is <strong>important</strong> text.</p>
  <p>This is <em>emphasised</em> text.</p>
  <p>This is <mark>highlighted</mark> text.</p>
  <p>The formula of water is H<sub>2</sub>O.</p>
  <p>5<sup>2</sup> is 25.</p>
  <p>Old price <del>$100</del>, new price <ins>$80</ins>.</p>
</body>
</html>

Output in the browser:

Output in the browser

This is important text.

This is emphasised text.

This is highlighted text.

The formula of water is H2O.

52 is 25.

Old price $100, new price $80.

How it works

  • strong shows bold and em shows italic text.
  • sub lowers the text and sup raises it.
  • del draws a line through the text and ins underlines it.

Read HTML Formatting.

Write an HTML program to create links (new tab and email)

Description: Create a web page with a link that opens a website in a new tab, a link to another page of your site, and a link to send an email.

Hint:

  • Use the <a> tag with href.
  • Use target="_blank" for a new tab and mailto: for an email link.

HTML program to create links

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Links</title>
</head>
<body>
  <p><a href="https://www.example.com" target="_blank" rel="noopener">Visit Example (new tab)</a></p>
  <p><a href="about.html">About us</a></p>
  <p><a href="mailto:info@example.com">Send an email</a></p>
</body>
</html>

Output in the browser:

How it works

  • target="_blank" opens the link in a new tab, and rel="noopener" keeps it safe.
  • about.html is a relative link to a file in the same folder.
  • mailto: opens the email app of the visitor.

Read HTML Links.

Write an HTML program to display an image

Description: Create a web page that shows an image of size 120 x 80 pixels, and another copy of the image that opens a website when clicked.

Hint:

  • Use the <img> tag with src, alt, width and height.
  • Put the image inside an <a> tag to make it a link.

HTML program to display an image

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Image</title>
</head>
<body>
  <img src="orange.png" alt="Orange sample picture" width="120" height="80">

  <a href="https://www.example.com">
    <img src="orange.png" alt="Go to Example" width="120" height="80">
  </a>
</body>
</html>

Output in the browser:

Output in the browser

How it works

  • src is the path of the picture. Keep the file in the same folder as the HTML file, or write the right path.
  • alt is shown if the picture cannot load.
  • Inside <a> the image becomes clickable.

Read HTML Images.

Write an HTML program to create an unordered list of fruits

Description: Create a web page with a bullet list of 5 fruits.

Hint:

  • Use <ul> for the list and one <li> for each fruit.

HTML program for an unordered list

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Fruits</title>
</head>
<body>
  <h2>My favourite fruits</h2>
  <ul>
    <li>Apple</li>
    <li>Banana</li>
    <li>Mango</li>
    <li>Orange</li>
    <li>Grapes</li>
  </ul>
</body>
</html>

Output in the browser:

Output in the browser

My favourite fruits

  • Apple
  • Banana
  • Mango
  • Orange
  • Grapes

How it works

  • ul means unordered list. The browser shows bullet points.
  • Every item must be inside an li tag.
  • Add style="list-style-type: square;" to the ul to change the bullet type.

Read HTML Lists.

Write an HTML program to create an ordered and a nested list

Description: Create a numbered list of three steps that starts from number 5. Then create a nested list of Fruits and Vegetables with two sub items each.

Hint:

  • Use <ol start="5"> to start from 5.
  • For a nested list, put a new <ul> inside an <li>.

HTML program for ordered and nested lists

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Lists</title>
</head>
<body>
  <ol start="5">
    <li>Open the editor</li>
    <li>Write the code</li>
    <li>Save the file</li>
  </ol>

  <ul>
    <li>Fruits
      <ul>
        <li>Apple</li>
        <li>Mango</li>
      </ul>
    </li>
    <li>Vegetables
      <ul>
        <li>Potato</li>
        <li>Tomato</li>
      </ul>
    </li>
  </ul>
</body>
</html>

Output in the browser:

Output in the browser
  1. Open the editor
  2. Write the code
  3. Save the file
  • Fruits
    • Apple
    • Mango
  • Vegetables
    • Potato
    • Tomato

How it works

  • The start attribute sets the first number of an ordered list.
  • The inner <ul> is inside the <li> of its parent item, so it shows as a sub list.

Read HTML Lists.

Write an HTML program to create a table of student marks

Description: Create a table that shows the name and marks of three students, with a header row and borders.

Hint:

  • Use th for the header cells and td for the data cells.
  • A table has no border by default, so add a border with CSS.

HTML program to create a table

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Marks</title>
  <style>
    table { border-collapse: collapse; }
    th, td { border: 1px solid #999; padding: 8px; }
  </style>
</head>
<body>
  <table>
    <tr><th>Name</th><th>Marks</th></tr>
    <tr><td>Robin</td><td>85</td></tr>
    <tr><td>Maria</td><td>92</td></tr>
    <tr><td>John</td><td>78</td></tr>
  </table>
</body>
</html>

Output in the browser:

Output in the browser
Name Marks
Robin 85
Maria 92
John 78

How it works

  • Each tr is a row. The first row has header cells (th), which are bold.
  • border-collapse: collapse joins the borders of neighbour cells into one line.

Read HTML Tables.

Write an HTML program to create a table with colspan and rowspan

Description: Create a small timetable with a heading cell that covers two columns, and one subject name that covers two rows.

Hint:

  • colspan merges cells in a row, and rowspan merges cells in a column.
  • Remove the cells that are covered by the merged cell.

HTML program for colspan and rowspan

<table border="1">
  <tr>
    <th colspan="2">Monday Timetable</th>
  </tr>
  <tr>
    <td rowspan="2">Morning</td>
    <td>Maths</td>
  </tr>
  <tr>
    <td>Science</td>
  </tr>
  <tr>
    <td>Afternoon</td>
    <td>Sports</td>
  </tr>
</table>

Output in the browser:

Output in the browser
Monday Timetable
Morning Maths
Science
Afternoon Sports

How it works

  • The heading cell has colspan="2", so it covers both columns.
  • “Morning” has rowspan="2", so it covers two rows. Because of this, the “Science” row has only one cell.
  • The border="1" attribute is used here only to keep the code short. In real pages use CSS borders.

Read Rowspan and Colspan.

Write an HTML program to create a registration form

Description: Create a registration form that takes name, email, password, gender, hobbies and country, and has a Register button.

Hint:

  • Use the form tag with method="post".
  • Connect every label to its field with for and id.
  • Radio buttons of the same group must have the same name.

HTML registration form

<form action="register.php" method="post">
  <p><label for="name">Name:</label>
     <input type="text" id="name" name="name" required></p>
  <p><label for="email">Email:</label>
     <input type="email" id="email" name="email" required></p>
  <p><label for="pass">Password:</label>
     <input type="password" id="pass" name="password" required></p>

  <p>Gender:
    <input type="radio" id="m" name="gender" value="male"><label for="m">Male</label>
    <input type="radio" id="f" name="gender" value="female"><label for="f">Female</label></p>

  <p>Hobbies:
    <input type="checkbox" id="h1" name="hobby" value="reading"><label for="h1">Reading</label>
    <input type="checkbox" id="h2" name="hobby" value="music"><label for="h2">Music</label></p>

  <p><label for="country">Country:</label>
    <select id="country" name="country">
      <option value="india">India</option>
      <option value="usa">USA</option>
    </select></p>

  <button type="submit">Register</button>
</form>

Output in the browser:

Output in the browser

Gender:

Hobbies:

How it works

  • The required attribute stops the form from being sent when the field is empty.
  • type="email" checks the email format, and type="password" hides the typed text.
  • The form data goes to register.php with the POST method. That file must be made with a server language like PHP.

Read HTML Forms and HTML Input Types.

Write an HTML program to create a contact form

Description: Create a contact form with name, email, subject and message fields, and a Send button.

Hint:

  • Use textarea for the long message.
  • Mark the important fields with required.

HTML contact form

<form action="contact.php" method="post">
  <p><label for="cname">Name:</label><br>
     <input type="text" id="cname" name="name" required></p>
  <p><label for="cemail">Email:</label><br>
     <input type="email" id="cemail" name="email" required></p>
  <p><label for="subject">Subject:</label><br>
     <input type="text" id="subject" name="subject"></p>
  <p><label for="message">Message:</label><br>
     <textarea id="message" name="message" rows="5" cols="40" required></textarea></p>
  <button type="submit">Send</button>
</form>

Output in the browser:

Output in the browser





How it works

  • <br> puts the field below its label.
  • rows and cols set the size of the textarea.
  • The server file contact.php reads the values and sends the email. A form alone cannot send an email.

Also see our HTML contact form template.

Write an HTML program to create a navigation menu

Description: Create a navigation menu with the links Home, About, Services and Contact, shown in one line.

Hint:

  • A menu is a list of links, so use ul, li and a inside a nav tag.
  • Use CSS to remove the bullets and show the items in one line.

HTML navigation menu

<nav>
  <ul class="menu">
    <li><a href="index.html">Home</a></li>
    <li><a href="about.html">About</a></li>
    <li><a href="services.html">Services</a></li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
</nav>

<style>
  .menu { list-style: none; padding: 0; }
  .menu li { display: inline-block; margin-right: 15px; }
</style>

Output in the browser:

Output in the browser

How it works

  • The nav tag tells that this block has navigation links.
  • list-style: none removes the bullets, and display: inline-block puts the items in a row.

Read HTML Semantic Elements.

Write an HTML program to create a page layout with semantic elements

Description: Create a web page layout with a header, a navigation bar, a main area with an article and a sidebar, and a footer.

Hint:

  • Use header, nav, main, article, aside and footer.

HTML page layout with semantic tags

<!DOCTYPE html>
<html lang="en">
<head>
  <title>My Blog</title>
</head>
<body>
  <header>
    <h1>My Blog</h1>
  </header>

  <nav>
    <a href="/">Home</a> | <a href="/about">About</a>
  </nav>

  <main>
    <article>
      <h2>Learn HTML</h2>
      <p>This is the main article.</p>
    </article>
    <aside>
      <h3>Related links</h3>
      <p>Links go here.</p>
    </aside>
  </main>

  <footer>
    <p>&copy; 2025 My Blog</p>
  </footer>
</body>
</html>

Output in the browser:

Output in the browser

My Blog

Learn HTML

This is the main article.

© 2025 My Blog

How it works

  • Each semantic tag tells the meaning of its part of the page.
  • Without CSS they all stay one below another. With CSS (Flexbox or Grid) you can put the aside next to the article.

Read HTML Semantic Elements. A div based version is in Basic HTML page layout using div tags.