HTML Tutorial
The Words of a Web Language
When you look at a finished webpage you see a cohesive visual thing - text, images, links, sections - but a browser sees it completely differently: a structured list of labeled content pieces, each one tagged to say what kind of thing it is. This heading, that paragraph, this image, that navigation menu. HTML elements are the individual labels and their contents, and learning HTML is mostly learning what each element means and when to use it. Once that vocabulary is in your head the rest of what HTML does - attributes, nesting, how things flow on the page - follows pretty naturally.
Anatomy of an HTML Element
The anchor tag - the link element - is a good one to dissect because it usually has several attributes at once, which makes all the component parts visible in one place.
1<a href="https://www.example.com" target="_blank" class="link">Visit Example</a>Component Parts
Opening tag- The a with angle brackets - this tells the browser an anchor element is starting here.Attribute names- href, target, and class - the names of the configuration options being set on this element.Attribute values- The values in quotation marks after each equals sign - the URL, where the link opens, and the CSS class name.Content- Visit Example - the visible text the user sees and clicks on.Closing tag- The /a with angle brackets - signals where the anchor element ends.
HTML Attributes: Adding Extra Information
Attributes live inside the opening tag and configure what the element does or how it behaves - the href on an anchor says where the link goes, the src on an image says which file to load, the class on a div says which CSS styles to apply. They follow the same pattern every time: attribute name, equals sign, value in double quotes. An element can have as many attributes as it needs and the order doesn't matter, which is one of the more relaxed parts of HTML syntax.
Common Attributes
id- Gives an element a unique identifier on the page. No two elements should share the same id - it breaks CSS targeting and JavaScript selection if they do.class- Assigns one or more style categories to an element. Multiple elements can share the same class, which is how CSS applies the same styles to many elements at once.src- Specifies the file path or URL for the media to load - used on img, audio, video, and script elements.href- Specifies the destination URL for links. Short for hypertext reference, which explains the naming even if it's not intuitive at first.alt- Alternative text for images. Screen readers read this aloud to users who can't see the image, and it's what browsers show if the image fails to load. Skipping alt text is both an accessibility problem and an SEO miss.style- Inline CSS written directly on the element. Works fine for quick testing but keeping styles in a separate CSS file is much better for maintenance.
Empty Elements (Void Elements)
Some HTML elements genuinely have nothing inside them - they're self-contained instructions rather than containers for content - and these are called void elements or empty elements. The img tag is the most common example: it embeds an image using its src attribute alone, there's nothing to put between an opening and closing tag, so there is no closing tag. Writing a closing img tag isn't just unnecessary - it's invalid HTML. The same applies to br for line breaks, hr for horizontal rules, and input for form fields.
1<!-- Correct: void elements with no closing tag -->
2<img src="cat.jpg" alt="A fluffy cat">
3<br>
4<hr>
5<input type="text">
6
7<!-- Wrong: adding a closing tag to a void element -->
8<img src="cat.jpg" alt="A fluffy cat"></img>Common Void Elements
img- Embeds an image. Requires src and alt attributes.br- Line break within text. Use sparingly - CSS margin is usually the better tool for spacing.hr- Horizontal rule, represents a thematic break between sections.input- Form input field. Its behavior changes completely based on the type attribute.
Nesting Elements: Putting Elements Inside Other Elements
Nesting is how you build structure - wrapping one element inside another to express a relationship between them, like a paragraph that contains a link, or a list that contains list items, or a div that groups several paragraphs together into a section. The rule that trips people up is the closing order: elements must be closed in the reverse of the order they were opened, like stacking plates and unstacking them last-in-first-out. Opening a strong tag inside a paragraph and then closing the paragraph before the strong is invalid HTML and browsers handle it inconsistently.
1<!-- Simple nesting -->
2<p>My favorite animal is the <strong>capybara</strong>.</p>
3
4<!-- Correct nesting order -->
5<p>This is <strong>very <em>important</em></strong> text.</p>
6
7<!-- Wrong nesting order - strong closed before em -->
8<p>This is <strong>very <em>important</strong> text</em>.</p>Rules for Proper Nesting
Last opened, first closed- The innermost element must be closed before any element that contains it. Think of it as a set of nested boxes - you close the inner box before you can close the outer one.Block elements don't go inside inline elements- A paragraph element inside a span or anchor tag is invalid HTML. Block elements can contain inline elements but not the other way around.
Block-Level vs Inline Elements
Every HTML element is either block-level or inline by default, and this is one of the most foundational layout concepts in HTML because it determines how elements flow on the page without any CSS. Block elements stack vertically - each one starts on its own new line and stretches the full available width. Inline elements flow horizontally within text - they sit next to each other and wrap to new lines when they run out of space, just like words in a sentence do. Div and paragraph and h1 are block elements. Anchor, strong, em, and span are inline elements. Understanding this default behavior explains why some elements always create a line break and others don't.
Element Types
Block-level elements- Start on a new line, take up the full available width, stack vertically. Common examples: div, p, h1 through h6, ul, ol, li, section, article, nav, header, footer.Inline elements- Flow within text, only as wide as their content, don't force new lines. Common examples: a, strong, em, span, img, br, code.CSS can change this- The block or inline behavior is just the default. CSS display property can make any element behave like a block, inline, or flex/grid container regardless of its HTML default.
Putting It All Together: A Practical Example
Here's a small section of a webpage that uses block and inline elements together, nested elements, and attributes - the kind of structure you'll write constantly once these concepts click into place.
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <title>Element Example</title>
6</head>
7<body>
8 <!-- Block-level heading -->
9 <h1 id="main-title">My Pets</h1>
10
11 <!-- Block paragraph containing inline elements -->
12 <p>I have two pets. A very <strong>lazy cat</strong> named Whiskers and a <em>hyperactive</em> dog named <a href="spot.html">Spot</a>.</p>
13
14 <!-- Block div acting as a container -->
15 <div class="pet-profile">
16 <!-- Heading nested inside the div -->
17 <h2>Whiskers the Cat</h2>
18
19 <!-- Void element nested inside the div -->
20 <img src="whiskers.jpg" alt="A gray tabby cat sleeping on a sofa" class="pet-photo">
21
22 <!-- Paragraph nested inside the div -->
23 <p>Whiskers enjoys napping and ignoring everyone.</p>
24 </div>
25</body>
26</html>You've Learned the Vocabulary
Elements, tags, attributes, nesting, void elements, block versus inline - these aren't just theoretical concepts, they're the mental model you use every time you read or write HTML. When you look at a page's source code now, you should be able to read the structure rather than just see a wall of angle brackets. The next thing to build on top of this foundation is familiarity with the specific elements you'll use most often - headings, paragraphs, links, images, lists, and the semantic elements that give sections of a page their meaning