Elements & Attributes Deep Dive
HTML elements are the building blocks of web pages. Master how elements work, their anatomy, and how attributes provide additional information and functionality.
Anatomy of an HTML Element
An HTML element typically consists of three parts: an opening tag, content, and a closing tag.
<tagname attribute="value">Content goes here</tagname>
↓ ↓ ↓ ↓ ↓
Opening Attribute Value Content Closing
Tag Tag<p class="intro" id="first-paragraph">
This is a paragraph with attributes.
</p>Breaking it down:
<p>- Opening tag (defines a paragraph)class="intro"- Attribute for CSS stylingid="first-paragraph"- Unique identifier for the elementThis is a paragraph...- The content</p>- Closing tag (ends the paragraph)
Block vs. Inline Elements
HTML elements are divided into two main display categories: block-level and inline elements.
Key Differences:
| Block Elements | Inline Elements |
|---|---|
| Start on a new line | Flow within text, don't break lines |
| Take up full width available | Only take up necessary width |
| Can contain block and inline elements | Can only contain inline elements and text |
Examples: <div>, <p>, <h1> | Examples: <span>, <a>, <strong> |
<!-- Block elements (each on new line) -->
<h1>This is a heading</h1>
<p>This is a paragraph</p>
<div>This is a div container</div>
<!-- Inline elements (within text flow) -->
<p>This paragraph contains <strong>bold text</strong> and <em>italic text</em> inline.</p>Common Block-Level Elements
<div> - Division/Container
Purpose: Generic container for grouping content
Use case: Layout sections, applying styles to groups of elements
<div class="header">
<h1>Welcome</h1>
<p>Subtitle text</p>
</div><p> - Paragraph
Purpose: Text paragraphs
Use case: Body text, article content
<p>This is a paragraph of text. Browsers automatically add space before and after.</p><h1> to <h6> - Headings
Purpose: Section headings (hierarchical)
Use case: Document structure, SEO
<h1>Main Title</h1>
<h2>Section Heading</h2>
<h3>Subsection</h3><ul>, <ol>, <li> - Lists
Purpose: Unordered and ordered lists
Use case: Navigation menus, feature lists, steps
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<ol>
<li>First step</li>
<li>Second step</li>
</ol>Common Inline Elements
<span> - Inline Container
Purpose: Generic inline container
Use case: Styling specific text portions
<p>The price is <span class="highlight">$99.99</span> today!</p><a> - Anchor/Link
Purpose: Hyperlinks to other pages or sections
Use case: Navigation, external links
<a href="https://example.com">Visit Example</a>
<a href="#section2">Jump to Section 2</a><strong> and <em> - Emphasis
Purpose: Semantic emphasis (strong importance, emphasis)
Use case: Important text, stressed words
<p><strong>Warning:</strong> This action cannot be undone.</p>
<p>I <em>really</em> need to finish this.</p><img> - Image
Purpose: Embed images
Use case: Photos, illustrations, icons
<img src="logo.png" alt="Company Logo" width="200" height="100">Void (Self-Closing) Elements
Some elements don't have content and therefore don't need a closing tag. These are called void elements or empty elements.
| Element | Purpose | Example |
|---|---|---|
<br> | Line break | Line 1<br>Line 2 |
<hr> | Horizontal rule (divider) | <hr> |
<img> | Image | <img src="pic.jpg" alt="Photo"> |
<input> | Form input field | <input type="text" name="username"> |
<meta> | Metadata | <meta charset="UTF-8"> |
<link> | External resource link | <link rel="stylesheet" href="style.css"> |
/>) is optional for void elements. Both <br> and <br /> are valid.HTML Attributes Explained
Attributes provide additional information about HTML elements. They appear in the opening tag and follow a name="value" format.
Attribute Syntax Rules:
- Attributes go in the opening tag only
- Format:
attribute-name="value" - Use quotes (double or single) around values
- Multiple attributes separated by spaces
- Attribute names are case-insensitive (but lowercase is conventional)
<img src="photo.jpg" alt="Sunset" width="800" height="600" class="featured">Universal (Global) Attributes
These attributes can be used on any HTML element:
id
Purpose: Unique identifier for an element
Rules: Must be unique on the page, used for CSS/JS targeting
<div id="header">...</div>class
Purpose: One or more class names for CSS styling
Rules: Can repeat across elements, space-separated for multiple classes
<p class="intro highlight">Text</p>style
Purpose: Inline CSS styles
Warning: Avoid for maintainability—use external CSS instead
<p style="color: red; font-size: 18px;">Styled text</p>title
Purpose: Tooltip text that appears on hover
Use case: Additional information, abbreviation expansions
<abbr title="HyperText Markup Language">HTML</abbr>data-*
Purpose: Custom data attributes for JavaScript
Use case: Store extra information without affecting display
<button data-user-id="123" data-action="delete">Delete</button>lang
Purpose: Language of element content
Use case: Accessibility, translation tools, SEO
<p lang="es">Hola mundo</p>Element-Specific Attributes
Some attributes are specific to certain elements:
<a> (Link) Attributes:
href- Destination URLtarget- Where to open link (_blank,_self, etc.)rel- Relationship to target (nofollow,noopener)
<a href="https://example.com" target="_blank" rel="noopener">External Link</a><img> (Image) Attributes:
src- Image file path (required)alt- Alternative text for accessibility (required)widthandheight- Dimensions (optional but recommended)
<img src="logo.png" alt="Company Logo" width="200" height="100"><input> (Form Input) Attributes:
type- Input type (text, email, password, etc.)name- Name for form submissionvalue- Default/current valueplaceholder- Hint textrequired- Makes field mandatory
<input type="email" name="user_email" placeholder="Enter email" required>Boolean Attributes
Some attributes are boolean (true/false). Their presence = true, absence = false.
<!-- These are equivalent: -->
<input type="checkbox" checked>
<input type="checkbox" checked="checked">
<input type="checkbox" checked="">
<!-- Disabled button -->
<button disabled>Can't Click Me</button>
<!-- Required field -->
<input type="text" required>
<!-- Readonly field -->
<input type="text" value="Cannot edit" readonly>Best Practices
✅ Do
- Use semantic elements (
<header>,<nav>,<article>) - Always include
alttext for images - Use
idfor unique elements,classfor reusable styles - Nest elements properly (close in reverse order)
- Use lowercase for element and attribute names
- Quote all attribute values
⌠Don't
- Use
<div>for everything (lack of semantics) - Forget closing tags (except void elements)
- Overlap tags incorrectly
- Use inline styles excessively
- Duplicate
idvalues on same page - Use deprecated elements (
<font>,<center>)