2. HTML Document Structure 💻 HowTo
2. Document Structure
How to Add a DOCTYPE Declaration
- Place
<!DOCTYPE html>as the very first line of your HTML document - This declaration must come before any other code
- It tells the browser to use HTML5 standards for rendering
- It should be written in uppercase or lowercase – both work
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
How to Set the HTML Language Attribute
- Add the
langattribute to your<html>element - Use language codes like “en” for English, “es” for Spanish, etc.
- This helps with accessibility and SEO
Example:
<html lang="en">
How to Create a Proper HTML Document Structure
- Start with
<!DOCTYPE html> - Add
<html>element with language attribute - Include
<head>section with metadata - Add
<body>section with visible content
Complete Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to my website</h1>
<p>This is a paragraph.</p>
</body>
</html>
How to Add Character Encoding in Head Section
- Include
<meta charset="UTF-8">within the<head>section - Place it early in the head section
- This ensures proper display of special characters
Example:
<head>
<meta charset="UTF-8">
<title>My Page</title>
</head>
How to Set the Page Title
- Add
<title>element within the<head>section - The text inside will appear in browser tab
- Keep titles descriptive and concise
Example:
<title>My Web Page</title>
How to Add Viewport Meta Tag for Responsive Design
- Include
<meta name="viewport" content="width=device-width, initial-scale=1.0">in<head> - This makes your page responsive on mobile devices
- It controls the page’s dimensions and scaling
Example:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
How to Add a Description Meta Tag
- Use
<meta name="description" content="Your description here">in head section - Write a brief summary of your page content
- Helps with SEO and search engine results
Example:
<meta name="description" content="This is a sample webpage about HTML">
How to Link External CSS Files
- Use
<link rel="stylesheet" href="styles.css">in head section - Place the link element within
<head>tags - Make sure the file path is correct
Example:
<head>
<link rel="stylesheet" href="styles.css">
</head>
How to Add External JavaScript Files
- Use
<script src="script.js"></script>in head or body section - Place it before closing
</body>tag for better performance - Can also be placed in head section
Example:
<head>
<script src="script.js" defer></script>
</head>
How to Add a Favicon
- Use
<link rel="icon" href="favicon.ico" type="image/x-icon">in head section - Place the favicon file in your project directory
- This displays an icon in browser tabs
Example:
<head>
<link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
How to Add Keywords Meta Tag
- Use
<meta name="keywords" content="HTML, web development, tutorial">in head section - Include relevant keywords for SEO purposes
- Separate keywords with commas
Example:
<meta name="keywords" content="HTML, web development, tutorial">
How to Add Author Meta Tag
- Use
<meta name="author" content="John Doe">in head section - Specify the author’s name or company
- Helps identify content creators
Example:
<meta name="author" content="John Doe">
How to Add Visible Content to Body Section
- Place all visible elements within
<body>tags - Include headings, paragraphs, images, links, etc.
- This is where users will see the actual page content
Example:
<body>
<h1>Main Heading</h1>
<p>This is visible content.</p>
<img src="image.jpg" alt="Sample image">
</body>
How to Add Images with Alt Text
- Use
<img src="image.jpg" alt="Description of image">in body section - Always include descriptive alt text for accessibility
- Helps screen readers and SEO
Example:
<img src="image.jpg" alt="A beautiful sunset over the ocean">
How to Create Hyperlinks
- Use
<a href="url">Link Text</a>in body section - The
hrefattribute specifies where the link goes - The text between tags is what users click on
Example:
<a href="page.html">Visit another page</a>
How to Add Multiple Meta Tags
- Include multiple
<meta>elements in head section - Each with different
nameandcontentattributes - Combine character encoding, viewport, description, keywords, etc.
Example:
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Page description">
<meta name="keywords" content="HTML, CSS, JavaScript">
</head>
How to Create a Complete Head Section
- Include character encoding meta tag
- Add viewport meta tag for responsive design
- Set the page title
- Add other meta tags (description, keywords, author)
- Link external resources (CSS, favicon)
Complete Example:
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="This is a sample webpage">
<meta name="keywords" content="HTML, web development">
<meta name="author" content="Author Name">
<title>My Web Page</title>
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
</head>