1. HTML

1.1. Introduction

In this chapter, various component of HTML are discussed to design a web page.

The basic structure for an HTML page is shown below.

  • Entries inside the /< … /> are known as tags. Most of the tags has an opening and closing e.g. <head> (opening head) and </head> (closing head). Some of the tags do not have closing tags e.g. <!DOCTYPE …> and <br />. We need to write the HTML codes inside the tags.
  • The comments are written between ‘<!–’ and ‘–>’.
  • Here Line 1 gives the details of the ‘HTML version’ to the web-browser. The ‘html’ tells it is version 5.
  • The ‘head’ tag (Lines 3-5) contains the header related tags e.g. ‘title for the page’ and ‘links for the css files’ etc.
  • The ‘body’ tag (7-11) contains the actual HTML code which is displayed on the web-browser. Also, we add all the JavaScript related codes just before the closing body tag (</body>).
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<!DOCTYPE html>  <!-- tells browser above the html version -->
<html> <!-- beginning of the html document -->
    <head>
            <!-- header related tags e.g. title, links etc.  -->
    </head>

    <body>
            <!-- actual html document here -->

            <!-- add JavaScript files here -->
    </body>
</html>

1.2. First code

In below code, the message “Hello World” is displayed on the HTML page. The Fig. 1.1 is the resultant HTML page.

  • The title (Line 4) appears on the top of the browser.
  • The tag <h1> is called ‘header’ tag, which has the larger size than the normal text (see the size of ‘Hello World!’).
  • The tag <p> is called the ‘paragraph’ tag, which can be used to write the paragraphs.
<!DOCTYPE html>
<html>
<head>
    <title>HTML Tutorial</title>
</head>
<body>
    <h1> Hello World! </h1>
    <p> This is the first HTML code </p>
</body>
</html>
../_images/html1.png

Fig. 1.1 First code

1.3. Basic tags

  • The Table 1.1 shows the list of tags which are required for writing the basic ‘HTML’ codes i.e. without any style e.g. bold, italics and numbering etc.
Table 1.1 List of basic tags
Tag Description Example
h1, …, h6 Header tag h1 to h6 <h2> Hi </h2>
p paragraphs (Line changes at the end) <p> Hi </p>
span No line change after span <span>Hi</span> Bye.
div make division between contents <div> … </div>
a hyperlink see Section 1.9
center Move content to center <center> Hi </center>
br Line break (no closing tag) <br /> or <br>
hr horizontal line (no closing tag) <hr /> or <hr>
pre preserve formatting <pre> …. </pre>
table insert table see Section 1.5
  • Let’s see the example of each of these tags,

Note

All the new codes are added below the previous codes in the ‘body’ tag. Therefore only newly added codes are shown in the tutorial.

<h2> Heading 2 </h2>
    <h6> Heading 6 </h6>

    <p> This is paragraph </p>

    <span> This is span.</span>
    <span> The 'br' tag is used after span to break the line </span>
    <br/>

    <div style="color:blue;">
        The 'div' tag can be used for formatting the tags inside it at once using 'style' and 'classes' etc.

        <p> This paragraph is inside the 'div' tag </p>
        <span> This span is inside the 'div' tag </span>
        <br/>

    </div>

    <center>
        <h3> Heading 3 is centered</h3>
        <p><span> Centered span inside the paragraph.</span><p>
    </center>

    Two horizontal line is drawn using two 'hr' tag.
    <hr />
    <hr>


    <pre> 'pre' tag preserve the formatting (good for writing codes)

            # Python code
            x = 2
            y = 3
            print(x+y)

    </pre>
  • Fig. 1.2 is the output of above code. Read the text to understand each tag,
../_images/html2.png

Fig. 1.2 Basic tags : Attribute ‘style’ is used in ‘div’ tag

1.4. Attributes

In Fig. 1.2, we saw an example of attribute (i.e. style) which changed the color of all the elements to ‘blue’ inside the ‘div’ tag.

1.4.1. Attribute ‘name’ and ‘value’

  • Attribute is defined inside the opening part of a ‘tag’. For example, in the below code, the attribute ‘style’ is defined inside the ‘div’ tag.
<div style="color:blue;">

</div>
  • An attribute has two parts i.e. ‘name’ and ‘value’. For example, in the above code, name and value of the attribute are ‘style’ and ‘blue’ respectively.

1.4.2. Core attributes

Below are the three core attributes which are used frequently in web design.

  • id : The ‘id’ is the unique name which can be given to any tag. This is very useful in distinguishing the element with other elements.
<p id='para1'> This is paragraph with id 'para1' </p>
<p id='para2'> This is paragraph with id 'para2' </p>
  • class : The attribute ‘class’ can be used with multiple tags. This is very useful in making groups in HTML design.
