Beginner's HTML Guide: Understanding and Using Basic Tags and Attributes
π HTML Basics: Building Blocks of the Web
Welcome to the world of HTML (Hypertext Markup Language)! π HTML is the language used to create the structure of web pages, allowing us to build everything from simple text to complex, interactive websites. Letβs explore the basic tags that form the foundation of every webpage! ποΈ
π§ What is HTML?
HTML is the code that structures the content of a webpage. It tells the browser how to display text, images, links, and other elements. Itβs made up of tags (enclosed in < >
), which are the building blocks of web pages.
π‘ HTML Document Structure
Every HTML document starts with a basic structure:
<!DOCTYPE html>
<html>
<head>
<title>My First Web Page</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is my first webpage!</p>
</body>
</html>
Letβs break it down:
<!DOCTYPE html>
: Tells the browser this is an HTML5 document.<html>
: The root element. It wraps all other HTML content.<head>
: Contains meta information like the page title and links to stylesheets or scripts.<title>
: The title that appears in the browser tab.<body>
: The main content of the page (text, images, links, etc.).
π Common HTML Tags
Letβs go over the most commonly used HTML tags with examples! π₯
1. <h1>
to <h6>
β Headings π’
These tags are used to create headings of different sizes.
<h1>
is the largest, and<h6>
is the smallest.<h1>This is a Main Heading</h1> <h2>This is a Subheading</h2> <h3>Another Subheading</h3>
- π‘ Pro tip: Use headings to structure your content clearly.
<h1>
is usually reserved for the pageβs main title.
- π‘ Pro tip: Use headings to structure your content clearly.
2. <p>
β Paragraphs π
The
<p>
tag is used for regular text or paragraphs.<p>This is a paragraph of text. You can write as much as you want here!</p>
3. <a>
β Links π
The
<a>
tag creates hyperlinks to other web pages or sections of the same page.<a href="https://www.example.com">Visit Example Website</a>
href
is an attribute that specifies the linkβs destination.
4. <img>
β Images πΌοΈ
Use the
<img>
tag to add images to your page.<img src="image.jpg" alt="A description of the image">
src
: The source of the image (URL or file path).alt
: A description of the image for accessibility.
5. <ul>
, <ol>
, and <li>
β Lists π
<ul>
creates an unordered (bulleted) list, while<ol>
creates an ordered (numbered) list.<li>
represents each list item.<ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <ol> <li>First Item</li> <li>Second Item</li> <li>Third Item</li> </ol>
6. <strong>
and <em>
β Emphasis βοΈ
<strong>
: Bold text for strong emphasis.<em>
: Italic text for emphasis.<p>This is <strong>very important</strong> text.</p> <p>This text is <em>italicized for emphasis</em>.</p>
7. <br>
β Line Breaks π
Use the
<br>
tag to insert a line break (a new line of text).<p>This is a line.<br>This is another line.</p>
8. <div>
and <span>
β Containers π§°
<div>
: A block-level element used to group content together (usually for styling or layout).<span>
: An inline element for small parts of content (usually for styling specific text).<div> <h2>Section Title</h2> <p>This is a section of content.</p> </div> <p>This is a <span style="color: blue;">blue word</span> in a sentence.</p>
9. <table>
, <tr>
, <th>
, and <td>
β Tables π
Use these tags to create tables with rows and columns.
<table> <tr> <th>Name</th> <th>Age</th> </tr> <tr> <td>John</td> <td>25</td> </tr> <tr> <td>Jane</td> <td>30</td> </tr> </table>
<th>
: Table header.<td>
: Table data (cells).
10. <form>
, <input>
, and <button>
β Forms π
HTML forms allow users to submit data to a server.
<form action="/submit-form" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name"> <label for="email">Email:</label> <input type="email" id="email" name="email"> <button type="submit">Submit</button> </form>
<input>
: Accepts user input (text, email, password, etc.).<button>
: Creates a clickable button.
π HTML Attributes
HTML tags can have attributes that provide additional information about an element. Here are some common attributes:
class
: Assigns a class for styling with CSS.id
: Assigns a unique identifier to an element.style
: Applies inline styles (though it's better to use CSS for this).
π HTML Attributes: Adding Power to Your Tags
In HTML, attributes are used to provide additional information about an element. They are always included inside the opening tag of an element, following the tag name. Attributes usually come in name/value pairs, meaning the attribute name is followed by an equal sign (=
) and the attribute value enclosed in quotes.
Hereβs an example:
<a href="https://example.com">Click here to visit Example</a>
In this example, href
is an attribute of the <a>
(anchor) tag, and "
https://example.com
"
is its value.
π Common HTML Attributes
Letβs break down some of the most frequently used attributes with examples and explanations.
1. href
β Hyperlinks π
Element:
<a>
(anchor)What it does: The
href
attribute specifies the URL of the page the link goes to. Without it, the link will not be clickable.
Example:
<a href="https://www.example.com">Visit Example</a>
2. src
β Source of Media π₯
Element:
<img>
,<video>
,<audio>
, etc.What it does: The
src
attribute specifies the file path or URL of the image, video, or audio file that you want to embed in your webpage.
Example:
<img src="image.jpg" alt="A description of the image">
Explanation: The src
attribute in the <img>
tag defines where the browser should find the image file.
3. alt
β Alternative Text πΌοΈ
Element:
<img>
What it does: The
alt
attribute provides alternative text for an image. This is displayed if the image cannot load or when someone is using a screen reader (accessibility purposes).
Example:
<img src="image.jpg" alt="A beautiful sunset over the mountains">
Explanation: The alt
text describes what the image is about. It's useful for visually impaired users and when the image fails to load.
4. class
β Grouping for Styling π¨
Element: Almost any element.
What it does: The
class
attribute allows you to assign a class name to an HTML element. This is mainly used for styling elements via CSS or targeting elements via JavaScript.
Example:
<p class="intro-text">This is an introductory paragraph.</p>
Explanation: The class
attribute gives this paragraph a class name intro-text
, which you can then use in CSS or JavaScript to style or manipulate this specific element (or multiple elements with the same class).
CSS Example:
.intro-text {
font-size: 18px;
color: blue;
}
5. id
β Unique Identifier π
Element: Almost any element.
What it does: The
id
attribute gives an element a unique identifier, which must be unique within the document. This is helpful when you want to target one specific element with CSS or JavaScript.
Example:
<p id="main-paragraph">This is the main paragraph.</p>
Explanation: The id="main-paragraph"
gives this paragraph a unique ID, which can be referenced directly in CSS or JavaScript.
CSS Example:
#main-paragraph {
font-weight: bold;
color: red;
}
JavaScript Example:
.getElementById("main-paragraph").innerHTML = "New content for the paragraph.";
6. style
β Inline CSS π
Element: Almost any element.
What it does: The
style
attribute allows you to apply CSS directly within an HTML tag, which is called inline styling. This can be used to define specific styles for individual elements, but itβs generally better to use an external CSS stylesheet for cleaner code.
Example:
<p style="color: green; font-size: 20px;">This is a styled paragraph.</p>
Explanation: The style
attribute applies specific CSS styles to this paragraph. While useful for quick styling, itβs usually better to define styles in a separate CSS file for better maintainability.
7. title
β Tooltip Information βΉοΈ
Element: Almost any element.
What it does: The
title
attribute adds extra information that appears when a user hovers over the element. This can be used to give more context or explanations.
Example:
<a href="https://example.com" title="Go to Example website">Visit Example</a>
Explanation: When the user hovers over the link, a small tooltip will appear saying βGo to Example website.β
8. target
β Control Where the Link Opens π
Element:
<a>
(anchor)What it does: The
target
attribute specifies where to open the linked document. The most common value is_blank
, which opens the link in a new tab or window.
Example:
<a href="https://example.com" target="_blank">Open Example in a New Tab</a>
Explanation: This link will open in a new browser tab when clicked, allowing users to stay on the current page.
9. type
β Input Types on Forms π
Element:
<input>
What it does: The
type
attribute defines the type of data the user can enter into an<input>
field. Some common types includetext
,email
,password
, andnumber
.
Example:
<input type="email" name="email" placeholder="Enter your email">
Explanation: The type="email"
attribute ensures that the input field is specifically for email addresses and can perform validation.
10. placeholder
β Form Input Hints βοΈ
Element:
<input>
What it does: The
placeholder
attribute provides a hint or example of what should be entered in the input field.
Example:
<input type="text" placeholder="Enter your name">
Explanation: The placeholder
text is displayed inside the input field until the user starts typing.
11. action
and method
β Form Submission π¬
Element:
<form>
What it does:
action
: Specifies the URL where the form data should be sent.method
: Specifies the HTTP method (GET
orPOST
) used to submit the form data.
Example:
<form action="/submit-form" method="post">
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
Explanation: When the user submits the form, the data will be sent to /submit-form
using the POST
method.
12. disabled
β Disabling Elements π«
Element:
<input>
,<button>
,<select>
, etc.What it does: The
disabled
attribute prevents the user from interacting with the form element. It doesnβt require a value β just including the attribute makes the element disabled.
Example:
<button disabled>Submit</button>
Explanation: The button will be displayed but not clickable, as itβs disabled.
13. required
β Mandatory Fields β
Element:
<input>
,<select>
, etc.What it does: The
required
attribute ensures the user must fill out a form field before submitting it. Likedisabled
, it doesnβt require a value.
Example:
<input type="text" name="username" required>
Explanation: The user cannot submit the form unless they fill in the username
field.
π― Why Attributes Are Important
HTML attributes give us the flexibility to control how elements behave, how data is processed, and how users interact with our web pages. They make HTML much more powerful and adaptable to different situations.
Attributes are essential for:
π Styling elements with
class
,id
, andstyle
.π§βπ» Interactivity with form controls like
required
anddisabled
.π Navigation through links and actions like
href
andtarget
.π€ Accessibility enhancements with
alt
text andtitle
tooltips.
With a clear understanding of HTML attributes, youβre well on your way to building dynamic, functional, and user-friendly websites. π Feel free to experiment with these attributes as you design and develop your own web pages! π»
π Conclusion
HTML is the starting point for all web development. With the basics of HTML tags and attributes, you're ready to build simple yet effective web pages! π As you explore more advanced tags and pair HTML with CSS and JavaScript, you'll be able to create interactive, modern websites. π¨π¨βπ»
Happy coding! π»β¨
Subscribe to my newsletter
Read articles from Siddhi Mahajan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by