Creating an automatic table of contents (TOC) in an HTML webpage can be achieved using JavaScript. Here's a simple example:
HTML Structure
First, create the basic HTML structure with headings:
Explanation
- HTML Structure: The HTML includes various headings (h2 and h3) which will serve as the TOC items.
- CSS Styles: Basic styles for the TOC and headings.
- JavaScript:
- It listens for the DOM content to load.
- It selects all
h2
andh3
headings. - It creates a list item for each heading, generates an ID based on the heading text, and appends it to the TOC.
How It Works
When the page loads, the script will generate a TOC based on the headings present in the document, allowing users to click and navigate directly to those sections.
Enjoy, follow us for more
No comments:
Post a Comment