HTML - Structuring Content
HTML - Structuring Content
HTML Structure
An HTML document has a basic structure that includes essential tags to define the content of a
web page. Every HTML file starts with a <!DOCTYPE> declaration, followed by an <html> tag,
and inside it, there are two main sections: the <head> and the <body>.
html
CopyEdit
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Document Title</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>Welcome to HTML Cheat Sheet</p>
</body>
</html>
● head: Contains meta-information (not displayed on the page) like the title, character set,
and links to CSS or JS files.
● body: Contains all the visible content on the web page, including text, images, and other
elements.
Basic Tags
1. Headings (<h1> to <h6>)
html
CopyEdit
<h1>Main Heading</h1>
<h2>Subheading</h2>
<h3>Smaller Subheading</h3>
○ Defines paragraphs.
html
CopyEdit
<p>This is a paragraph.</p>
html
CopyEdit
<p>Line one.<br>Line two.</p>
html
CopyEdit
<hr />
○ <b> is for bold text; <strong> adds emphasis and indicates important text.
html
CopyEdit
<b>This text is bold.</b>
<strong>This text is strongly emphasized.</strong>
○ <i> italicizes text, while <em> adds emphasis (usually italicized with semantic
importance).
html
CopyEdit
<i>This text is italicized.</i>
<em>This text is emphasized.</em>
html
CopyEdit
<u>This text is underlined.</u>
html
CopyEdit
<s>This text is struck through.</s>
Links and Anchor Tags
html
CopyEdit
<a href="https://www.example.com">Visit Example</a>
html
CopyEdit
<a href="mailto:[email protected]">Send Email</a>
html
CopyEdit
<a href="#section1">Go to Section 1</a>
<p id="section1">This is Section 1.</p>
Images
○ Adds an image to the page. Requires the src and alt attributes.
html
CopyEdit
<img src="image.jpg" alt="Description of the image" />
● src: Specifies the image URL or path.
● alt: Provides alternative text if the image fails to load (important for accessibility).
html
CopyEdit
<img src="image.jpg" alt="Responsive Image" width="100%" />
Lists
html
CopyEdit
<ol>
<li>First item</li>
<li>Second item</li>
</ol>
html
CopyEdit
<ul>
<li>First item</li>
<li>Second item</li>
</ul>
3. Definition List (<dl>, <dt>, <dd>)
html
CopyEdit
<dl>
<dt>HTML</dt>
<dd>Hypertext Markup Language</dd>
<dt>CSS</dt>
<dd>Cascading Style Sheets</dd>
</dl>
Tables
○ Create tables using rows (<tr>), data cells (<td>), and header cells (<th>).
html
CopyEdit
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>25</td>
</tr>
</table>
html
CopyEdit
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>30</td>
</tr>
</table>
Forms
html
CopyEdit
<form action="/submit" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" />
<br />
<label for="email">Email:</label>
<input type="email" id="email" name="email" />
<br />
<button type="submit">Submit</button>
</form>
○ Multi-line input.
html
CopyEdit
<textarea rows="5" cols="40" name="comment">Enter comment</textarea>
html
CopyEdit
<input type="checkbox" name="subscribe" /> Subscribe to newsletter
<input type="radio" name="gender" value="male" /> Male
<input type="radio" name="gender" value="female" /> Female
Semantic tags clearly describe the content they contain, improving readability and SEO.
html
CopyEdit
<header>
<h1>Website Title</h1>
</header>
html
CopyEdit
<footer>
<p>Copyright 2025</p>
</footer>
html
CopyEdit
<section>
<h2>About Us</h2>
<p>Information about us.</p>
</section>
html
CopyEdit
<article>
<h2>Blog Post Title</h2>
<p>This is a blog post.</p>
</article>
5. Aside (<aside>)
html
CopyEdit
<aside>
<p>Related articles or advertisements.</p>
</aside>
Meta Tags
Meta tags contain information about the document, such as character set, viewport, and SEO
metadata.
html
CopyEdit
<meta charset="UTF-8" />
html
CopyEdit
<meta name="viewport" content="width=device-width, initial-scale=1.0"
/>
Media Elements
○ Embeds a video.
html
CopyEdit
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
html
CopyEdit
<audio controls>
<source src="audio.mp3" type="audio/mpeg" />
Your browser does not support the audio tag.
</audio>
Comments
This cheat sheet covers every essential concept in HTML, making it easy to reference during
development!
Yes, there are a few advanced HTML concepts and features I haven’t covered yet in the basic
cheat sheet. Below are additional topics that include more advanced features and modern
HTML5 elements that are helpful in building complex and feature-rich web applications:
○ HTML5 introduced several new semantic elements for better structure and SEO:
○ Main (<main>)
html
CopyEdit
<main>
<article>
<h2>Main Content</h2>
<p>This is the main content of the page.</p>
</article>
</main>
○
○ Nav (<nav>)
html
CopyEdit
<figure>
<img src="image.jpg" alt="Image description" />
<figcaption>Caption for the image</figcaption>
</figure>
○ The <canvas> element is used to draw 2D shapes and graphics (lines, circles,
etc.) via JavaScript.
html
CopyEdit
<canvas id="myCanvas" width="500" height="300"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(20, 20, 150, 100);
</script>
3.
○ Useful for creating games, charts, or dynamic visuals.
3. SVG (Scalable Vector Graphics) (<svg>)
○ Custom attributes that allow you to store extra information on HTML elements.
These attributes can be used in JavaScript for advanced interactions.
html
CopyEdit
<div data-user-id="12345" data-role="admin">John Doe</div>
<script>
var userId =
document.querySelector("div").getAttribute("data-user-id");
console.log(userId); // 12345
</script>
○ HTML5 introduced new input types and form attributes for client-side validation:
html
CopyEdit
<input type="email" placeholder="Enter your email" required />
<input type="url" placeholder="Enter website URL"
pattern="https?://.+" />
○
○ Datalist (<datalist>)
html
CopyEdit
<input list="browsers" name="browser" />
<datalist id="browsers">
<option value="Chrome"></option>
<option value="Firefox"></option>
<option value="Safari"></option>
</datalist>
html
CopyEdit
<script>
// Store data
localStorage.setItem("username", "JohnDoe");
// Retrieve data
var name = localStorage.getItem("username");
console.log(name); // JohnDoe
</script>
html
CopyEdit
<video id="myVideo" width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
<script>
var video = document.getElementById("myVideo");
video.play(); // Play the video using JavaScript
</script>
html
CopyEdit
<progress value="70" max="100">70%</progress>
html
CopyEdit
<div draggable="true" ondragstart="drag(event)" id="drag1">Drag
me!</div>
<script>
function drag(event) {
event.dataTransfer.setData("text", event.target.id);
}
</script>
11.Web Workers
● HTML5 Web Workers provide background threads to execute JavaScript code in parallel
without blocking the main thread.
html
CopyEdit
if (window.Worker) {
var worker = new Worker("worker.js");
worker.postMessage("Hello Worker");
}
html
CopyEdit
<details>
<summary>More Info</summary>
<p>This is the hidden content that is displayed when expanded.</p>
</details>
Contenteditable Attribute
○ Makes an element editable by the user.
html
CopyEdit
<div contenteditable="true">Edit this text.</div>
html
CopyEdit
<my-element></my-element>
○ Provides a way to make web content more accessible to people with disabilities.
html
CopyEdit
<button aria-label="Close" role="button">X</button>
These advanced HTML topics are crucial when you're developing complex, interactive, and
responsive web applications. You can use these elements to enhance user experiences and
optimize performance for modern web applications. Let me know if you’d like to dive deeper into
any of these specific topics!
Let's dive deeper into even more advanced and less frequently discussed HTML topics that
you may find useful in certain cases, especially for modern, complex web development projects.
○ As mentioned before, you can create custom data attributes that can be
manipulated with JavaScript. These attributes can store any extra information
directly in HTML elements. However, it’s important to know some specific use
cases and how these can be leveraged for efficient data handling:
<script>
var element = document.getElementById('myElement');
// Add or change a data attribute
element.setAttribute('data-status', 'active');
// Access the attribute's value
console.log(element.dataset.userId); // "001"
console.log(element.dataset.status); // "active"
</script>
○ This is extremely useful for working with dynamically generated content, such
as managing attributes in single-page applications (SPAs) or data-driven web
components.
○ Custom Elements:
html
CopyEdit
<script>
class MyElement extends HTMLElement {
connectedCallback() {
this.innerHTML = "<p>This is a custom element!</p>";
}
}
customElements.define('my-element', MyElement);
</script>
<my-element></my-element>
○ Shadow DOM:
■ Encapsulates the DOM and CSS so they don’t affect the rest of the
document.
html
CopyEdit
<script>
class ShadowElement extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({ mode: 'open' });
shadow.innerHTML = `
<style> p { color: blue; } </style>
<p>This is a shadow DOM element</p>`;
}
}
customElements.define('shadow-element', ShadowElement);
</script>
<shadow-element></shadow-element>
○ HTML Templates:
■ HTML <template> tag can hold HTML code that is not rendered until
explicitly cloned.
html
CopyEdit
<template id="myTemplate">
<p>This will only be displayed when cloned</p>
</template>
<script>
var template = document.getElementById('myTemplate');
var clone = document.importNode(template.content, true);
document.body.appendChild(clone);
</script>
○
○ HTML Imports were once part of the Web Components specification and
allowed importing HTML documents into other HTML documents. It has been
deprecated, but understanding it is helpful for backward compatibility in some
projects.
html
CopyEdit
<link rel="import" href="/path/to/import.html" />
○ Service Workers are scripts that your browser runs in the background, separate
from the web page. They enable features like offline capabilities, background
sync, and push notifications.
html
CopyEdit
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js').then(function
(registration) {
console.log('Service Worker registered with scope:',
registration.scope);
});
}
</script>
Service Worker Example (sw.js):
javascript
CopyEdit
self.addEventListener('install', function (event) {
// Cache resources during the install phase
});
○
○ This is a vital technology for Progressive Web Apps (PWAs), providing offline
support and making web apps behave like native apps.
○ ARIA attributes enhance HTML accessibility, especially for screen readers and
other assistive devices. When creating complex interfaces like menus, carousels,
or dialogs, ARIA attributes ensure these elements are navigable for everyone.
○
○
ARIA States: Helps manage the state of interactive elements.
html
CopyEdit
<button aria-expanded="false" aria-controls="menu">Open Menu</button>
<div id="menu" aria-hidden="true">Menu Content</div>
javascript
CopyEdit
var socket = new WebSocket('ws://example.com/socketserver');
socket.onopen = function () {
socket.send('Hello Server');
};
socket.onmessage = function (event) {
console.log('Message from server ', event.data);
};
○ WebSockets are crucial in real-time applications like chat apps, stock tickers,
or multiplayer games.
○ A browser API that allows you to watch for visibility changes of an element in
relation to the viewport. It’s especially useful for lazy-loading images or
triggering animations when elements come into view.
javascript
CopyEdit
let observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
console.log('Element is in view!');
}
});
});
observer.observe(document.querySelector('#targetElement'));
○ Allows you to monitor changes in the DOM (e.g., when elements are added,
removed, or modified).
javascript
CopyEdit
const targetNode = document.getElementById('myElement');
const config = { attributes: true, childList: true, subtree: true };
const callback = function (mutationsList) {
for (let mutation of mutationsList) {
console.log('Mutation observed: ', mutation);
}
};
○ Use different images for different devices or resolutions using the <picture>
element or srcset attribute for responsive image loading.
html
CopyEdit
<picture>
<source media="(min-width: 650px)" srcset="large.jpg">
<source media="(min-width: 465px)" srcset="medium.jpg">
<img src="small.jpg" alt="Sample image">
</picture>
○ The srcset attribute also works directly on an <img> tag to define multiple
resolutions:
html
CopyEdit
<img src="small.jpg" srcset="medium.jpg 2x, large.jpg 3x"
alt="Responsive image">
● CSP is a security feature that helps prevent Cross-Site Scripting (XSS) attacks by
restricting where resources (like scripts, styles, or images) can be loaded from.
html
CopyEdit
<meta http-equiv="Content-Security-Policy" content="default-src
'self'; script-src 'self' https://trusted.com">