Courses / Application of ICT (GEN-103) – BS 1st Semester

Week 17 Theory – Hyperlinks, Images, Colours, Frames and Tables in HTML

Week 17 – HTML Links, Images, Colours, Frames and Tables · 80 min read · 37 MCQs to check yourself

Hyperlinks – the anchor tag <a>

A hyperlink connects one page to another page, a section of the same page, a file or an email address. It is created with the anchor tag <a> and its href (hypertext reference) attribute.

<a href="https://www.hec.gov.pk">Visit HEC</a>

Useful attributes

Attribute Use Example
href Destination address href="about.html"
target="_blank" Open link in a new tab/window <a href="..." target="_blank">
target="_self" Same tab (default)
title Tooltip shown on hover title="Go to home page"

Types of links

<!-- Link to another website -->
<a href="https://www.w3schools.com" target="_blank">W3Schools</a>

<!-- Link to another page of the same site -->
<a href="page2.html">Next page</a>

<!-- Link to a section of the same page (bookmark / internal link) -->
<h2 id="fees">Fee Structure</h2>
<a href="#fees">Go to Fee Structure</a>

<!-- Email link -->
<a href="mailto:info@university.edu.pk">Email us</a>

<!-- Download / file link -->
<a href="outline.pdf" download>Download course outline</a>

<!-- Image as a link -->
<a href="index.html"><img src="logo.png" alt="Home"></a>

Link colours can be set in the body tag (older HTML): link (unvisited, default blue), vlink (visited, default purple), alink (active, at the moment of clicking, default red):

<body link="blue" vlink="purple" alink="red">

Adding images – the <img> tag

<img> is an empty tag that inserts an image.

<img src="campus.jpg" alt="University campus" width="400" height="250" border="2" align="right">
Attribute Purpose
src Source – path/URL of the image file (required)
alt Alternative text shown if the image cannot load; read by screen readers; helps SEO
width, height Size in pixels (or %)
border Border thickness around the image
align Position: left, right, top, middle, bottom (text wraps around)
title Tooltip on hover
hspace, vspace Horizontal/vertical space around the image

Common image formats for the Web: JPG/JPEG (photographs), PNG (transparent graphics, logos), GIF (simple animations), SVG (vector graphics), WebP (modern compressed format).

Backgrounds

<!-- Background colour -->
<body bgcolor="lightblue">

<!-- Background image (tiles/repeats by default) -->
<body background="pattern.jpg">

With CSS: <body style="background-color:#F0F8FF;"> or style="background-image:url('bg.jpg');".

Applying background and foreground colours

<body bgcolor="#000080" text="#FFFFFF">          <!-- navy background, white text -->
  <h1>ICT Department</h1>
  <p><font color="yellow">This line is yellow.</font></p>
  <table bgcolor="lightgreen"> ... </table>
</body>

Colours can be given by name (red, blue, lightgreen…) or hexadecimal code #RRGGBB where each pair is 00–FF:

Colour Hex code
Black #000000
White #FFFFFF
Red #FF0000
Green (lime) #00FF00
Blue #0000FF
Yellow #FFFF00
Grey #808080

CSS method: <p style="color:white; background-color:green;">Text</p>.

HTML frames

Frames divide the browser window into multiple sections, each showing a separate HTML page. They use <frameset> and <frame> (instead of <body>).

<!DOCTYPE html>
<html>
<head><title>Frames Example</title></head>
<frameset cols="25%,75%">
  <frame src="menu.html" name="menu">
  <frame src="home.html" name="content">
</frameset>
</html>

Advantages: common menu stays visible while content changes; content in each frame loads separately. Disadvantages: bookmarking specific pages is difficult; poor for search engines; problems on small screens and printing; <frameset> and <frame> are obsolete in HTML5.

Modern alternative – <iframe> (inline frame): embeds another page inside the current page; still valid in HTML5 (used for YouTube videos, Google Maps).

<iframe src="https://www.example.com" width="600" height="400" title="Example"></iframe>

HTML tables

Tables display data in rows and columns.

Tag Meaning
<table> Creates the table
<tr> Table row
<th> Table header cell (bold and centred by default)
<td> Table data (standard cell)
<caption> Title of the table
<thead>, <tbody>, <tfoot> Group header, body and footer rows
<table border="1" cellpadding="8" cellspacing="0" width="60%" align="center" bgcolor="lightyellow">
  <caption>ICT Mid-Term Result</caption>
  <tr bgcolor="lightblue">
    <th>Roll No</th>
    <th>Name</th>
    <th>Marks</th>
  </tr>
  <tr>
    <td>101</td>
    <td>Ayesha</td>
    <td align="center">27</td>
  </tr>
  <tr>
    <td>102</td>
    <td>Bilal</td>
    <td align="center">24</td>
  </tr>
</table>

Table attributes

Attribute Purpose
border Thickness of border lines (0 = no border)
cellpadding Space between cell content and cell border
cellspacing Space between cells
width, height Table size (pixels or %)
align Position of table (left, center, right)
bgcolor Background colour of table / row / cell
bordercolor Colour of the border

Merging cells