<p class="c_blue"> This is paragraph with class 'blue'</p>
<span class="c_blue"> This is span with class 'blue'</span>
  • style : We already see the example of style attribute, which can be used to change the formatting of the text in HTML design. We can specify various styles which are discussed in Chapter 2.
<p style="font-weight:bold; color:red;">Style attribute is used to bold and color</p>

Note

Above three attributes are used with ‘CSS (cascading style sheet)’ and JavaScript/jQuery, which are the very handy tools to enhance the look and functionalities of the web-page respectively. The CSS is discussed in Chapter 2, whereas JavaScript and jQuery are discussed in Chapter 4 and Chapter 5 respectively.

  • Also we can define multiple attributes for one tag as shown below,
<p class="my_class" id="para_with_class" style="color:green"> Multiple attributes </p>
  • The other useful attributes are listed in Table 1.2
Table 1.2 List of attributes
Name Values Description
id user defined names <p id=’p_1’> Hi </p>
class user defined names <p class=’p_class’> Hi </p>
style CSS styles <p style=”color:red; font-weight:bold;”> Hi </p>
align left, right, center horizontal alignment
width numeric value or % value width of images and tables etc.
height numeric value height of images and tables etc.

1.5. Tables

In this section, we will learn to draw tables along with some attributes which are discussed in Table 1.2. Table 1.3 shows the list of tags available to create the table, which are used in Listing 1.1.

Table 1.3 Tags and attributes for creating tables
Tag Description
table beginning and end of table
tr row of table
th header cell
td data cell
Attributes  
rowspan number of rows to merge
colspan number of columns to merge
border width of border
cellpadding width of whitespace between two border
cellspacing width of whitespace within a border
bgcolor background color
bordercolor color of border
width width of table (numeric or %)
height height of table (numeric)
caption caption for table
  • Some of the attributes of Table 1.3 are used in below example,
Listing 1.1 Table with border and color
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!-- border-color, width and height -->
<table border="1" bordercolor="black" width="450" height="100">
<caption>Table 1 : Various tags of table</caption>
    <tr bgcolor="red" > <!-- row -->
        <th>Column 1</th> <!-- header -->
        <th>Column 2</th>
        <th>Column 3</th>
    </tr>

    <tr bgcolor="cyan"> <!-- background color -->
        <td>Data 1</td> <!-- data -->
        <td>Data 2</td>
        <td>Data 3</td>
    </tr>

    <tr bgcolor="yellow"> <!-- row -->
        <td colspan="2">New Data 1</td> <!-- column span -->
        <td>New Data 2</td> <!-- data -->
    </tr>
</table>



<!-- width in % -->
<table border="1" bordercolor="black" width="80%" height="100">
<caption> Table 2 : Width is 80%</caption>
    <tr bgcolor="red" >
        <th>Column 1</th>
        <th>Column 2</th>
        <th>Column 3</th>
    </tr>

    <tr bgcolor="cyan"> <!-- row -->
        <td>Data 1</td> <!-- data -->
        <td>Data 2</td>
        <td>Data 3</td>
    </tr>

</table>
../_images/html3.png

Fig. 1.3 Table generated by Table 1.3

1.6. Text formatting

In this section, we will see some of the text formatting options (see Table 1.4) e.g. bold, italic, subscript and strike etc.

Table 1.4 Text formatting
Tag Description
b bold
i italic
u, ins underline
strike, del strike
sup superscript
sub subscript
big big size text
small small size text
  • Below are the some of the examples of text formatting, whose results are shown in Fig. 1.4,
<!-- Text formatting  -->
<p>This is <b>bold</b> text</p>
<p>This is <strike>striked</strike> text</p>
<p>This is <sub>subscript</sub> text</p>
../_images/html4.png

Fig. 1.4 Text formatting

1.7. Images

Image tag has two important attribues i.e. ‘src’ and ‘alt’ as described below,

  • src : tells the location of ‘image’ file e.g. in Line 2 the image ‘logo.jpg’ will be searched inside the folder ‘img’.
  • alt : is the ‘alternate text’ which is displayed if image is not found. For example, in Line 6, the name of the image is incorrectly written i.e. ‘logoa’ (instead of ‘logo’), therefore the value of ‘alt’ i.e. ‘Missing Logo.jpg’ will be displayed as shown in Fig. 1.5.
1
2
3
4
5
6
<!-- Images -->
<img src="img/logo.jpg" alt="Logo.jpg" width="20%"/>

<br/> <br/>

<img src="img/logoa.jpg" alt="Missing Logo.jpg" width="20%"/>
../_images/html5.png

Fig. 1.5 Images

Note

We can use other attributes as well e.g. ‘height’, ‘align’ and ‘border’ etc.

1.8. Lists

There are three type of lists in HTML,

  • Unordered list : bullet are used in it (see Lines 2 and 9)
  • Ordered list : numbers are used in it (see Lines 15, 22 and 28)
  • Definition list : This can be used for writing definitions in HTML (see Line 35)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!-- Lists -->
