Course Content
Introduction to HTML (Structure of the Web)
0/1
Introduction to CSS (Styling the Web)
Getting Started with CSS
0/2
Introduction to JavaScript (Interactivity on the Web)
Project Presentation and Review
0/1
Protected: Web Development Bootcamp: Master HTML, CSS, and JavaScript in 3 Weeks
About Lesson

1. Hyperlinks with <a> and Creating Navigation Menus in HTML

Hyperlinks are a vital part of any webpage, as they allow users to navigate between different pages or sections of a site. The <a> tag (anchor tag) is used to create hyperlinks in HTML. It can link to external websites, other pages within the same website, or specific sections of the current page.

In this section, we will also cover how to create navigation menus using links to organize and guide users through your website.


1. The <a> Tag: Creating Hyperlinks

The <a> tag is used to define hyperlinks. It requires the href attribute, which specifies the destination URL of the link.

Syntax:

<a href="URL">Link Text</a>
  • href (Hypertext Reference): This attribute holds the URL or the location the link points to.
  • Link Text: This is the clickable text that the user sees.

Example 1: External Link

To link to an external website:

<a href="https://www.example.com">Visit Example Website</a>
  • Clicking on this link will take the user to the external website “example.com”.

Example 2: Internal Link

To link to another page within the same website:

<a href="about.html">About Us</a>
  • Clicking this will take the user to the “about.html” page in the same folder.

Example 3: Anchor Link (Within the Same Page)

You can link to a specific section within the same page using an anchor link. This requires an id on the target element.

<a href="#contact">Go to Contact Section</a>

<!-- Later on the page -->
<div id="contact">
  <h2>Contact Us</h2>
  <!-- Content for the contact section -->
</div>
  • Clicking the link will scroll to the “Contact Us” section of the page.

2. Additional Attributes for <a>

The <a> tag has several other useful attributes that control how the link behaves:

1. target="_blank"

This opens the linked page in a new tab or window.

<a href="https://www.example.com" target="_blank">Visit Example Website</a>

2. title

The title attribute provides additional information that appears when the user hovers over the link (like a tooltip).

<a href="https://www.example.com" title="Click to visit Example Website">Visit Example Website</a>

3. rel

The rel attribute specifies the relationship between the current page and the linked page. It’s commonly used with target="_blank" to enhance security.

<a href="https://www.example.com" target="_blank" rel="noopener noreferrer">Visit Example Website</a>

3. Creating Navigation Menus

Navigation menus are essential for organizing the structure of a website and allowing users to easily navigate between different sections or pages. A simple navigation menu can be created using unordered lists (<ul>) and list items (<li>) with anchor (<a>) tags.

Basic Navigation Menu Example:

<nav>
  <ul>
    <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>
  • <nav>: A semantic HTML element used to define navigation links on a webpage.
  • <ul>: An unordered list is used to group the navigation items.
  • <li>: Each list item defines a link within the navigation menu.

4. Styling the Navigation Menu with CSS

You can style the navigation menu to look more professional and visually appealing. Here’s an example of how to style the basic navigation menu using CSS:

<head>
  <style>
    nav ul {
      list-style-type: none; /* Remove bullet points */
      margin: 0;
      padding: 0;
      display: flex; /* Use flexbox for horizontal layout */
    }

    nav li {
      margin-right: 20px; /* Add space between menu items */
    }

    nav a {
      text-decoration: none; /* Remove underline from links */
      color: #333; /* Text color */
      font-size: 18px;
      padding: 10px;
      border-radius: 5px;
    }

    nav a:hover {
      background-color: #ddd; /* Change background on hover */
    }
  </style>
</head>

<body>
  <nav>
    <ul>
      <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>
</body>

Explanation of the CSS:

  • display: flex: Used to lay out the navigation items in a horizontal row.
  • margin-right: 20px: Adds space between the list items.
  • text-decoration: none: Removes the underline from the anchor tags.
  • color: Sets the text color for the links.
  • hover effect: When a user hovers over a menu item, the background color changes.

5. Responsive Navigation Menu

You can also create a responsive navigation menu that adapts to different screen sizes. Here’s a simple example using media queries:

<head>
  <style>
    nav ul {
      list-style-type: none;
      margin: 0;
      padding: 0;
      display: flex;
    }

    nav li {
      margin-right: 20px;
    }

    nav a {
      text-decoration: none;
      color: #333;
    }

    /* Responsive design for smaller screens */
    @media screen and (max-width: 600px) {
      nav ul {
        flex-direction: column;
        align-items: center;
      }
      
      nav li {
        margin-bottom: 10px;
      }
    }
  </style>
</head>

<body>
  <nav>
    <ul>
      <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>
