Author: saqibkhan

  • Nested Tables

    Nested Tables

    HTML nested tables refer to the table where one or more tables are placed inside it; the inner tables are placed inside the <td> tag. The nested tables involve the utilization of one table within another, providing a versatile way to structure complex data layouts. Various elements, including other HTML tags, can be incorporated within table cells (<td>).

    Not only can tables be nested, but various HTML tags can also be used within the <td> (table data) tag for arranging contents in a structured manner.

    Basic Structure of Nested Tables

    Nested tables refer to the practice of embedding an entire table structure within a cell of another table. This technique allows for the creation of more complex and structured layouts in HTML.

    See this image of how a nested table looks like i.e., this image demonstartes a structure of nested tables:

    HTML Nested Tables

    How To Create a Nested Table?

    You can create a nested table in HTML by creating one or more tables in different table cells. The following are the steps to create an HTML nested table:

    Step 1: Create Outer Table

    Begin by creating the outer table, which serves as the container.

    <table border="1"><!-- Outer table content --></table>

    Step 2: Define Rows and Columns

    Within the outer table, define the necessary rows and columns.

    <tr><td><!-- Content within the cell --></td></tr>

    Step 3: Embed Inner Table

    Inside the cell, embed a new table structure using the <table> tags.

    <td><table border="1"><!-- Inner table content --></table></td>

    Step 4: Define Inner Table Content

    Within the inner table, define rows and columns as needed.

    <tr><td>Inner Content</td></tr>

    Step 5 – Close Tags

    Ensure proper closure of all HTML tags.

    </table>

    Example of Nested Tables

    Following is an example by following the above steps −

    Open Compiler

    <!DOCTYPE html><html><head><title>Nested Tables</title></head><body><table border="1" width="100%"><tr><td><table border="1" width="100%"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></td></tr></table></body></html>

    Tables Within Cells

    Tables can be nested within table cells, allowing for complex structures.

    Example

    The following example creates a table with two columns (Name and Salary). Inside the first column, there’s a nested table displaying employee details with the same columns.

    The outer table’s header and data rows are defined, and the inner table showcases two employees, Ramesh Raman, and Shabbir Hussein, with corresponding salaries.

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td><table border="1" width="100%"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></td></tr></table></body></html>

    Styling Nested Tables

    You can apply styles to nested tables just like regular tables to enhance visual appearance. To write CSS styles for nested tables, you can simply apply the CSS rules on the ‘table‘ selector to style the outer table and use the ‘table table‘ selector to style the inner tables.

    Example

    The following code includes a main table styled with CSS properties. The border-collapse ensures seamless borders, and ‘width: 100%’ makes it responsive. Cells (<td>, <th>) have padding, border, and text alignment.

    Additionally, nested tables within cells inherit a distinctive style, including a colored background and borders. The main table’s data cell contains a nested table with a blue border, 80% width, and centered using ‘margin: 10px auto’.

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Nested Tables with CSS Styles</title><style>
    
      table {
         border-collapse: collapse;
         width: 100%;
      }
      td,
      th {
         border: 1px solid #dddddd;
         text-align: left;
         padding: 8px;
      }
      /* Additional styles for nested tables */
      table table {
         border: 2px solid #3498db;
         width: 80%;
         margin: 10px auto;
      }
      table table td {
         background-color: #ecf0f1;
         border: 1px solid #bdc3c7;
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td><table border="1" width="100%"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></td></tr></table></body></html>

    Images in Nested Tables

    Embedding images within nested tables enhances visual presentation. Use the <img> tag inside table cells to seamlessly integrate images into complex layouts like nested tables.

    Example

    The following example illustrates the use of images inside nested tables:

    Open Compiler

    <!DOCTYPE html><html><head><title>Nested Tables</title></head><body><table border="1" width="100%"><tr><td><table border="1" width="100%"><tr><th>Image </th><th>Name</th></tr><tr><td><img src="images\logo.png" alt="Nested Image"></td><td>LOGO </td></tr><tr><td><img src="images\html5_icon.png" alt="Nested Image"></td><td>HTML5 </td></tr></table></td></tr></table></body></html>

    Benefits of Nested Tables

    Following are the advantages of the nested tables −

    • Organized Layouts − Nested tables enable the creation of organized and structured layouts, enhancing the presentation of complex data.
    • Cell Customization − Each cell in a nested table can contain diverse content, including text, images, or even additional nested tables.
    • Complex Designs − Achieve intricate design patterns by nesting tables within cells, providing flexibility in webpage design.
  • Table Colgroup

    HTML Table Colgroup

    In HTML, the <colgroup> element is used to define a group of columns in a table. It allows you to apply properties to multiple columns simultaneously, providing a more efficient way to style or format columns.

    HTML Table Colgroup

    The <colgroup> Tag

    The <colgroup> is often used in conjunction with the <col> element, where each <col> tag represents an individual column within the group. This grouping enhances readability and simplifies the application of styles or attributes to specific columns in a table.

    Syntax

    Following is the syntax to use <colgroup> with <table> tag:

    <table><colgroup><col span="value" style="width: ...;"><col style="..."><!-- More <col> elements... --></colgroup><!-- Other table elements such as <thead>, <tbody>, ... --></table>

    Using <colgroup> Tag in HTML Table

    Using <colgroup> in HTML involves the following steps −

    1. Insert <colgroup> Tag

    Place the <colgroup> tag within the <table> element, usually inside the <thead> (table head) or <tbody> (table body) section.

    <table><colgroup><!-- Column definitions --></colgroup><thead><!-- Table headers --></thead><tbody><!-- Table rows --></tbody></table>

    2. Define Columns

    Inside the <colgroup> tag, use one or more <col> tags to represent each column. Specify attributes or styles for the columns within these <col> tags.

    <table><colgroup><col><col><col></colgroup><!-- Table content --></table>

    3. Apply Attributes or Styles

    Define attributes or styles for the columns by adding attributes such as spanwidthstyle, or class to the <col> tags.

    Open Compiler

    <table><colgroup><col style="background-color: lightgrey;" span="2"><!-- First two columns --><col style="background-color: lightblue;"><!-- Third column --></colgroup><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td><td>Data 3</td></tr></tbody></table>

    Example of HTML Table Colgroup

    In this example, the <colgroup> tag defines two columns with different widths, and the styles are applied to the columns using the &lt;col&gt; tags. The second row in the table is highlighted using a CSS class.

    Open Compiler

    <!DOCTYPE html><html><body><table border=1><colgroup><col style="width: 30%;"><col style="width: 70%;"></colgroup><thead><tr><th>Column 1</th><th>Column 2</th></tr></thead><tbody><tr><td>Row 1, Col 1</td><td>Row 1, Col 2</td></tr><tr class="highlight"><td>Row 2, Col 1</td><td>Row 2, Col 2</td></tr></tbody></table></body></html>

    CSS Properties for <colgroup> Tag

    In HTML, the <colgroup> element allows some specific CSS properties to enhance the presentation of table columns. The legal CSS properties that can be used within a <colgroup> are as follows −

    • width Property − This property sets the width of the columns within the <colgroup>. It allows you to define the relative or absolute width of each column.
    • visibility Property − The visibility property can be used to control the visibility of columns within the <colgroup>. You can set it to “hidden” to make a column invisible.
    • Background Properties − Background properties, such as background-color, can be applied to add background styling to the columns. This can enhance the visual appeal of the table.
    • Border Properties − Border properties, like border-color and border-width, enable the customization of borders around the columns. This is useful for creating well-defined visual boundaries.

    Attempting to apply other CSS properties will have no impact on the styling of the table columns. Therefore, when styling tables with <colgroup>, focus on the available properties to achieve the desired layout and appearance.

    Example

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>
    
      table {
         width: 100%;
         border-collapse: collapse;
      }
      colgroup {
         /* Setting width for columns */
         width: 20%;
         background-color: #f0f0f0;
         /* Background color for columns */
         visibility: visible;
         /* Making columns visible */
         border: 2px solid #3498db;
         /* Border around columns */
      }
      col {
         /* Additional styling for individual columns */
         background-color: #ecf0f1;
         border: 1px solid #bdc3c7;
      }
      td,
      th {
         border: 1px solid #dddddd;
         text-align: left;
         padding: 8px;
      }
    </style></head><body><table><colgroup><col><col style="width: 30%;"><col></colgroup><thead><tr><th>Header 1</th><th>Header 2</th><th>Header 3</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td><td>Data 3</td></tr><tr><td>Data 4</td><td>Data 5</td><td>Data 6</td></tr></tbody></table></body></html>

    Multiple Col Elements

    Certainly! The <colgroup> element in HTML allows you to group a set of columns in a table and apply styles to them collectively. Within <colgroup>, you can use multiple <col> elements to define different styles for individual columns.

    Example

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>
    
      col {
         /* Additional styling for individual columns */
         background-color: #ecf0f1;
         border: 1px solid #bdc3c7;
      }
    </style></head><body><table border=5><colgroup><col style="width: 20%;"><col style="width: 30%;"><col style="width: 50%;"></colgroup><thead><tr><th>Header 1</th><th>Header 2</th><th>Header 3</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td><td>Data 3</td></tr><tr><td>Data 4</td><td>Data 5</td><td>Data 6</td></tr></tbody></table></body></html>

    The <colgroup> contains three <col> elements, each with a specific ‘width’ style, defining the width of individual columns.

    Empty Column groups

    In HTML, a <colgroup> element can be used to define a group of columns in a table. An empty <colgroup> can be employed to provide a structural placeholder for potential styling or later use. While it doesn’t contain explicit <col> elements, it can still influence the overall structure of the table.

    Example

    Here’s a simple example demonstrating the use of an empty <colgroup>. In here, the <colgroup> is empty but serves as a placeholder for potential styling. The entire <colgroup> is styled with a background color and a border. The <col> elements are not explicitly used, but their styling can be defined within <colgroup> for future use or consistency in the structure.

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>
    
      colgroup {
         /* Styling for the colgroup (can be empty) */
         background-color: #f0f0f0;
         /* Background color for the entire colgroup */
         border: 2px solid #3498db;
         /* Border around the entire colgroup */
      }
      /* Additional styling for individual columns */
      col {
         background-color: #ecf0f1;
         border: 1px solid #bdc3c7;
      }
    </style></head><body><table border=3><colgroup></colgroup><thead><tr><th>Header 1</th><th>Header 2</th><th>Header 3</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td><td>Data 3</td></tr><tr><td>Data 4</td><td>Data 5</td><td>Data 6</td></tr></tbody></table></body></html>

  • Table Styling

    You can style HTML tables by using the CSS. Table styling helps you to customize the normal appearance of the elements like borders, cell padding, text alignment, background colors, and more to create a well-formatted table on a webpage.

    The following are some of the table stylings in HTML:

    • Collapsed Border Table
    • Horizontal Zebra Striped Table
    • Text Alignment in Table Cells
    • Vertical Zebra Striped Table
    • Table with Combined Vertical and Horizontal Zebra Stripes
    • Table with Horizontal Dividers
    • Hoverable Table Rows

    Collapsed Border Table

    You have the flexibility to manage the space between table borders by manipulating the ‘border-collapse’ property. This property determines how adjacent table cell borders interact, and adjusting it allows you to control the spacing or gap between the borders within your table.

    By setting ‘border-collapse’ to “collapse”, borders will merge, eliminating any spacing, while “separate” allows you to control the spacing using the ‘border-spacing’ property, providing a more customized appearance to your table layout.

    Example

    This is an example of a collapsed border table, where the table borders are merged into a single border using the border-collapse: collapse; property.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
      #table1 {
         border-collapse: separate;
      }
      #table2 {
         border-collapse: collapse;
      }
    </style></head><body><table id="table1" border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table><br /><table id="table2" border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Horizontal Zebra Striped Table

    The Zebra Stripes – Horizontal technique involves styling table rows with alternating colors, enhancing the visual appeal and readability of the table.

    The :nth-child(even) selector targets every second row, and a background color is applied to create a striped effect.

    Example

    This is an example of a horizontal zebra-striped table, here alternating rows are styled with different background colors:

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
      tr:nth-child(even) {
         background-color: #8a83de;
      }
    </style></head><body><table border="1"><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></table></body></html>

    Text Alignment in Table Cells

    You can align the text within your table cells horizontally and vertically using the text-align and vertical-align properties.

    Example

    This is an example of text alignment in table cells:

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
      td,
      th {
         text-align: center;
         vertical-align: middle;
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Vertical Zebra Striped Table

    The Zebra Stripes – Vertical technique enhances table readability by applying alternating styles to every other column. This is achieved using the :nth-child(even) selector for both table data (td) and table header (th) elements.

    Example

    This is an example of a vertical zebra-striped table, where alternating columns are styled with different background colors:

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
      td:nth-child(even),
      th:nth-child(even) {
         background-color: #D6EEEE;
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Table with Combined Vertical & Horizontal Zebra Stripes

    You can achieve a captivating visual effect by combining both horizontal and vertical zebra stripe patterns on your table. This involves applying alternating styles to both rows (:nth-child(even)) and columns (td:nth-child(even), th:nth-child(even)).

    To enhance this effect, consider adjusting the color transparency using the rgba() function, creating an engaging and aesthetically pleasing overlap of stripes for a unique and visually interesting outcome.

    Example

    This is an example of a table with combined vertical and horizontal zebra stripes, where both alternating rows and columns are styled with different background colors:

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
      tr:nth-child(even) {
         background-color: rgba(150, 212, 212, 0.4);
      }
      th:nth-child(even),
      td:nth-child(even) {
         background-color: rgba(212, 150, 192, 0.4);
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Table with Horizontal Dividers

    You can enhance the visual structure of your table by incorporating horizontal dividers. Achieve this effect by styling each ‘<tr>’ element with a bottom border.

    This CSS customization provides a clear separation between rows, contributing to improved table clarity and a more organized presentation of tabular data.

    Example

    This is an example of a table with horizontal dividers, where each row is separated by a distinct horizontal line:

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
      table {
         border-collapse: collapse;
      }
      tr {
         border-bottom: 5px solid #ddd;
      }
      th,
      td {
         padding: 10px;
         /* Add padding for better visibility */
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    The above HTML program defines a simple table with two rows and two columns. CSS styles are applied to create a visual separation between rows using a solid border at the bottom of each row. The border-collapse property ensures a cleaner layout, and padding is added for improved visibility of table cells.

    Hoverable Table Rows

    You can improve user interaction by employing the ‘:hover’ selector, which highlights table rows when users hover over them. This enhances the visual feedback, making the table more interactive and user-friendly.

    Example

    This is an example of hoverable table rows, where the background color of a table row changes when the user hovers over it:

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
      tr:hover {
         background-color: #D6EEEE;
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    The above HTML program creates a table with a border. The CSS style makes the rows change the background color to light blue when hovered over, enhancing user interaction.

  • HTML Table Headers and Captions

    Table Headers and Captions

    Headers and captions are used inside tables to organize and present data in a structured format.

    The table heading is an essential part of a table, providing labels for columns. The <th> (table header) element is used to define table headings.

    Captions are used in the tables to provide a title or explanation for the table. The <caption> element is placed immediately after the opening table tag.

    HTML Table Headers and Captions

    The <caption> tag is deprecated in HTML5 and XHTML. This means that it is still supported by most web browsers, but it is not recommended for use in new web pages. If you are writing new code, you should use the figure and figcaption elements instead. The figure element is used to group related content, and the figcaption element is used to provide a caption for the content.

    The <caption> tag is deprecated in HTML5 and XHTML. This means that it is still supported by most web browsers, but it is not recommended for use in new web pages. If you are writing new code, you should use the figure and figcaption elements instead. The figure element is used to group related content, and the figcaption element is used to provide a caption for the content.

    Syntax to Create Table’s Header and Caption

    The following is the syntax to create a header and caption for an HTML table:

    <table><caption>Description of table</caption><tr><th>heading 1</th><th>heading 2</th><th>heading 3</th></tr></table>

    Define a Header Row for a Table

    The <th> tag is used to represent table headings, and it is typically used within the <tr> (table row) element. Unlike the <td> (table data) tag used for regular cells, <th> is reserved for headers. In most cases, the first row of a table is designated as the header row.

    Example

    Consider a simple HTML table with headings for “Name” and “Salary”:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>HTML Table Header</title></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Styling Table Headings

    Styling table headings can enhance the visual appeal of a table. CSS can be applied to <th> elements to customize their appearance. In the following example, a simple CSS style is added to the <style> tag within the <head> section to modify the background color and text alignment of the table headings.

    Example

    This example demonstrates how you can style table headings with CSS:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>Styled HTML Table Header</title><style>
       th {
    
      background-color: #4CAF50;
      color: white;
      text-align: left;
      padding: 8px;
    } </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Using Header Cells in Any Row

    While it’s common to use <th> in the first row of a table, you can utilize it in any row based on your requirements. This flexibility allows for the creation of complex tables with multiple header rows or headers interspersed within the table.

    Example

    In this example, we are creating table headers in the first row:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>Styled HTML Table Header</title><style>
    
      th {
         background-color: #4CAF50;
         color: white;
         text-align: left;
         padding: 8px;
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><th>Additional Details</th><th>Specialization</th></tr><tr><td>Technical Lead</td><td>Web Development</td></tr></table></body></html>

    Table Header Using <thead> Element

    The <thead> tag is used to group table header cells so that a combined CSS style can be applied to header cells.

    The <thead> tag is typically placed within the <table> element and contains one or more <tr> elements, each of which, in turn, contains <th> elements representing column headers.

    Example

    In this example, we are creating a table header using the thead tag:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>HTML Table Header</title></head><body><table border=1><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><!-- Table body goes here --></table></body></html>

    Defining Multiple Header Rows

    You can include multiple <tr> elements within <thead> to create multiple header rows. This is useful when your table structure requires a more complex hierarchy of headers.

    Example

    In this example, we are defining two rows as the table header:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>HTML Table Header</title></head><body><table border=1><thead><tr><th colspan=2>Tutorialspoint</th></tr><tr><th>Role</th><th>Experience</th></tr></thead><tr><td>Technical Lead</td><td>5 Years</td></tr><tr><td>Web Developer</td><td>2 Years</td></tr></table></body></html>

    Using ‘<colgroup>’ Inside ‘<thead>’

    The <colgroup> tag can be used within <thead> to group together a set of column and apply CSS styling or attributes to entire columns.

    Example

    In this example we apply style to first two columns of table by grouping those columns in colgroup tag.

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
      .col1 {
         background-color: #f2f2f2;
      }
    </style></head><body><h1>Table with colgroup</h1><table border="1"><colgroup class="col1"><col style="width: 150px;"><col style="width: 200px;"></colgroup><col style="width: 150px;"><col style="width: 100px;"><thead><tr><th>Product ID</th><th>Product Name</th><th>Category</th><th>Price</th></tr></thead><tbody><tr><td>1</td><td>Smartphone</td><td>Electronics</td><td>$299.00</td></tr><tr><td>2</td><td>Office Chair</td><td>Furniture</td><td>$89.00</td></tr><tr><td>3</td><td>Laptop</td><td>Electronics</td><td>$999.00</td></tr></tbody></table></body></html>

    Combining with ‘<tfoot>’ and ‘<tbody>’

    The <thead> tag is often combined with <tfoot> (table footer) and <tbody> (table body) to create a comprehensive table structure.

    Example

    In the following code the structure of table separates header, body, and footer content for better organization.

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>HTML Table</title></head><body><table border><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><tbody><tr><td>value 1</td><td>value 2</td><td>value 3</td></tr></tbody><tfoot><tr><td>Total</td><td></td><td></td></tr></tfoot></table><p>
    
      Footers are generally used to enter 
      sum of values of each column.
    </p></body></html>

    Difference between <thead> and <th>

    Following are the points that highlight the differences between <thead> and <th> −

    • The <thead> tag is a structural element used to group header content, while <th> is a cell-level element defining a header cell.
    • It’s common to use <th> within <thead>, but <th> can also be used outside <thead> to define headers in regular rows.
    • Including <thead> is optional; however, using it improves the semantic structure of the table.

    Adding Caption to a HTML Table

    The caption tag will serve as a title or explanation for the table, and it shows up at the top of the table.

    Example

    In the following code we will display a caption on top of a HTML table:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Table Caption</title></head><body><table border="1"><caption>This is the caption</caption><tr><td>row 1, column 1</td><td>row 1, column 2</td></tr><tr><td>row 2, column 1</td><td>row 2, column 2</td></tr></table></body></html>

    Table Header, Body, and Footer

    Tables can be divided into three portions: a header, a body, and a foot. The head and foot are rather similar to headers and footers in a word-processed document that remain the same for every page, while the body is the main content holder of the table.

    The three elements for separating the head, body, and foot of a table are −

    • The <thead> tag to create a separate table header.
    • The <tbody> tag to indicate the main body of the table.
    • The <tfoot> tag to create a separate table footer.

    A table may contain several <tbody> elements to indicate different pages or groups of data. But it is notable that <thead> and <tfoot> tags should appear before <tbody>

    Example

    In this example, we are creating an HTML table with the table header, body, and footer:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Table</title></head><body><table border="1" width="100%"><thead><tr><th colspan="4">
    
               This is the head of the table
            &lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tfoot&gt;&lt;tr&gt;&lt;td colspan="4"&gt;
               This is the foot of the table
            &lt;/td&gt;&lt;/tr&gt;&lt;/tfoot&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cell 1&lt;/td&gt;&lt;td&gt;Cell 2&lt;/td&gt;&lt;td&gt;Cell 3&lt;/td&gt;&lt;td&gt;Cell 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cell 5&lt;/td&gt;&lt;td&gt;Cell 6&lt;/td&gt;&lt;td&gt;Cell 7&lt;/td&gt;&lt;td&gt;Cell 8&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • HTML – Tables

    HTML tables represent data, such as text, images, etc. in a structured format with rows and columns.

    HTML tables offer a visual structure that aids in clarity and comprehension, making them a fundamental element in web development.

    HTML Tables

    Why HTML Tables are Used?

    HTML tables are used for various reasons, primarily centered around organizing and presenting data effectively. Some key purposes include −

    • Structuring Data − Tables provide a coherent structure for organizing and displaying data, making it easier for users to interpret information.
    • Comparative Presentation − When there is a need to compare different sets of data side by side like difference between two concepts, tables offer a clear and visually accessible format.
    • Tabular Data Representation − Information that naturally fits into rows and columns, such as schedules, statistics, or pricing tables, can be well-represented using HTML tables.

    Creating an HTML Table

    You can create a table in HTML by using the <table> tag along with several tags that define the structure and content inside the table. The primary tags that are used with the <table> tag are <tr><td>, and <th>.

    Creating tables in HTML involves several elements that define the structure and content. The primary tags used are <table>, <tr>, <td>, and <th>.

    • HTML <table> Tag: This tag is used to create the table that wrap the rows and columns within it.
    • HTML <tr> Tag: Stands for “table row” and is used to create a row within the table.
    • HTML <td> Tag: Represents “table data” and is used to create standard cells within a row.
    • HTML <th> Tag: Represents “table header” and is used to create header cells within a row.

    HTML Table Structure – Rows and Columns

    • Rows: Each row in an HTML table is defined using the <strong>&lt;tr></strong> tag. It contains a set of table cells (<strong>&lt;td></strong> or <strong>&lt;th></strong>), representing the individual elements within that row.
    • Columns: The actual data or header information is contained within the table cells. Cells in the same position in different rows form a column.
    • A table row is defined by the <tr> tag. To set table header, we use <th> tag. To insert data in table cell, use the <td> tag.
    • A table in HTML consists of table cells inside rows and columns of the table.
    • Table heading is defined by the <th>…</th>. Data inside the <th> are the headings of the column of a table.
    • Each table cell is defined by a <td>…</td> tag. Data inside the <td> tag are the content of the table rows and columns.
    • Each table row starts with a <tr> ….</tr> tag.
    • We use style sheet to create border for the table.

    Example

    Consider a table representing a simple list of products with their respective categories and prices. This basic table structure organizes data in a clear, tabular format, making it easy to read and understand. Here, the border is an attribute of <table> tag and it is used to put a border across all the cells. If you do not need a border, then you can use border=”0″.

    Open Compiler

    <!DOCTYPE html><html><body><table border="1"><tr><th>Product</th><th>Category</th><th>Price</th></tr><tr><td>Laptop</td><td>Electronics</td><td>$800</td></tr><tr><td>Bookshelf</td><td>Furniture</td><td>$150</td></tr><tr><td>Coffee Maker</td><td>Appliances</td><td>$50</td></tr></table></body></html>

    Styling HTML Tables

    You can also style an HTML table using CSS properties to give it a custom appearance. Either you can create classes to apply styles on a table, or you can simply write internal CSS properties to style the table.

    Example

    In the example below, we are styling the table with some CSS properties to make it stylish:

    Open Compiler

    <!DOCTYPE html><html><head><style>
       table {
    
      width: 100%;
      border-collapse: collapse;
      margin-bottom: 20px;
    } th, td {
      border: 1px solid #ddd;
      padding: 8px;
      text-align: left;
    } th {
      background-color: #f2f2f2;
    } </style></head><body><h2>HTML Table</h2><p>This table is 3*3 cells including table header.
    &lt;table&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Data 1&lt;/td&gt;&lt;td&gt;Data 2&lt;/td&gt;&lt;td&gt;Data 3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Data 4&lt;/td&gt;&lt;td&gt;Data 5&lt;/td&gt;&lt;td&gt;Data 6&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Table Background Color and Image

    You can set the background color and background image of an HTML table by using the CSS and attributes of the <table> tag.

    Using Attributes

    The following are the attributes that can be used with <table> tag to set the background color and/or image:

    • bgcolor: The bgcolor attribute sets the table's background color.<table bgcolor="#f0f0f0">
    • background: The background attribute sets a background image.<table background="image.jpg">

    Using CSS Properties

    Using table tag's attributes is an old (outdated) style. It is recommended that you should use CSS to style an HTML table. The background-color and background-image properties are used to set background color and image respectively.

    table {
      background-color: #f0f0f0;
      background-image: url('image.jpg');
    }

    Example to set table's background color and image using attributes

    Here we are setting background color and image for a table using the attributes of <table> tag:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Table Background Color</title></head><body><table border="1" bordercolor="green" bgcolor="yellow" background="/images/test.png"><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr><tr><td rowspan="2">Row 1 Cell 1</td><td>Row 1 Cell 2</td><td>Row 1 Cell 3</td></tr><tr><td>Row 2 Cell 2</td><td>Row 2 Cell 3</td></tr><tr><td colspan="3">Row 3 Cell 1</td></tr></table></body></html>

    Example to set table's background color and image using CSS

    Here we are setting background color and image for a table using the CSS properties:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Table Background Color</title><style>
    table {
      background-color: yellow;
      background-image: url('/images/test.png');
    }
    </style></head><body><table><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr><tr><td rowspan="2">Row 1 Cell 1</td><td>Row 1 Cell 2</td><td>Row 1 Cell 3</td></tr><tr><td>Row 2 Cell 2</td><td>Row 2 Cell 3</td></tr><tr><td colspan="3">Row 3 Cell 1</td></tr></table></body></html>

    Table Width and Height

    The table's width and height can be set using either attributes or CSS properties. These values can be defined in pixels or percentages.

    Using Attributes

    The following attributes can set the width and height of a table:

    • width: It defines the width of the table.<table width="80%">
    • height: It defines the height of the table.<table height="200">

    Using CSS

    The following CSS properties can set the width and height of a table:

    • width: It defines the width of the table.table { width: 80%; }
    • height: It defines the height of the table.table { height: 400px; }

    Example to set table's width and height using attributes

    Here we are setting width (80%) and height (400px) of the table using the <table> tag's attributes:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Table Width/Height</title></head><body><table border="1" width="80%" height="400"><tr><th>Header 1</th><th>Header 2</th></tr><tr><td>Row 1, Column 1</td><td>Row 1, Column 2</td></tr><tr><td>Row 2, Column 1</td><td>Row 2, Column 2</td></tr></table></body></html>

    Example to set table's width and height using CSS

    Here we are setting width (80%) and height (400px) to the table using the CSS properties:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Table Width/Height</title><style>
       table{
    
       width: 80%;
       height: 400px;
    } </style></head><body><table border="1"><tr><th>Header 1</th><th>Header 2</th></tr><tr><td>Row 1, Column 1</td><td>Row 1, Column 2</td></tr><tr><td>Row 2, Column 1</td><td>Row 2, Column 2</td></tr></table></body></html>

    HTML Nested Tables

    Nested HTML tables refer to create tables inside a table. You can create tables inside a table by using the <table> tab inside any <td> tag, it creates another table in the main table's cell.

    Example

    In the following example, we are creating nested tables:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Nested Tables</title></head><body><table border=1><tr><td> First Column of Outer Table </td><td><table border=1><tr><td> First row of Inner Table </td></tr><tr><td> Second row of Inner Table </td></tr></table></td></tr><tr><td><table border=1><tr><td> First row of Inner Table </td></tr><tr><td> Second row of Inner Table </td></tr></table></td><td> First Column of Outer Table </td></tr></table></body></html>

    Table-related Tags Reference

    The following are the table-related tags. You can click on the link to read about the specific tag and click on "Try It" to practice its example:

    TagDescriprtionExample
    <table>It is used to create HTML table.Try It
    <th>This tag defines the header of the table.Try It
    <tr>This tag defines a table row.Try It
    <td>This tag is used to store table data of each cell.Try It
    <caption>This tag specifies the caption for the table.Try It
    <colgroup>This tag describes the collection of one or more columns in a table for formattig.Try It
    <col>This tag is used to offer information about columns.Try It
    <thead>This tag is used to define table header section.Try It
    <tbody>This tag is used to define table body section.Try It
    <tfoot>This tag is used to define the table footer section.
  • HTML – Backgrounds

    The background of a webpage is a layer behind its content, which includes text, images, colors, and various other elements.

    It is an essential part of web design that improves the overall look of a web page as well as the user experience. HTML offers multiple attributes and properties for manipulating the background of elements within a document.

    By default, our webpage background is white in color. We may not like it, but no worries. HTML provides the following two good ways to decorate our webpage background:.

    • HTML Background with Colors
    • HTML Background with Images

    Syntax

    The following are the syntaxes for HTML backgrounds:

    <body background = value><body style="background-color: value;">

    The value can be an English name of color, an RGB value of color, or a hexadecimal value of color.

    Examples of HTML Background

    Following are some example codes that show how to set different styles of background for an HTML document.

    • Setting Color for Background
    • Setting Image as Background
    • Background Repeat and Position
    • Setting Patterned Backgrounds

    Setting Color for Background

    An elementary method to modify the background is by altering its color. The background-color property facilitates the specification of a color for an element’s background. This can be accomplished by incorporating the following style attribute within the opening tag of an HTML element.

    Example

    The following is an example of setting color for background to a DIV:

    <!DOCTYPE html><html lang="en"><head><title>Styled Div Example</title></head><body><div style="background-color: #3498db; "><h1>Welcome to My Website</h1><p>
    
         This is an example of a styled div with a 
         background color and text color.
      &lt;/p&gt;&lt;!-- Additional content goes here --&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Image as Background

    HTML allows us to specify an image as the background of our HTML web page or table. The background and background-image can be used to control the background image of an HTML element, specifically page body and table backgrounds. We simply need to pass the path of an image as a value to both properties as illustrated in the next example. In the below example, the background-image property is assigned to the body of web page.

    Example

    The following is an example of setting an image as a background color; here we are setting an image to the background of the webage with the <body> tag:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>Background Image Example</title></head><body background="/market/public/assets/newDesign/img/logo.svg"><div style="background-color: rgba(255, 255, 255, 0.8); padding: 20px;"><h1>Welcome to My Website</h1><p>
    
         This is an example of setting a background 
         image using HTML attributes.
      &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Background Repeat and Position

    Although you can set an image as the background using just HTML, in order to control it's behavior, like repeating and positioning, we need to use CSS. We recommend watching our CSS background-image for better understanding. CSS offers options for controlling how background images repeat and their positioning. The background-repeat property specifies whether the image should repeat horizontally, vertically, both, or neither. Furthermore, the background-position property empowers developers to determine where the background image should be positioned within the element.

    Example

    The below HTML program creates a webpage with a centered content div having a repeating vertical background pattern from the specified image. The background color of the container is set to white by default, and the text within the container is styled with a black color, creating a visually appealing and cohesive design.

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>HTML Background Repeat</title><style>
    
      body {
         background-image: 
            url('/market/public/assets/newDesign/img/logo.svg');
         background-repeat: repeat-y;
         background-position: center;
         justify-content: center;
         align-items: center;
         display: flex;
      }
      div{
         background-color: rgba(255, 255, 255, 0.6); 
         padding: 20px;
      }
    </style></head><body><div><h1>Welcome to My Website</h1><p>
         This is an example of setting a background 
         image using HTML attributes.
      &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Patterned Backgrounds

    You might have seen many patterns or transparent backgrounds on various websites. This can simply be achieved by using a patterned image or transparent image in the background. It is suggested that while creating patterns or transparent GIF or PNG images, use the smallest dimensions possible, even as small as 1x1 to avoid slow loading.

    Example

    Here is an example of how to set the background pattern of a table.

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Background Images</title></head><body><!-- Set a table background using pattern --><table background = "/images/pattern1.gif" 
    
         width = "100%" 
         height = "100"&gt;&lt;tr&gt;&lt;td&gt;
         This background is filled up with a pattern image.
      &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;!-- Another example on table background using pattern --&gt;&lt;table background = "/images/pattern2.gif" 
         width = "100%" 
         height = "100"&gt;&lt;tr&gt;&lt;td&gt;
         This background is filled up with a pattern image.
      &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>
  • HTML – id

    The id is an important keyword in HTML. HTML “id” is an attribute used to uniquely identify an element within a web page. It serves as a label for that element and enables JavaScript and CSS to target it specifically.

    HTML id attribute is defined in the HTML code using the “id” keyword, and the styling is determined in CSS. This identification helps in applying custom styles, making interactive features, and navigating within the webpage with precision. The “id” values must be unique within the document.

    We highly recommend you to use classes to style any element through CSS..

    Syntax for id

    In CSS, you can target the id attribute by using a hash symbol (#) followed by the id name in HTML element. Try not to use id in CSS rather you can use class attribute. Ids are specially created to execute through JavaScript.

    • In HTML:<element class=”highlight”>…</element>
    • In CSS:/* CSS using id Attribute Selector */#highlight{background-color: yellow;color: black;font-weight: bold;}
    • In JavaScript:document.getElementById(‘highlight’)

    Using HTML id Attribute

    HTML ids are essential for managing events, and changing the structure of documents. In order to create interactive and dynamic web pages, it gives developers the ability to target particular parts and provide them specialized behavior and appearance. Rarely it is used to do the styling in CSS.

    Define a id for Styling

    In the following example, we have create two element one is h1 and other is p, and we set id on them as well “header” & “heightlight” but using the “heightlight” is in internal CSS to style our p element. You can use the “header” id in the similar way to style the h1 element.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
      &lt;!-- CSS id attribute Selector Used --&gt;
      #highlight {
         background-color: yellow;
         color: black;
         padding: 5px;
      }
    </style></head><body><!-- Using id attribute in both Element --><h1 id="header">Tutorialspoint</h1><p id="highlight">Simply Easy Learning</p></body></html>

    Using id Attribute through JavaScript

    The ids are frequently used to identify elements for JavaScript functions. For example, you can use a id to target specific elements, like paragraph, and make them interactive through JavaScript. In the following code we have create a button which will trigger a function that will change the display property none to block of a p element. You will see a paragraph.

    Open Compiler

    <!DOCTYPE html><html><head><script>
    
      function showContent() {
         var element = document.getElementById('content');
         if (element.style.display === 'none') {
            element.style.display = 'block';
         } else {
            element.style.display = 'none';
         }
      }
    </script><style>
      .interactive-button {
         background-color: #007bff;
         color: #fff;
         padding: 10px 20px;
         border: none;
         cursor: pointer;
      }
    </style></head><body><button class="interactive-button"
           onclick="showContent()"&gt;Click Me&lt;/button&gt;&lt;p class="content" style="display: none;"&gt;
       This content can be toggled by clicking the button.
    </p></body></html>

    Difference between id and class in HTML

    In HTML, the id attribute uniquely identifies a single element on a page, making it useful for targeting with CSS and JavaScript, and it must be unique within the document. The class attribute, on the other hand, can be applied to multiple elements, allowing for the grouping of elements that share common styles or behaviors.

    <!DOCTYPE html><html lang="en"><head><title>Difference between id and class</title><style>
    
      /* ID selector */
      #header {
         background-color: blue;
         color: white;
         padding: 10px;
      }
      /* Class selector */
      .button {
         background-color: green;
         color: white;
         padding: 5px 10px;
         margin: 5px;
      }
    </style></head><body><!-- Unique ID for the header --><div id="header">
         This is the header
      &lt;/div&gt;&lt;!-- Shared class for buttons --&gt;&lt;div class="button"&gt;
         Button 1
      &lt;/div&gt;&lt;div class="button"&gt;
         Button 2
      &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Things to Remember about id

    • The id attribute should contain at least one character should be there, the starting letter should be a character (a-z) or (A-Z), and the rest of the letters of any type can written even special characters.
    • The id attribute does not contain any spaces.
    • Within the document every id must be unique.

    Valid id Attributes Pattern

    Certain ID Attributes are valid in HTML 5, but not in Cascading Style Sheets. In such cases, it is recommended to go with simple output rather than styled output because certain values that we use for ID may be invalid in CSS.

    Following example demonstrates the use of simple ID attributes.

    Example

    If we execute below code, two div elements will be displayed, one with id attribute (TutorialsPoint Website), and the other one with other id attribute (Html5 Tutorials, CSS Tutorials, JavaScript Tutorials).

    <!DOCTYPE html><html><head><title>Simple Id Attributes</title><style>
    
      /* Remove @ from the code and run the code again */
      #@TP {
         color: #070770;
         text-align: center;
         font-size: 30px;
         font-weight: bold;
      }
      #@TP1 {
         text-align: center;
         font-size: 25px;
      }
    </style></head><body><div id="@TP">
      TutorialsPoint Website
    </div><div id="@TP1">
      Html5 Tutorials, CSS Tutorials, JavaScript Tutorials 
    </div></body></html>

    If we remove the @ symbol from the id's value the it will become avalid id declaration and applied styles will be effected on the HTML Element.

  • Meta Tags

    HTML <meta> tag lets us specify metadata, which is additional important information about a document, in a variety of ways. The META elements can be used to include name and content pairs describing properties of the HTML document, such as author, expiry date, a list of keywords, document author, etc.

    HTML <meta> tag can be used to provide extra information. It’s a self-closing element, meaning it doesn’t require a closing tag but carries information within its attributes. You can include one or more meta tags in your document based on what information you want to keep in your document, but in general, meta tags do not impact the physical appearance of the document, so from the appearance point of view, it does not matter if you include them or not.

    Adding Metadata to Web Pages Using Meta Tags

    You can add metadata to your web pages by placing <meta> tags inside the header of the document, which is represented by the <head> tag.

    The following metadata can be added using the <meta> tag:

    Below, you can check all the examples that are well described with the code for how we should use individuals on our website.

    Specifying Keywords

    You can use the <meta> tag to specify important keywords related to the document, and later these keywords are used by the search engines while indexing your webpage for searching purposes.

    Example

    Following is an example where we are adding “HTML, Meta Tags, and Metadata” as important keywords about the document:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /></head><body><p>Hello HTML5!</p></body></html>

    Document Description

    You can use the <meta> tag to give a short description about the document. This again can be used by various search engines while indexing your webpage for searching purposes.

    Example

    The following example demonstrates how you can define a meta description for a webpage:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /></head><body><p>Hello HTML5!</p></body></html>

    Document Revision Date

    You can use the <meta> tag to give information about the last time the document was updated. This information can be used by various web browsers while refreshing your webpage.

    Example

    The following example demonstrates how you can define a revision date:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="revised" content="Tutorialspoint, 3/7/2014" /></head><body><p>Hello HTML5!</p></body></html>

    Document Refreshing

    The <meta> tag can be used to specify a duration after which your web page will keep refreshing automatically.

    Example

    If you want your page to keep refreshing after every 5 seconds, then use the following syntax:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="revised" content="Tutorialspoint, 3/7/2014" /><meta http-equiv="refresh" content="5" /></head><body><p>Hello HTML5!</p></body></html>

    Page Redirection

    You can use the <meta> tag to redirect your page to any other webpage. You can also specify a duration if you want to redirect the page after a certain number of seconds.

    Example

    Following is an example of redirecting the current page to another page after 5 seconds. If you want to redirect the page immediately, then do not specify the content attribute:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="revised" content="Tutorialspoint, 3/7/2014" /><meta http-equiv="refresh" content="5; url=http://www.tutorialspoint.com" /></head><body><p>Hello HTML5!</p></body></html>

    Setting Cookies

    Cookies are data stored in small text files on your computer, and it is exchanged between a web browser and a web server to keep track of various information based on your web application needs.

    You can use the <meta> tag to store cookies on the client side, and later this information can be used by the Web server to track a site visitor. If you do not include the expiration date and time, the cookie is considered a session cookie and will be deleted when the user exits the browser.

    Example

    Following is an example of redirecting the current page to another page after 5 seconds. If you want to redirect the page immediately, then do not specify the content attribute.

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="revised" content="Tutorialspoint, 3/7/2014" /><meta http-equiv="cookie" content="userid=xyz; expires=Wednesday, 08-Aug-15 23:59:59 GMT;" /></head><body><p>Hello HTML5!</p></body></html>

    Note: You can check the PHPand Cookies tutorial for a complete detail on cookies.

    Setting Author Name

    You can set an author name on a web page using a <meta> tag. Author name be specified by assigning the “author” value to the “name” attribute.

    Example

    The following example demonstrates how you can set an author name:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="author" content="Mahnaz Mohtashim" /></head><body><p>Hello HTML5!</p></body></html>

    Specify Character Set

    You can use the <meta> tag to specify the character set used within the webpage. By default, Web servers and Web browsers use ISO-8859-1 (Latin1) encoding to process Web pages.

    Example

    Following is an example to set UTF-8 encoding:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="author" content="Mahnaz Mohtashim" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><p>Hello HTML5!</p></body></html>

    Example

    To serve the static page with traditional Chinese characters, the webpage must contain a <meta> tag to set Big5 encoding:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="author" content="Mahnaz Mohtashim" /><meta http-equiv="Content-Type" content="text/html; charset=Big5" /></head><body><p>Hello HTML5!</p></body></html>

  • HTML Computer Code Elements

    HTML computer code elements (tags) provide unique formatting and text styles for different types of code-related messages, such as keyboard input, preformatted text, code snippets, variables, and sample outputs. The computer code elements are listed as follows:

    • <kbd> Element
    • <pre> Element
    • <code> Element
    • <var> Element
    • <samp> Element

    HTML <kbd> Element

    The <kbd> tag is used to define the keyboard input. Use this when you want the user to type on their keyboard, for example, shortcut keys Ctrl+C for copy, Esc for exit, etc.

    Example

    Let us now see an example to implement the <kbd> tag:

    Open Compiler

    <!DOCTYPE html><html><body><h2>Example of kbd Tag</h2><p>Use the following shortcut keys:</p><p><strong>Cut</strong><kbd>CTRL</kbd>+<kbd>X</kbd></p><p><strong>Copy</strong><kbd>CTRL</kbd>+<kbd>C</kbd></p><p><strong>Paste</strong><kbd>CTRL</kbd>+<kbd>V</kbd></p><p><strong>Undo</strong><kbd>CTRL</kbd>+<kbd>Z</kbd></p></body></html>

    HTML <pre> Element

    The <pre> tag in HTML is used to set preformatted text. The text preserves spaces and line breaks, appearing the same on the web page as in the HTML code.

    Example

    Let us now see an example to implement the <pre> tag:

    Open Compiler

    <!DOCTYPE html><html><body><h2>Example of pre Tag</h2><pre>
      This is a demo text
      and will appear
      in the same format as
      it
      is visible
      here. The pre tag displays
      the text in a fixed-width
      font. It preserves
      both spaces and
      line breaks as you can see
      here.
    </pre></body></html>

    HTML <code> Element

    The <code> tag is used to format code in an HTML document. For example, to write and format Java code properly, use the <code> element.

    Example

    Let us now see an example to implement the <code> element:

    Open Compiler

    <!DOCTYPE html><html><body><h1>Example of code Tag</h1><h2>C++</h2><code>
    
         #include 
         &lt;iostream&gt;&lt;/code&gt;&lt;h2&gt;C&lt;/h2&gt;&lt;code&gt;
         #include 
         &lt;stdio.h&gt;&lt;/code&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    HTML <var> Element

    The <var> tag in HTML is used to display mathematical expressions for calculations.

    Example

    Let us now see an example to implement the <var> tag:

    Open Compiler

    <!DOCTYPE html><html><body><h2>Example of var Tag</h2>
    
      Sample equation  &lt;var&gt;2x&lt;/var&gt; - &lt;var&gt;2z&lt;/var&gt; = &lt;var&gt;3y&lt;/var&gt; + 9
    </body></html>

    HTML <samp> Element

    The <samp> tag is a phrase tag used to format text in an HTML document.

    Example

    Let us now see an example to implement the <samp> element:

    Open Compiler

    <!DOCTYPE html><html><body><h2>Exam Results</h2><p><s>Result would be announced on 6th June.</s></p><samp>New date for results is 7th June.</samp></body></html>

    HTML Computer Code Elements

    Here is the list of the computer code tags along with their descriptions used for defining user input and computer code:

    TagDescription
    <kbd>Defines keyboard input.
    <pre>Displays preformatted text with preserved spaces and line breaks.
    <code>Defines a piece of computer code.
    <var>Represents a variable in programming or math expressions.
    <samp>Defines sample output from a program or device.
  • Phrase Tags

    HTML phrase tags have been designed for specific purposes, though they are displayed in a similar way as other basic tags like <b>, <i>, <pre>, and <tt>. Here we will take you through all the important phrase tags; so let’s start seeing them one by one. Following is the list of phrase tags, some of them are discussed in HTML formatting and quotations.

    HTML Phrase Tags List

    • Emphasized Text – HTML em Tag
    • Marked Text – HTML mark Tag
    • Strong Text – HTML strong Tag
    • Abbreviation Text – HTML abbr Tag
    • Acronym Text – HTML acronym Tag
    • Directed Text – HTML bdo Tag
    • Special Terms – HTML dfn Tag
    • Short Quoting Text – HTML q tag
    • Long Quoting Text – HTML blockquote Tag
    • Citations Text – HTML cite Text
    • Computer Code Text – HTML code Tag
    • Keyboard Text – HTML kbd Text
    • Programming Variables – HTML pre Tag
    • Program Output – HTML samp Tag
    • Address Text – HTMl address Tag

    Below we have used each tags of phrase tags, each tag has it’s default styling few of them accepts some attributes as well.

    Emphasized Text

    Content that is enclosed within the <em>…</em> element is displayed as emphasized text. The <em> element typically renders text in italics, indicating emphasis.

    Example

    Open Compiler

    <!DOCTYPE html><body><p>The following word uses a <em>emphasized</em> typeface.</p></body></html>

    Output

    The following word uses a emphasized typeface.
    

    Marked Text

    Anything that is enclosed within the <mark>…</mark> element is displayed as marked with yellow ink.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>The following word has been <mark>marked</mark> with yellow.</p></body></html>

    Output

    The following word has been marked with yellow.
    

    Strong Text

    Content that is enclosed within the <strong>…</strong> element is displayed as important text. The <strong> element displays text in a bold font, indicating strong importance.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>The following word uses a <strong>strong</strong> typeface. </p></body></html>

    Output

    The following word uses a strong typeface.
    

    Abbreviation Text

    You can abbreviate a text by putting it inside opening <abbr> and closing </abbr> tags. If present, the title attribute must contain this full description and nothing else.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>My best friend's name is <abbr title="Abhishek">Abhy</abbr>. </p></body></html>

    Output

    My best friend's name is Abhy.
    

    Acronym Text

    The <acronym> element allows you to indicate that the text between <acronym> and </acronym> tags is an acronym.

    At present, the major browsers do not change the appearance of the content of the <acronym> element.

    The <acronym> element is deprecated in HTML5. Instead, you should use the <abbr> element to define abbreviations, and you can specify the full description using the “title” attribute.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>This chapter covers marking up text in <acronym>XHTML</acronym>. </p></body></html>

    Output

    This chapter covers marking up text in XHTML.
    

    Directed Text

    The <bdo>…</bdo> element stands for Bi-Directional Override, and it is used to override the current text direction.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>This text will go left to right.</p><p><bdo dir="rtl">This text will go right to left.</bdo></p></body></html>

    Output

    This text will go right to left.
    

    Special Terms

    The <dfn>…</dfn> element (or HTML Definition Element) allows you to specify that you are introducing a special term. Its usage is similar to italic words in the midst of a paragraph.

    Typically, you would use the <dfn> element the first time you introduce a key term. Most recent browsers render the content of a <dfn> element in an italic font.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>The following word is a <dfn>special</dfn> term. </p></body></html>

    Output

    The following word is a special term.
    

    Quoting Text

    When you want to quote a passage from another source, you should put it in between <blockquote>…</blockquote> tags.

    Text inside a <blockquote> element is usually indented from the left and right edges of the surrounding text and sometimes uses an italicized font.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>The following description of XHTML is taken from the W3C Web site:</p><blockquote>XHTML 1.0 is the W3C's first Recommendation for XHTML, following on from earlier work on HTML 4.01, HTML 4.0, HTML 3.2 and HTML 2.0.</blockquote></body></html>

    Output

    The following description of XHTML is taken from the W3C Web site:
    XHTML 1.0 is the W3C's first Recommendation for XHTML, following on 
    from earlier work on HTML 4.01, HTML 4.0, HTML 3.2 and HTML 2.0.
    

    Short Quotations

    The <q>…</q> element is used when you want to add a double quote within a sentence. By using <q>…</q>, you ensure that the enclosed text is presented as a direct quotation, enhancing readability and maintaining proper punctuation in your HTML document.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>Amit is in Spain, <q>I think I am wrong</q>. </p></body></html>

    Output

    Amit is in Spain, I think I am wrong.
    

    Text Citations

    If you are quoting a text, you can indicate the source by placing it between an opening <cite>tag and closing </cite> tag.

    As you would expect in a print publication, the content of the <cite> element is rendered in italicized text by default.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>This HTML tutorial is derived from <cite>W3 Standard for HTML</cite>. </p></body></html>

    Output

    This HTML tutorial is derived from W3 Standard for HTML.
    

    Computer Code

    Any programming code to appear on a Web page should be placed inside <code>…</code> tags. Usually the content of the <code> element is presented in a monospaced font, just like the code in most programming books.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>Regular text. <code>This is code.</code> Regular text. </p></body></html>

    Output

    Regular text. This is code. Regular text.
    

    Keyboard Text

    When you are talking about computers, if you want to tell a reader to enter some text, you can use the <kbd>…</kbd> element to indicate what should be typed in, as in this example.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>Regular text. <kbd>This is inside kbd element</kbd> Regular text. </p></body></html>

    Output

    Regular text. This is inside kbd element Regular text.
    

    Programming Variables

    The <var> element is usually used in conjunction with the <pre> and <code> elements to indicate that the content of that element is a variable.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p><code>document.write(" <var>user-name</var>") </code></p></body></html>

    Output

    document.write(" user-name") 

    Program Output

    The <samp>…</samp> element indicates sample output from a program, and script, etc. Again, it is mainly used when documenting programming or coding concepts.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>Result produced by the program is <samp>Hello World!</samp></p></body></html>

    Output

    Result produced by the program is Hello World!
    

    Address Text

    The <address>…</address> element is used to contain any address.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><address>388A, Road No 22, Jubilee Hills - Hyderabad</address></body></html>

    Output

    388A, Road No 22, Jubilee Hills - Hyderabad