Author: Saim Khalid

  • 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.

    <!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:

    <!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:

    <!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:

    <!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:

    <!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:

    <!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:

    <!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.

  • 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”:

    <!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:

    <!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:

    <!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:

    <!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:

    <!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.

    <!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.

    <!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:

    <!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:

    <!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>

  • 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″.

    <!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:

    <!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:

    <!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:

    <!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:

    <!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:

    <!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:

    <!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.Try It

  • 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

    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:

    <!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.

    <!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.

    <!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>
  • 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.

    <!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.

    <!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.

  • Classes

    The class is an important keyword in HTML. It is an attribute that can be applied to one or more elements and is used to style and categorize elements based on common characteristics or purpose. Classes allows multiple elements to share the same styling rules. By assigning the same class to multiple elements, you can apply CSS styles or JavaScript functionality to all of them simultaneously. This promotes consistency in design and layout, making it easier to manage and update a website.

    HTML class attribute is defined in the HTML code using the “class” keyword, and the styling is determined in CSS. This separation of content and style is a key principle in web design, facilitating the creation of visually appealing and organized web pages.

    Syntax for Class

    To create a CSS rule for HTML elements using class attribute in CSS write a (.) followed by the class name mentioned in HTML element, the we can define the CSS prpeties with curly braces in key: value; format like color: yellow;.

    In this code, we’ve selected a class named “highlight” that changes the background color, text color, and font weight of the elements it’s applied to.

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

    Using HTML Class Attribute

    HTML classes are essential for styling and formatting web page elements consistently. They allow you to apply the same styles to multiple elements without repeating code, promoting maintainability and a cohesive design. The class attribute can be used on any HTML Elements(Except elements placed in head element). Here’s how to use classes effectively with a practical example.

    Define a Class for Styling

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

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

    Multiple classes

    We can apply multiple classes to a single element by separating class names with a space.

    In the following example, the <h1> element has two classes applied “heading” and “content.” This is achieved using a space to separate the class names within the class attribute.

    Multiple classes can be applied to the same element to inherit styling from both classes. In this case, “heading” class provides a large font size and center alignment, while the “content” class provides a specific text color and line-height.

    <!DOCTYPE html><html><head><style>
    
        .heading {
            font-size: 24px;
            color: #333;
            text-align: center;
        }
        .content {
            font-size: 16px;
            color: #666;
            line-height: 1.5;
        }
        .button {
            background-color: #007bff;
            color: #fff;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;!-- Defined two Classes in h1 Element --&gt;&lt;h1 class="heading content"&gt;
        Welcome to Tutorialspoint
    &lt;/h1&gt;&lt;p class="content"&gt;
        We make Tutorials - Simply Easy Learning
    &lt;/p&gt;&lt;button class="button"&gt;Click Me&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Same class on Multiple Elements

    The most important feature of classes is their reusability. You can apply the same class to multiple elements to maintain a consistent look throughout your website. Here in the following example we create 2 p elements(paragraphs). Both of these paragraphs will have the same highlighting because they share the "highlight" class.

    <!DOCTYPE html><html><head><style>
    
      .highlight {
         background-color: yellow;
         color: black;
         font-weight: bold;
      }
    </style></head><body><p class="highlight">
      To create a class, you need to define it within
      your HTML document or link to an external CSS 
      file that contains class definitions. Classes 
      are defined using the "class" attribute.
    </p><p class="highlight">
      HTML classes are essential for styling and formatting
      web page elements consistently. They allow you to apply
      the same styles to multiple elements without repeating 
      code, promoting maintainability and a cohesive design.
    </p></body></html>

    Using class Attribute through JavaScript

    HTML classes are versatile and serve various purposes beyond styling.

    The classes are frequently used to identify elements for JavaScript functions. For example, you can use a class to target specific elements, like buttons, 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.

    <!DOCTYPE html><html><head><script>
    
      function showContent() {
         var element = document.getElementsByClassName('content')[0];
         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>

    Things to Remember about Class

    • More than 1 class can be define on any HTML element.
    • Class are used by CSS and JavaScript both to select the element.
    • The class is case sensitive so be careful when you are using to select the element.
    • Multiple elements can have the same class as well.
    • In CSS we use .className and in JavaScript getElementsByClassName() method to select the class assigned 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:

    <!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:

    <!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:

    <!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:

    <!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:

    <!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.

    <!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:

    <!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:

    <!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:

    <!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>
  • 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:

    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:

    <!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:

    <!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:

    <!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:

    <!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:

    <!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

    <!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

    <!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

    <!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

    <!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

    <!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

    <!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

    <!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

    <!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

    <!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

    <!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

    <!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

    <!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

    <!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

    <!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

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

    Output

    388A, Road No 22, Jubilee Hills - Hyderabad
    
  • Iframes

    HTML iframe is an inline frame that allows you to embed another document within the current HTML document. Whenever you want to display another webpage within the webpage, you can use an iframe. 

    Creating iframe (Inline Frame)

    In HTML, the inline frame is defined with the <iframe> tag. This tag creates a rectangular region at a specified place within the HTML document in which the browser can display an external document such as a map or another web page.

    Iframe Syntax

    The following is the syntax to create an inline frame (iframe) in HTML:

    <iframe src="url" title="description"></iframe>

    The src Attribute

    The URL or path of the external document is attached using the src attribute of the <iframe> tag. If the content of the iframe exceeds the specified rectangular region, HTML automatically includes the scrollbars. HTML allows any number of iframes, but it may affect the performance of the website.

    Iframe Example

    The following example demonstrates how you can create an iframe in HTML:

    <!DOCTYPE html><html><head><title>HTML Iframes</title></head><body><p>It is an example of HTML Iframe</p><iframe src="/html/menu.htm"> Sorry your browser does not support inline frames. </iframe></body></html>

    The <iframe> Tag Attributes

    The following table describe the attributes used with the <iframe> tag.

    S.No.Attribute & Description
    1srcThis attribute is used to give the file name that should be loaded in the frame. Its value can be any URL. For example, src=”/html/top_frame.htm” will load an HTML file available in html directory.
    2nameThis attribute allows to give a name to a specific frame. It is used to indicate which frame a document should be loaded into. This is important when you want to create links in one frame that load pages into an another frame, in which case the second frame needs a name to identify itself as the target of the link.
    3heightThis attribute specifies the height of <iframe>. By default it is 150 pixels.
    4widthThis attribute specifies the width of <iframe>. By default it is 300 pixels.
    5allowIt is used to specify the permission policies to access features like microphone and camera.
    6loadingIt specifies the time to load a given iframe.

    Setting Height and Width of Iframes

    You can set the height and width of an HTML iframe by using the height and width attributes of the <iframe> tag.

    Example

    The following example demonstrates how you can set the height and width of an iframe:

    <!DOCTYPE html><html><head><title>HTML Iframes</title></head><body><h2>Example of Setting Height and width of HTML Iframe</h2><iframe src="/index.htm" width="500" height="300"> 
    
    Sorry your browser does not support inline frames. 
    &lt;/iframe&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The above code will display the "index.htm" webpage in an iframe with the specified height and width.

    Linking to an Iframe: Target and Name Attributes

    You can use an iframe as a target frame to open a webpage on clicking a link.

    You can create a target iframe for a link (hyperlink) by using the name attribute of the <iframe> tag. The value of the name attribute is used in the target attribute of elements like <form> and <a> to specify the target frame.

    Example

    The following example demonstrates how you can make a target iframe for a hyperlink:

    <!DOCTYPE html><html><head><title>HTML Iframes</title></head><body><h2>Linking to an Iframe: Target and Name Attributes</h2><p>Click on the link below to load the content inside the specified frame...</p><p><a href="/html/html_iframes.htm" target="Iframe">
    
      Iframe Tutorial
      &lt;/a&gt;&lt;/p&gt;&lt;iframe style="background-color: skyblue;" name="Iframe" width="500" height="300"&gt;
    Sorry your browser does not support inline frames.
    &lt;/iframe&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    On execution, the above code will generate a link and an Iframe with a sky-blue background. When the link is clicked, its content will open inside the iframe.

    Styling Iframe

    You can also use the style or class attributes to apply the CSS rules on an iframe.

    Example

    The following example demonstrates how you can apply CSS styles to an iframe:

    <!DOCTYPE html><html><head><title>HTML Iframes</title><style type="text/css">
    
      body{
        background-color: #FFF4A3;
      }
      .my_iframe{
        width: 90%;
        height: 180px;
        border: 2px solid #f40;
        padding: 8px;
      }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Example of Styling Iframe&lt;/h2&gt;&lt;iframe src="/index.htm" class="my_iframe"&gt; 
    Sorry your browser does not support inline frames. 
    &lt;/iframe&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Multiple Iframes

    You can embed multiple documents (webpages) within a webpage. HTML allows you to use multiple <iframe> tags in an HTML document.

    Note: Use of multiple iframes may slow down your page loading speed.

    Example

    In the following example, we are embedding three webpages using multiple iframes:

    <!DOCTYPE html><html><head><title>HTML Iframes</title><style type="text/css">
    
      body{
        background-color: #FFF4A3;
      }
      .my_iframe{
        width: 90%;
        height: 180px;
        border: 2px solid #f40;
        padding: 8px;
        margin-bottom: 8px;
      }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Example of Multiple Iframes&lt;/h2&gt;&lt;h3&gt;Index Page&lt;/h3&gt;&lt;iframe src="/index.htm" class="my_iframe"&gt; 
    Sorry your browser does not support inline frames. 
    &lt;/iframe&gt;&lt;h3&gt;Tutorials Library&lt;/h3&gt;&lt;iframe src="/tutorialslibrary.htm" class="my_iframe"&gt; 
    Sorry your browser does not support inline frames. 
    &lt;/iframe&gt;&lt;h3&gt;Compilers&lt;/h3&gt;&lt;iframe src="/codingground.htm" class="my_iframe"&gt; 
    Sorry your browser does not support inline frames. 
    &lt;/iframe&gt;&lt;/body&gt;&lt;/html&gt;</pre>