</body>
  • Media Query: The @media rule ensures that for screens smaller than 600px wide (such as mobile devices), the navigation menu will stack vertically instead of horizontally.

Summary:

  • <a>: Used to create hyperlinks to other pages, websites, or sections within the page.
  • Attributes:
    • href: Specifies the URL of the link.
    • target="_blank": Opens the link in a new tab.
    • title: Adds extra information shown on hover.
    • rel: Enhances security when opening external links in a new tab.
  • Navigation Menus: Created using <ul>, <li>, and <a>, and can be styled with CSS for better user experience.
  • Responsive Design: Using media queries, navigation menus can adjust their layout based on screen size.

 

2. Ordered (<ol>) and Unordered Lists (<ul>) in HTML

Lists are a fundamental part of organizing content on web pages. In HTML, we use ordered lists (<ol>) for content that has a specific order or sequence, and unordered lists (<ul>) for content that doesn’t require a specific order. Both list types contain list items, which are defined using the <li> tag.


1. Unordered Lists (<ul>)

An unordered list is used to group a collection of items without implying any specific order. Each list item is represented by the <li> tag.

Syntax:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>
  • <ul>: Defines the unordered list container.
  • <li>: Represents each list item within the unordered list.

By default, items in a <ul> list are marked with bullet points (•), but this can be customized using CSS.

Example:

<h3>Fruits</h3>
<ul>
  <li>Apple</li>
  <li>Banana</li>
  <li>Cherry</li>
  <li>Grapes</li>
</ul>

Output:

  • Apple
  • Banana
  • Cherry
  • Grapes

2. Ordered Lists (<ol>)

An ordered list is used when the order of the items is important, such as steps in a process or a ranking. The items in an ordered list are automatically numbered.

Syntax:

<ol>
  <li>Step 1</li>
  <li>Step 2</li>
  <li>Step 3</li>
</ol>
  • <ol>: Defines the ordered list container.
  • <li>: Represents each list item in the ordered list.

By default, items in an <ol> list are numbered sequentially (1, 2, 3, …), but this numbering can be customized with CSS or by using different attributes.

Example:

<h3>Steps to Make a Cup of Coffee</h3>
<ol>
  <li>Boil water</li>
  <li>Add coffee powder</li>
  <li>Pour water into the cup</li>
  <li>Stir and enjoy</li>
</ol>

Output:

  1. Boil water
  2. Add coffee powder
  3. Pour water into the cup
  4. Stir and enjoy

3. Nested Lists

You can also nest lists within each other, creating hierarchical structures. This can be done with both ordered and unordered lists.

Syntax for Nested Lists:

<ol>
  <li>Item 1
    <ul>
      <li>Sub-item 1</li>
      <li>Sub-item 2</li>
    </ul>
  </li>
  <li>Item 2</li>
</ol>

Example:

<h3>Tasks for the Day</h3>
<ol>
  <li>Morning
    <ul>
      <li>Eat breakfast</li>
      <li>Go for a walk</li>
    </ul>
  </li>
  <li>Afternoon
    <ul>
      <li>Complete work</li>
      <li>Have lunch</li>
    </ul>
  </li>
  <li>Evening
    <ul>
      <li>Relax</li>
      <li>Prepare for bed</li>
    </ul>
  </li>
</ol>

Output:

  1. Morning
    • Eat breakfast
    • Go for a walk
  2. Afternoon
    • Complete work
    • Have lunch
  3. Evening
    • Relax
    • Prepare for bed

4. Customizing List Styles with CSS

By default, unordered lists use bullets, and ordered lists use numbers. However, you can customize the appearance of both types of lists using CSS.

Example: Styling Unordered Lists

<style>
  ul {
    list-style-type: square; /* Changes bullet style to square */
  }
</style>

<ul>
  <li>Apple</li>
  <li>Banana</li>
  <li>Cherry</li>
</ul>

Example: Styling Ordered Lists

<style>
  ol {
    list-style-type: upper-roman; /* Changes numbers to Roman numerals (I, II, III, ...) */
  }
</style>

<ol>
  <li>First Step</li>
  <li>Second Step</li>
  <li>Third Step</li>
</ol>

Other list-style-type Values:

  • Unordered lists: disc, circle, square, none
  • Ordered lists: decimal, upper-alpha, lower-alpha, upper-roman, lower-roman

You can also remove the bullets or numbers altogether by setting list-style-type: none; in CSS.


5. Summary of Differences:

  • Unordered List (<ul>): Used for lists where the order of the items doesn’t matter (e.g., a list of fruits).
  • Ordered List (<ol>): Used for lists where the order of items is important (e.g., instructions or rankings).
  • Nested Lists: Lists can be nested inside each other to create hierarchies of items.
  • Customization: You can style the appearance of lists using the list-style-type property in CSS to change the bullets or numbers.