<table border="1">
  <tr>
    <th colspan="2">Student</th>
    <th rowspan="2">Grade</th>
  </tr>
  <tr>
    <th>Name</th>
    <th>Marks</th>
  </tr>
  <tr>
    <td>Sara</td>
    <td>45</td>
    <td>A</td>
  </tr>
</table>

HTML table Tag: HTML Tutorial for Beginners

HTML Crash Course (links and images sections) – Programming with Mosh

Short and long questions

1. Differentiate between cellpadding and cellspacing.

Model answer

cellpadding is the space between a cell's content and its border; cellspacing is the space between adjacent cells.

2. Explain hyperlinks and images in HTML with their attributes and examples.

Model answer

Hyperlinks use the anchor tag <a>. Attributes: href (destination), target (_blank new tab, _self same tab, or a frame name), title (tooltip). Types: external link <a href="https://www.w3schools.com">W3Schools</a>; internal page link <a href="page2.html">Next</a>; same-page bookmark <a href="#top">Top</a> with id="top"; email link <a href="mailto:info@uni.edu.pk">Mail</a>; download link; image link <a href="index.html"><img src="home.png"></a>. Link colours: link (blue), vlink (purple), alink (red) in body. Images use the empty tag <img>. Attributes: src (file path, required), alt (alternative text), width, height, border, align, title, hspace/vspace. Example: <img src="campus.jpg" alt="Campus" width="400" height="250" border="1" align="left">. Formats: JPG, PNG, GIF, SVG, WebP. Background image: <body background="bg.jpg">.

3. Differentiate between absolute and relative URLs.

Model answer

An absolute URL gives the complete address including protocol and domain (https://www.hec.gov.pk/index.html). A relative URL gives the path relative to the current page within the same website (contact.html, notes/week1.html).

4. What is a hyperlink? Write HTML code to create a link to www.google.com that opens in a new tab.

Model answer

A hyperlink is clickable text or an image that takes the user to another web page, a section, a file or an email address. Code: <a href="https://www.google.com" target="_blank">Google</a>.

5. Write HTML code to insert an image "campus.jpg" with width 400, height 250 and alternative text.

Model answer

<img src="campus.jpg" alt="University campus" width="400" height="250"> — src gives the file, alt gives alternative text and width/height set the size.

6. How are background and foreground colours applied to a web page?

Model answer

Background colour is set with the bgcolor attribute (e.g. <body bgcolor="lightblue">, also on table, tr, td). Foreground (text) colour is set with the text attribute of body (<body text="navy">) or the color attribute of <font>. Colours may be names or hex codes such as #FF0000.

7. What are HTML frames? Explain frameset and frame tags with attributes, give an example, and state advantages and disadvantages.

Model answer

Frames split the browser window into separate regions that each display a different HTML document. <frameset> replaces <body> and uses cols (vertical division) or rows (horizontal division) with values in %, pixels or * (remaining space). <frame> defines each region with src (page), name (link target), noresize, scrolling (yes/no/auto) and frameborder. <noframes> provides content for browsers without frame support.

<frameset rows="15%,*">
  <frame src="header.html" noresize scrolling="no">
  <frameset cols="25%,75%">
    <frame src="menu.html" name="menu">
    <frame src="home.html" name="main">
  </frameset>
</frameset>

A link in the menu such as <a href="week1.html" target="main"> opens in the main frame. Advantages: menu/header stay fixed while content changes; parts load independently. Disadvantages: hard to bookmark/print, poor for search engines and mobiles, obsolete in HTML5 – replaced by CSS layouts and <iframe> for embedding.

8. What are frames? Write code to divide the window into two vertical frames.

Model answer

Frames divide the browser window into multiple sections, each displaying a separate HTML page. Code: <frameset cols="30%,70%"> <frame src="menu.html" name="menu"> <frame src="main.html" name="main"> </frameset>.

9. Write HTML code to create the following table with a caption "Semester Result", border 1, header row with light-blue background: Roll No, Name, ICT, English – with two student records.

Model answer
<!DOCTYPE html>
<html>
<head><title>Result</title></head>
<body>
<table border="1" cellpadding="6" cellspacing="0" align="center">
  <caption><b>Semester Result</b></caption>
  <tr bgcolor="lightblue">
    <th>Roll No</th>
    <th>Name</th>
    <th>ICT</th>
    <th>English</th>
  </tr>
  <tr>
    <td>101</td>
    <td>Ayesha Khan</td>
    <td>85</td>
    <td>78</td>
  </tr>
  <tr>
    <td>102</td>
    <td>Bilal Ahmed</td>
    <td>72</td>
    <td>80</td>
  </tr>
</table>
</body>
</html>

<table> creates the table, <caption> its title, <tr> rows, <th> header cells and <td> data cells; border, cellpadding and bgcolor format it.

10. Differentiate between colspan and rowspan.

Model answer

colspan makes a cell extend across multiple columns (horizontal merge); rowspan makes a cell extend down multiple rows (vertical merge).

This lesson ends with a quiz of 37 MCQs. Open the lesson on PrepOasis to take it and save your progress.

Previous: Week 16 Lab 16 – HTML: Creating Lists and Tables · Next: Week 17 Lab 17 – HTML: Hyperlinks, Frames, Images, Backgrounds and Colours (Mini Website)

All lessons in Application of ICT (GEN-103) – BS 1st Semester