<!-- unordered list -->
<ul> Unordered List
    <li>Pen</li>
    <li>Pencil</li>
    <li>Eraser</li>
</ul>

<ul type="circle"> Change bullets : 'square', 'circle' or 'disc'
    <li>Pen</li>
    <li>Pencil</li>
    <li>Eraser</li>
</ul>

<!-- ordered list -->
<ol> Ordered List
    <li>Pen</li>
    <li>Pencil</li>
    <li>Eraser</li>
</ol>

<ol type='i'> Change style : 'i', 'I', '1', 'a' or 'A'
    <li>Pen</li>
    <li>Pencil</li>
    <li>Eraser</li>
</ol>

<ol type='i' start="5"> Start from 'v'
    <li>Pen</li>
    <li>Pencil</li>
    <li>Eraser</li>
</ol>

<!-- Definition list -->
<dl>
    <dt> <h4>HTML Definition List</h4> </dt>
    <dd> HTML is easy </dd>
    <dd> HTML is good </dd>
<dl>

The outputs of above codes are shown in Fig. 1.6,

../_images/html6.png

Fig. 1.6 Lists

1.10. Forms

Forms can have different types of controls to collect the input-data from users, which are listed below and shown in Table 1.5,

  • Text input
  • Text area
  • Radio button
  • Checkbox
  • Select box
  • File select
  • Buttons
  • Submit and reset buttons
  • Hidden input
Table 1.5 List of control inputs and their attributes
Control Attributes Values Description
Input : text type text, password  
  value user-defined initial value in the area
  name user-defined name send to server
  size numeric value width of the text area
  maxlength numeric value maximum number of characters
Input : radio type radio  
  name user-defined name send to server
  value user-defined value value of the button if selected
  checked   check the button by default
Input : check box type checkbox  
  name user-defined name send to server
  value user-defined value value of the box if selected
  checked   check the box by default
Input : button type button trigger client side script
    submit submit the form and run ‘action’
    reset reset form
    image create image button
  method get, post get or post method
  action user-defined action to perform on submit
Input : hidden type hidden will not display on html, but can be used
      for sending information to server
Selection box name user-defined name send to server
  size numeric value enables scroll (default dropdown)
  multiple numeric value select multiple items
  value user-defined value value of the item if selected
  selected   select item by default
Text area rows, cols numeric value number of rows and cols
  name user-defined name send to server
  • Below are the exmaple of the control inputs described in Table 1.5
<!-- Forms -->
<form>
    <h4>Text input </h4>
    Name : <input type="text" name="user_name" size="4" value="e.g. meher21" maxlength="10"><br>
    Password : <input type="password" name="user_pass" ><br>


    <h4> Radio button: name should be same</h4>
    <input type="radio" name="r_gender"> Male
    <input type="radio" name="r_gender"> Female
    <input type="radio" name="r_gender" checked> Infant

    <h4> Check box : name should be different</h4>
    <input type="checkbox" name="c_male" checked> Male
    <input type="checkbox" name="c_female"> Female
    <input type="checkbox" name="c_infant"> Infant

    <h4> Select box : drop-down</h4>
    <select name="s_box">
        <option value="s_male">Male</option>
        <option value="s_female" selected>Female</option>
        <option value="s_infant">Infant</option>
    </select>

    <h4> Select box : scroll</h4>
    <select name="s_box" size="4" multiple>
        <option value="s_male" selected>Male</option>
        <option value="s_female" selected>Female</option>
        <option value="s_infant">Infant 1</option>
        <option value="s_infant" selected>Infant 2</option>
        <option value="s_infant">Infant 3</option>
        <option value="s_infant">Infant 4</option>
    </select>

    <h4> Text area</h4>
    <textarea rows="10" cols="80" name="txt_area">Initial Text
    x = 2
    y = 3
    </textarea> <!-- formatting work as pre -->

</form>

Fig. 1.8 is the output of above code,

../_images/html8.png

Fig. 1.8 Various control inputs for creating form

  • Below is the code which shows the working of various buttons. Note that method and action are defined in this form, which will be triggered on ‘submit’ button. Lastly, ‘hidden’ option is used in this example.
<form method="get|post" action="jquery.html">
    <h4> Buttons and Hidden</h4>

    Name : <input type="text" name="user_name" size="4" value="Meher" maxlength="16"><br>
    Password : <input type="password" name="user_pass" ><br>

    <input type="button" onclick="alert('Hello')" name="b_alert" value="Say Hello"/><br>
    <input type="submit" name="b_submit" value="Go to jQuery"/>
    <input type="reset" name="b_reset" value="Reset"/><br>

    <input type="hidden" name="h_data" value="html_tutorial">
</form>

Fig. 1.9 is the output of above code,

../_images/html9.png

Fig. 1.9 Various buttons and hidden-input in the form