-
DOCTYPE Declaration:
Start your document with the HTML5 DOCTYPE to ensure browsers render the page in standards mode.
<!DOCTYPE html>
- The Head Element:
- Character Set: Always define the character set. UTF-8 is the most commonly used and recommended.
<meta charset="UTF-8">
-
Viewport: Ensure your page is responsive and scales properly on all devices.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
- Title and Metadata: Include a meaningful title and relevant meta tags for SEO and social media sharing purposes.
- Links: Reference CSS files, favicon, and other resources here.
- Character Set: Always define the character set. UTF-8 is the most commonly used and recommended.
- The Body Element: This contains all the content visible to users. Organize your content using the following structural elements:
- Header (`<header>`): Contains the website logo, primary navigation, and other header elements.
- Navigation (`<nav>`): Encapsulates navigation links. Though often in the header, it can be used elsewhere like sidebars.
- Main (`<main>`): Holds the primary content of the document. Ensure there's only one `<main>` per page and that it's unique from content in the sidebars, footer, or header.
- Articles (`<article>`): Denotes a self-contained composition in a document, page, application, or site, which is intended to be independently distributable or syndicated.
- Sections (`<section>`): Represents standalone sections of content or functionality, which makes sense on its own.
- Aside (`<aside>`): Contains information related to the main content, but which can be considered separate from the main content.
- Footer (`<footer>`): Typically contains information about the author, copyright data, links related to documents, etc.
- Proper Use of Other Semantic Elements:
- Figures (`<figure>` and `<figcaption>`): Use these to mark up photos, illustrations, charts, and their captions.
- Details and Summary: Provide interactive disclosure widgets, allowing users to toggle the visibility of content.
- Accessibility Enhancements:
- ARIA Roles: Use ARIA (Accessible Rich Internet Applications) roles where applicable to enhance screen reader compatibility.
- Tabindex: Ensure interactive elements have a logical tab order for keyboard-only users.
- Script and Style Elements:
- Styles: External styles are recommended. However, if internal styles are necessary, enclose them within the `<style>` element in the `<head>`.
- Scripts: For performance reasons, scripts (external or internal) are typically placed just before the closing `</body>` tag, unless they're asynchronous or need to run before the page renders.
- Comments: Use comments judiciously to explain code where necessary, especially in complex sections. However, avoid over-commenting as it can clutter the code.
<!-- This is an HTML comment -->
Structuring HTML5 documents effectively is paramount in the modern era of web publishing. Properly structured content is not only beneficial for SEO but also ensures accessibility and a cohesive user experience across varying devices. Leveraging the semantic capabilities of HTML5 ensures that content is presented logically and is easily interpreted by both browsers and assistive technologies.