What is HTML? HTML (Hypertext Markup Language) is the standard language used to create web pages. It is used to structure content on the web, such as text, images, links, and other multimedia elements. HTML is not a programming language but a markup language that tells a web browser how to display content.
Basic Components of HTML: HTML is made up of:
- Tags: The building blocks of HTML. Tags are enclosed in angle brackets, e.g.,
<p>
or<a>
. - Elements: The combination of opening and closing tags along with content, e.g.,
<p>This is a paragraph.</p>
. - Attributes: Extra information about elements, written within the opening tag, e.g.,
<a href="https://example.com">Click here</a>
.
Structure of an HTML Document
An HTML document is structured in a very specific way. Let’s break down the structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Understanding HTML</title>
</head>
<body>
<h1>Welcome to HTML</h1>
<p>This is the first chapter of the book, where you’ll learn HTML basics.</p>
</body>
</html>
Explanation of the Structure:
<!DOCTYPE html>
: This declaration defines the document type and version (HTML5 in this case).<html>
: The root element that wraps all the content of the page.<head>
: Contains meta-information about the document, such as character encoding, viewport settings, and the document's title.<body>
: This section contains the content that is visible on the webpage, such as text, images, and links.
Basic Tags in HTML
-
HTML Tag: The
<html>
tag is the root of any HTML document. All other tags are nested inside it.<html> <head>...</head> <body>...</body> </html>
-
Head Tag: The
<head>
element contains meta-information that isn’t visible to users directly but is used by browsers, search engines, etc.Example:
<head> <meta charset="UTF-8"> <title>My Web Page</title> </head>
-
Body Tag: The
<body>
tag contains the actual content visible on the webpage.Example:
<body> <h1>Hello, World!</h1> <p>This is a paragraph in the body of the document.</p> </body>
Understanding Tags, Elements, and Attributes
- Tags: Enclose an element in angle brackets (
< >
), e.g.,<p>
,<h1>
. - Elements: The entire structure, including the opening tag, content, and closing tag, e.g.,
<p>This is a paragraph</p>
. - Attributes: Provide additional information about an element, e.g.,
<a href="https://example.com">Click here</a>
.
Example 1: Simple HTML Page
Here is an example of a simple HTML page with headings, paragraphs, and a link:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic HTML Example</title>
</head>
<body>
<h1>Introduction to HTML</h1>
<p>HTML is the foundation of web development. It defines the structure of web pages.</p>
<a href="https://www.example.com">Visit Example</a>
</body>
</html>
Explanation:
<h1>
: The main heading of the page.<p>
: A paragraph of text.<a href="https://www.example.com">
: A link to another webpage.
HTML Elements: Block-Level and Inline
- Block-Level Elements: These elements take up the full width available. They appear on a new line by default. Examples:
<h1>
,<p>
,<div>
,<table>
. - Inline Elements: These elements take up only as much width as necessary and do not start on a new line. Examples:
<a>
,<span>
,<img>
.
Example of Block-Level and Inline Elements:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Block vs Inline Example</title>
</head>
<body>
<h1>This is a block-level element (heading)</h1>
<p>This is a block-level element (paragraph).</p>
<p>This is a <a href="#">link</a> inside a block-level element.</p>
<span>This is an inline element (span).</span>
<img src="image.jpg" alt="Image Example"> <!-- Inline element -->
</body>
</html>
Attributes in HTML Elements
HTML tags can have attributes that modify their behavior or provide additional information. Attributes are written within the opening tag.
Common HTML Attributes:
href
in the<a>
tag specifies the destination URL.src
in the<img>
tag specifies the image source.alt
provides alternative text for an image.
Example 2: Using Attributes in HTML Elements
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Using Attributes Example</title>
</head>
<body>
<a href="https://www.example.com" target="_blank">Visit Example</a>
<img src="image.jpg" alt="An Example Image" width="300" height="200">
</body>
</html>
- The
target="_blank"
in the<a>
tag makes the link open in a new tab. - The
width
andheight
attributes in the<img>
tag specify the dimensions of the image.
Summary Table: Common HTML Tags and Their Attributes
Tag | Description | Example |
---|---|---|
<h1> |
Main heading | <h1>This is a heading</h1> |
<p> |
Paragraph of text | <p>This is a paragraph.</p> |
<a> |
Hyperlink to another page or resource | <a href="https://example.com">Link</a> |
<img> |
Display an image | <img src="image.jpg" alt="image" /> |
<div> |
Defines a section of the page | <div class="container">Content</div> |
<span> |
Inline container for text or other elements | <span>Inline Text</span> |
0 Comments