HTML provides several tags for formatting text. These tags change the appearance, structure, or meaning of text content on a webpage. Below are the most common text formatting tags and their explanations:
<b> makes text bold for visual styling, while <strong> emphasizes the importance of the text (with bold styling by default).
<b>This text is bold.</b>
<strong>This text is important.</strong>
<i> makes text italic for visual styling, while <em> emphasizes the importance of the text (with italic styling by default).
<i>This text is italic.</i>
<em>This text is emphasized.</em>
The <mark> tag highlights text by applying a background color (typically yellow).
<mark>This text is highlighted.</mark>
The <small> tag renders text in a smaller font size than the surrounding text.
<small>This is small text.</small>
The <del> tag indicates deleted text with a strikethrough, while the <ins> tag indicates inserted text with an underline (depending on the browser).
<del>This text was removed.</del>
<ins>This text was added.</ins>
The <sub> tag renders text as a subscript (below the baseline), while <sup> renders text as a superscript (above the baseline).
H<sub>2</sub>O (Water molecule)
X<sup>2</sup> (X squared)
The <q> tag is used for short inline quotes, while the <blockquote> tag is used for longer, block-level quotations.
<q>This is an inline quote.</q>
<blockquote>
This is a block-level quote that spans multiple lines.
</blockquote>
The <abbr> tag defines an abbreviation or acronym, and the title attribute provides the full term.
<abbr title="HyperText Markup Language">HTML</abbr>
<code> displays inline code, <pre> preserves whitespace and displays preformatted text, and <kbd> is used to represent keyboard input.
<code>print('Hello World')</code>
<pre>
for i in range(5):
print(i)
</pre>
<kbd>Ctrl + C</kbd>
The <cite> tag is used to reference the title of a work, such as a book, movie, or research paper. It renders the text in italic by default.
<cite>The Great Gatsby</cite>
The <u> tag underlines text. Although it was deprecated in older HTML versions, it is now used to denote non-link underlined content.
<u>This text is underlined.</u>
HTML allows embedding of links and multimedia elements like images, audio, video, and external content. Below are the main tags used for links and media:
The <a> tag defines a hyperlink, which can link to external websites, internal pages, or sections within the same page.
<a href="https://example.com">Visit Example</a>
<a href="#top">Go to Top of the Page</a>
Attributes of <a> Tag:
href: The URL of the link.target="_blank": Opens the link in a new tab.rel="noopener noreferrer": Prevents security issues with new tabs.The <img> tag embeds images into a webpage. It requires the src attribute to specify the image location and the alt attribute for accessibility.
<img src="image.jpg" alt="A beautiful scenery">
<img src="logo.png" alt="Website Logo" width="150" height="100">
Attributes of <img> Tag:
src: Specifies the image source.alt: Provides alternative text for accessibility.width and height: Set the image dimensions.The <audio> tag embeds audio content. It supports multiple audio formats and provides playback controls.
<audio controls>
<source src="audio.mp3" type="audio/mp3">
Your browser does not support the audio element.
</audio>
Attributes of <audio> Tag:
controls: Adds play, pause, and volume controls.autoplay: Plays the audio automatically (not recommended).loop: Repeats the audio in a loop.The <video> tag embeds video content with controls for playback.
<video width="320" height="240" controls>
<source src="video.mp4" type="video/mp4">
Your browser does not support the video element.
</video>
Attributes of <video> Tag:
controls: Adds playback controls.autoplay: Plays the video automatically.loop: Repeats the video.muted: Starts the video without sound.The <iframe> tag embeds external content, such as YouTube videos or Google Maps, within a webpage.
<iframe width="560" height="315"
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
frameborder="0"
allow="autoplay; encrypted-media" allowfullscreen>
</iframe>
Attributes of <iframe> Tag:
src: Specifies the URL of the embedded content.width and height: Set the dimensions of the iframe.frameborder: Specifies the border of the iframe (0 for no border).allowfullscreen: Allows fullscreen mode for videos.The <embed> tag embeds external resources like PDFs or multimedia files into a webpage.
<embed src="document.pdf" width="600" height="400" type="application/pdf">
The <object> tag embeds multimedia files or applications (like Flash or PDFs) into the webpage.
<object data="file.pdf" type="application/pdf" width="600" height="400">
Your browser does not support embedded PDFs.
</object>
The <svg> tag is used to create vector-based graphics that can scale without losing quality.
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
The <canvas> tag allows drawing graphics on a webpage using JavaScript.
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000">
Your browser does not support the canvas element.
</canvas>
<script>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#FF0000';
ctx.fillRect(10, 10, 150, 80);
</script>
HTML provides various ways to display content as lists. These lists help structure information and improve readability. Below are the main types of lists with examples:
An unordered list is used to display items in a bulleted format. Each item in the list is defined using the <li> tag.
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Cherry</li>
</ul>
Result:
An ordered list displays items in a numbered format. Each item is defined using the <li> tag. You can change the numbering type using the type attribute.
<ol type="1">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ol>
<ol type="A">
<li>First</li>
<li>Second</li>
</ol>
Result:
A description list is used for key-value pairs, such as terms and their descriptions. The <dl> tag wraps the list, <dt> defines a term, and <dd> provides its description.
<dl>
<dt>HTML</dt>
<dd>HyperText Markup Language</dd>
<dt>CSS</dt>
<dd>Cascading Style Sheets</dd>
</dl>
Result:
HTML allows lists to be nested inside other lists to create multi-level structures.
<ul>
<li>Fruits
<ul>
<li>Apple</li>
<li>Banana</li>
</ul>
</li>
<li>Vegetables
<ul>
<li>Carrot</li>
<li>Broccoli</li>
</ul>
</li>
</ul>
Result:
The <ol> tag supports the following attributes:
type: Specifies the type of numbering (e.g., 1 for numbers, A for uppercase letters).start: Specifies the starting number for the list.reversed: Reverses the numbering, starting from the highest number.
<ol type="I" start="5" reversed>
<li>Five</li>
<li>Four</li>
</ol>
Result:
Here’s a quick example of all list types together:
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<ol>
<li>Step 1</li>
<li>Step 2</li>
</ol>
<dl>
<dt>HTML</dt>
<dd>HyperText Markup Language</dd>
</dl>
Tables in HTML allow you to display data in a structured, grid-like format. Below are the key tags and attributes used in HTML tables:
The <table> tag defines the entire table structure. Inside it, rows are created with <tr>, and data cells with <td> or <th> for headers.
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>24</td>
</tr>
<tr>
<td>Bob</td>
<td>22</td>
</tr>
</table>
Result:
| Name | Age |
|---|---|
| Alice | 24 |
| Bob | 22 |
<th> (Table Header) defines a header cell, usually displayed in bold. <td> (Table Data) defines a regular data cell.
Use the border attribute to add borders to your table.
<table border="1">
<tr>
<th>Product</th>
<th>Price</th>
</tr>
<tr>
<td>Laptop</td>
<td>$800</td>
</tr>
</table>
The <caption> tag defines a title or caption for the table, which appears at the top.
<table border="1">
<caption>Student Grades</caption>
<tr>
<th>Student</th>
<th>Grade</th>
</tr>
<tr>
<td>Alice</td>
<td>A</td>
</tr>
</table>
Result:
| Student | Grade |
|---|---|
| Alice | A |
The colspan attribute allows a cell to span multiple columns, and the rowspan attribute spans multiple rows.
<table border="1">
<tr>
<th colspan="2">Monthly Report</th>
</tr>
<tr>
<td>Revenue</td>
<td>$10,000</td>
</tr>
<tr>
<td rowspan="2">Expenses</td>
<td>$2,000</td>
</tr>
<tr>
<td>$1,500</td>
</tr>
</table>
Result:
| Monthly Report | |
|---|---|
| Revenue | $10,000 |
| Expenses | $2,000 |
| $1,500 | |
The <thead> tag defines the header section, <tbody> contains the main data, and <tfoot> defines the footer.
<table border="1">
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Laptop</td>
<td>$800</td>
</tr>
<tr>
<td>Phone</td>
<td>$500</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td>$1,300</td>
</tr>
</tfoot>
</table>
Result:
| Product | Price |
|---|---|
| Laptop | $800 |
| Phone | $500 |
| Total | $1,300 |
You can style tables using CSS to improve their appearance.
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
}
th {
background-color: #f2f2f2;
text-align: left;
}
</style>
HTML forms allow users to input data and interact with web pages. Forms are essential for login pages, search boxes, surveys, and more. Below are the key elements used in HTML forms:
The <form> tag creates a form and typically includes attributes like action (where the form data will be sent) and method (GET or POST).
<form action="/submit" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="user_name">
<input type="submit" value="Submit">
</form>
Result:
The <input> tag creates various types of input elements using the type attribute.
<form>
<input type="text" name="name" placeholder="Enter your name">
<input type="password" name="password" placeholder="Enter your password">
<input type="email" name="email" placeholder="Enter your email">
<input type="number" name="age" min="1" max="100">
<input type="date" name="dob">
<input type="checkbox" name="agree" value="yes"> I agree
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
<input type="submit" value="Submit">
</form>
The <textarea> tag creates a multi-line text input.
<form>
<label for="message">Message:</label>
<textarea id="message" name="user_message" rows="4" cols="50"></textarea>
<input type="submit" value="Send">
</form>
The <select> tag creates a dropdown menu with options defined using <option>.
<form>
<label for="cars">Choose a car:</label>
<select id="cars" name="car">
<option value="volvo">Volvo</option>
<option value="bmw">BMW</option>
<option value="audi">Audi</option>
</select>
<input type="submit" value="Submit">
</form>
The <button> tag creates a clickable button, which can be used inside or outside forms.
<form>
<button type="submit">Submit Form</button>
<button type="button" onclick="alert('Button Clicked')">Click Me</button>
</form>
The <input type="file"> allows users to upload files.
<form action="/upload" method="POST" enctype="multipart/form-data">
<label for="file">Upload File:</label>
<input type="file" id="file" name="user_file">
<input type="submit" value="Upload">
</form>
You can use attributes like required, minlength, maxlength, and pattern for basic validation.
<form>
<input type="text" name="username" required minlength="5" placeholder="Username (min 5 chars)">
<input type="email" name="email" required placeholder="Valid Email">
<input type="submit" value="Register">
</form>
method="POST" to specify how data should be encoded (e.g., multipart/form-data for file uploads).Semantic elements clearly describe their meaning to both the browser and the developer. They enhance readability and accessibility, improving SEO and page structure.
The <header> tag represents the introductory content or a set of navigation links for a page or section.
<header>
<h1>Welcome to My Website</h1>
<nav>
<a href="#home">Home</a> |
<a href="#about">About</a> |
<a href="#contact">Contact</a>
</nav>
</header>
The <nav> tag is used to define a block of navigation links, typically at the top or side of a webpage.
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
The <main> tag contains the primary content of a webpage. There should be only one <main> element per page.
<main>
<article>
<h2>Latest News</h2>
<p>Here is the latest news from the tech world...</p>
</article>
</main>
The <article> tag defines independent, self-contained content that could stand alone (e.g., blog posts, news articles).
<article>
<h2>My First Blog Post</h2>
<p>This is my first blog post on web development...</p>
</article>
The <section> tag defines a section of content, often grouped by topic. It can contain headers, paragraphs, and articles.
<section>
<h2>About Us</h2>
<p>We are a team of developers building innovative solutions.</p>
</section>
The <aside> tag defines content that is related to the main content but not essential, like sidebars, advertisements, or related links.
<aside>
<h3>Related Articles</h3>
<ul>
<li><a href="#article1">How to Learn HTML</a></li>
<li><a href="#article2">CSS Basics</a></li>
</ul>
</aside>
The <footer> tag defines the footer of a section or page, containing copyright information, links, or contact details.
<footer>
<p>Copyright © 2024 MyWebsite. All Rights Reserved.</p>
<nav>
<a href="#privacy">Privacy Policy</a> |
<a href="#terms">Terms of Service</a>
</nav>
</footer>
The <figure> tag is used to group media content (e.g., images), and <figcaption> provides a caption for the content.
<figure>
<img src="image.jpg" alt="Sample Image">
<figcaption>This is a sample image.</figcaption>
</figure>
HTML supports various multimedia elements to embed audio, video, and other interactive content directly within a webpage. Below are the essential multimedia elements:
The <img> tag is used to embed images. It requires a src attribute to specify the image source and an alt attribute to describe the image.
<img src="example.jpg" alt="Sample Image" width="300" height="200">
Result:
The <audio> tag is used to embed audio files. It supports multiple audio formats like MP3, WAV, and OGG.
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
<source src="audio.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>
Result:
The <video> tag is used to embed video files. You can include multiple formats for cross-browser compatibility.
<video width="400" controls>
<source src="video.mp4" type="video/mp4">
<source src="video.ogg" type="video/ogg">
Your browser does not support the video element.
</video>
Result:
The <iframe> tag embeds another webpage or multimedia content (like YouTube videos) within the current page.
<iframe width="560" height="315"
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="YouTube video player"
frameborder="0"
allowfullscreen>
</iframe>
Result:
The <embed> tag allows you to embed external content, such as PDFs or Flash files, in the webpage.
<embed src="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
width="500" height="400" type="application/pdf">
The <object> tag can be used to embed multimedia elements or external resources such as videos, images, or Java applets.
<object data="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
type="application/pdf" width="500" height="400">
Your browser does not support the object element.
</object>
The <picture> tag provides multiple sources for an image, allowing the browser to select the best option based on screen size or format.
<picture>
<source srcset="image-small.jpg" media="(max-width: 600px)">
<source srcset="image-large.jpg" media="(min-width: 601px)">
<img src="image.jpg" alt="Responsive Image">
</picture>