By using both ordered and unordered lists, you can easily organize information on your website.

 

3. Structuring Data with Tables: <table>, <tr>, <td>

HTML tables are used to display data in a tabular format, which is helpful for organizing information in rows and columns. The <table> element defines the table, and inside it, we use several other tags to structure the data.


1. The Basic Table Structure

A basic table in HTML consists of the following tags:

  • <table>: Defines the table itself.
  • <tr>: Represents a row in the table.
  • <td>: Defines a cell within a table row (this is where the actual data goes).

Syntax:

<table>
  <tr>
    <td>Data 1</td>
    <td>Data 2</td>
  </tr>
  <tr>
    <td>Data 3</td>
    <td>Data 4</td>
  </tr>
</table>
  • <table>: Container for the entire table.
  • <tr>: Represents a row of cells.
  • <td>: Represents the individual cells of the table where data is inserted.

2. Example of a Simple Table

Let’s consider a table that shows the names of people and their ages.

Example:

<table border="1">
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>John</td>
    <td>25</td>
  </tr>
  <tr>
    <td>Jane</td>
    <td>30</td>
  </tr>
  <tr>
    <td>Sam</td>
    <td>28</td>
  </tr>
</table>

Explanation:

  • <th>: The <th> tag is used to define table headers. By default, the text in <th> is bold and centered.
  • border="1": This attribute adds a border around the table. This is for basic styling, but CSS is generally used for better design.

Output:

Name Age
John 25
Jane 30
Sam 28

3. Table Headers with <th>

The <th> tag is used for defining header cells, which are typically bold and centered. You can use them within the first row of the table to represent column headers.

Example:

<table border="1">
  <tr>
    <th>Name</th>
    <th>Age</th>
    <th>City</th>
  </tr>
  <tr>
    <td>John</td>
    <td>25</td>
    <td>Nairobi</td>
  </tr>
  <tr>
    <td>Jane</td>
    <td>30</td>
    <td>Mombasa</td>
  </tr>
</table>

Output:

Name Age City
John 25 Nairobi
Jane 30 Mombasa

4. Table Styling with CSS

You can style tables using CSS to improve their appearance. Common customizations include adding borders, spacing, alignment, and background colors.

Example of a Styled Table:

<style>
  table {
    width: 100%;
    border-collapse: collapse;
  }
  th, td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: center;
  }
  th {
    background-color: #f2f2f2;
  }
  tr:nth-child(even) {
    background-color: #f9f9f9;
  }
</style>

<table>
  <tr>
    <th>Name</th>
    <th>Age</th>
    <th>City</th>
  </tr>
  <tr>
    <td>John</td>
    <td>25</td>
    <td>Nairobi</td>
  </tr>
  <tr>
    <td>Jane</td>
    <td>30</td>
    <td>Mombasa</td>
  </tr>
  <tr>
    <td>Sam</td>
    <td>28</td>
    <td>Kiambu</td>
  </tr>
</table>

Explanation:

  • border-collapse: collapse;: Combines adjacent borders into a single border for a cleaner look.
  • th, td: Adds padding and border styling to both header and data cells.
  • nth-child(even): Applies a background color to even-numbered rows for better readability.

5. Merging Cells with colspan and rowspan

Sometimes you may want a cell to span across multiple columns or rows. You can achieve this with the colspan and rowspan attributes.

  • colspan: Specifies the number of columns a cell should span.
  • rowspan: Specifies the number of rows a cell should span.

Example of colspan and rowspan:

<table border="1">
  <tr>
    <th>Name</th>
    <th>Age</th>
    <th>City</th>
  </tr>
  <tr>
    <td>John</td>
    <td colspan="2">Nairobi</td>
  </tr>
  <tr>
    <td rowspan="2">Jane</td>
    <td>30</td>
    <td>Mombasa</td>
  </tr>
  <tr>
    <td>28</td>
    <td>Kiambu</td>
  </tr>
</table>

Explanation:

  • The cell containing “Nairobi” spans across two columns using colspan="2".
  • The cell containing “Jane” spans two rows using rowspan="2".

6. Summary of Table Tags:

  • <table>: Defines the entire table.
  • <tr>: Represents a row in the table.
  • <td>: Represents a cell containing data within a row.
  • <th>: Represents a header cell, usually placed in the first row.
  • colspan: Allows a cell to span across multiple columns.
  • rowspan: Allows a cell to span across multiple rows.

7. Common Use Cases for Tables

  • Displaying financial reports.
  • Listing product features.
  • Showing timetable schedules.
  • Creating data comparison charts.

By using tables in HTML, you can organize data efficiently and present it in a structured, readable format.