Beginner's Guide to HTML: What You Need to Know
Table of contents
HTML (HyperText Markup Language) is the standard language used to create web pages. It consists of a series of elements (tags) used to structure content on a webpage. Below is a comprehensive guide with commonly used HTML tags, their descriptions, and examples.
Basic Structure of HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Example</title>
</head>
<body>
</body>
</html>
Explanation:
<!DOCTYPE html>
: Defines the document type (HTML5).<html>
: Root element of an HTML page.<head>
: Contains metadata (title, meta tags).<meta>
: Provides metadata like character set and viewport settings.<title>
: Sets the title of the page.<body>
: Contains the visible content.
Headings:
HTML provides six levels of headings, <h1>
being the highest and <h6>
the lowest.
<h1>This is an H1 Heading</h1>
<h2>This is an H2 Heading</h2>
<h3>This is an H3 Heading</h3>
Explanation:
- Used to create hierarchical headings in a document.
Paragraphs:
<p>This is a paragraph of text.</p>
Explanation:
- The
<p>
tag defines a paragraph of text.
Links:
<a href="https://www.example.com">Visit Example</a>
Explanation:
- The
<a>
tag is used to create hyperlinks. Thehref
attribute specifies the URL.
Images:
<img src="image.jpg" alt="Image description" width="500" height="300">
Explanation:
- The
<img>
tag is used to embed images. Thesrc
attribute provides the image URL, andalt
gives an alternate text if the image doesn’t load.
Lists:
Unordered list:
<ul> <li>Item 1</li> <li>Item 2</li> </ul>
Ordered list:
<ol> <li>First item</li> <li>Second item</li> </ol>
Explanation:
- The
<ul>
tag creates an unordered list, and<ol>
creates an ordered list. Inside the list, each item is wrapped in<li>
(list item).
Tables:
<table>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
</table>
Explanation:
The
<table>
tag defines a table.<tr>
represents a row,<th>
represents a table header, and<td>
represents table data.
Forms:
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<input type="submit" value="Submit">
</form>
Explanation:
The
<form>
tag defines a form for user input.<input>
is used for various types of inputs like text, buttons, etc.
Buttons:
<button type="button">Click Me!</button>
Explanation:
- The
<button>
tag defines a clickable button.
Divisions:
<div>
This is a division or section of content.
</div>
Explanation:
- The
<div>
tag is used as a container for other HTML elements, often used for styling or layout.
Span:
<span>This is an inline section.</span>
Explanation:
- The
<span>
tag is used for inline styling or grouping content without affecting the layout.
Input Types:
Text input:
<input type="text" name="username">
Password input:
<input type="password" name="password">
Checkbox:
<input type="checkbox" name="subscribe" checked> Subscribe to newsletter
Radio buttons:
<input type="radio" name="gender" value="male"> Male <input type="radio" name="gender" value="female"> Female
Textarea:
<textarea rows="4" cols="50">Enter text here...</textarea>
Explanation:
- The
<textarea>
tag is used for multi-line text input.
Semantic Elements:
Header:
<header> <h1>Website Header</h1> </header>
Nav:
<nav> <a href="#home">Home</a> <a href="#services">Services</a> </nav>
Section:
<section> <h2>Section Title</h2> <p>This is a section of content.</p> </section>
Footer:
<footer> <p>Website Footer</p> </footer>
Media:
Audio:
<audio controls> <source src="audio.mp3" type="audio/mp3"> Your browser does not support the audio element. </audio>
Video:
<video controls> <source src="video.mp4" type="video/mp4"> Your browser does not support the video element. </video>
Iframes:
<iframe src="https://www.example.com" width="600" height="400"></iframe>
Explanation:
- The
<iframe>
tag is used to embed another webpage inside the current page.
Blockquote:
<blockquote cite="https://www.example.com">
This is a blockquote from another source.
</blockquote>
Explanation:
- The
<blockquote>
tag is used for quoting external content.
Comments:
<!-- This is a comment -->
Explanation:
- Comments are not displayed in the browser and are used to leave notes within the code.
Code Block:
<pre>
<code>
console.log('Hello World');
</code>
</pre>
Explanation:
<pre>
is used to preserve formatting, and<code>
is for inline code.
Here’s a breakdown of the different attributes for anchor (<a>
) tags and input (<input>
) tags in HTML:
1. Anchor (<a>
) Tag Attributes:
The <a>
tag is used to create hyperlinks, and several attributes control its behavior.
Common Attributes for <a>
:
href
: Specifies the URL of the page or resource the link points to.<a href="https://example.com">Visit Example</a>
target
: Specifies where to open the linked document._self
: Opens in the same tab (default)._blank
: Opens in a new tab._parent
: Opens in the parent frame._top
: Opens in the full body of the window.
<a href="https://example.com" target="_blank">Open in New Tab</a>
rel
: Defines the relationship between the current page and the linked resource.nofollow
: Instructs search engines not to follow the link.noopener
: Prevents new tabs from controlling the originating page (used withtarget="_blank"
).noreferrer
: Prevents referrer information from being sent.
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Secure Link</a>
download
: Specifies that the target should be downloaded when the link is clicked.<a href="document.pdf" download>Download Document</a>
type
: Specifies the MIME type of the linked document.<a href="file.pdf" type="application/pdf">PDF File</a>
title
: Provides additional information (displayed as a tooltip when hovered over).<a href="https://example.com" title="Go to Example">Example Link</a>
ping
: Specifies a list of URLs to notify (via POST request) when the hyperlink is clicked, for tracking purposes.<a href="https://example.com" ping="https://tracker.com">Trackable Link</a>
id
andclass
: Used to uniquely identify an anchor or apply styles and scripts.<a href="https://example.com" id="link1" class="btn">Styled Link</a>
name
: Defines a bookmark inside the page. When combined withhref="#name"
, it allows for internal page navigation.<a name="section1">Section 1</a> <a href="#section1">Go to Section 1</a>
2. Input (<input>
) Tag Attributes:
The <input>
tag is used to create various types of form controls, like text fields, checkboxes, radio buttons, etc. It supports a wide range of attributes that control its functionality.
Common Attributes for <input>
:
type
: Specifies the type of input field. Common types include:text
: Single-line text input.password
: Input field for passwords (hidden characters).checkbox
: A checkbox.radio
: A radio button.submit
: A submit button.reset
: A reset button.file
: Allows file upload.email
: Email input with validation.number
: Input for numeric values.date
: Date selection input.
<input type="text" />
name
: Specifies the name of the input, used for form submission.<input type="text" name="username" />
value
: Specifies the default value for the input field.<input type="text" value="Default Text" />
placeholder
: Provides a hint or example text inside the input field.<input type="text" placeholder="Enter your name" />
id
: Unique identifier for the input element, used with labels and for targeting by CSS/JavaScript.<input type="text" id="username" />
class
: Used to apply specific styles or scripts to the input element.<input type="text" class="form-control" />
required
: Specifies that the input field must be filled out before submitting the form.<input type="email" required />
readonly
: Makes the input field read-only, meaning the user cannot change its value.<input type="text" value="Read-only text" readonly />
disabled
: Disables the input field, making it unclickable and non-submittable.<input type="text" value="Disabled text" disabled />
maxlength
: Specifies the maximum number of characters allowed in the input.<input type="text" maxlength="20" />
min
andmax
: Specifies the minimum and maximum values for numeric or date input fields.<input type="number" min="1" max="10" />
step
: Specifies the stepping interval for numeric or date input fields.<input type="number" step="5" />
pattern
: Specifies a regular expression that the input’s value must match for form validation.<input type="text" pattern="[A-Za-z]{3,}" title="Only letters, min 3 characters" />
autofocus
: Automatically focuses the input field when the page loads.<input type="text" autofocus />
autocomplete
: Specifies whether the browser should automatically fill in the field with previously entered values.<input type="text" autocomplete="on" />
multiple
: Allows the user to select multiple values (usually used withfile
andemail
input types).<input type="file" multiple />
size
: Specifies the visible width of a text input.<input type="text" size="30" />
minlength
: Specifies the minimum number of characters required.<input type="text" minlength="10" />
list
: Associates the input field with a<datalist>
element, providing predefined options for the input.<input type="text" list="languages"> <datalist id="languages"> <option value="JavaScript"> <option value="Python"> </datalist>
form
: Associates the input element with a specific form (useful when the input is outside of the form).<input type="text" form="myForm" /> <form id="myForm"> <!-- Form elements --> </form>
Example Using Both Anchor and Input Tags:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Anchor and Input Example</title>
</head>
<body>
<!-- Example of anchor tag -->
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Visit Example</a>
<!-- Example of input fields -->
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your name" required />
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter your email" required />
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="18" max="100" />
<input type="submit" value="Submit" />
</form>
</body>
</html>