Please create an html file, containing css and js, to show the functionality of data retrieval from a js object array that contains a book about how to live healthy, productive and meaningful life with text and different heading levels eg h1, h2, h3... First it shows only the toc, on clicking each heading level, it shows the details including the text with professional book formatting. It should have a responsive design. The toc is always visible as a side panel through a burger menu and has open all and collapse all features.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a single self‐contained HTML file. It defines a small “book” in a JavaScript array, generates a collapsible Table of Contents, and displays the corresponding sections on demand. It also includes a responsive side panel (burger menu) with Open All / Collapse All controls:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Healthy, Productive & Meaningful Life Book</title>
<style>
/* ========== RESET & BASE ========== */
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Georgia', serif;
line-height: 1.6;
color: #333;
}
a { color: #0366d6; text-decoration: none; }
a:hover { text-decoration: underline; }
/* ========== LAYOUT ========== */
#burger {
position: fixed;
top: 10px; left: 10px;
font-size: 1.5rem;
background: none;
border: none;
cursor: pointer;
z-index: 1100;
}
.side-nav {
position: fixed;
top: 0; left: 0; bottom: 0;
width: 250px;
background: #f7f7f7;
overflow-y: auto;
border-right: 1px solid #ddd;
transform: translateX(-100%);
transition: transform 0.3s ease;
z-index: 1000;
padding: 20px;
}
.side-nav.open {
transform: translateX(0);
}
.main {
padding: 20px;
transition: margin-left 0.3s ease;
}
/* on desktop show nav by default */
@media (min-width: 768px) {
#burger { display: none; }
.side-nav { transform: translateX(0); }
.main.desktop-open { margin-left: 250px; }
}
/* ========== TOC ========== */
.toc-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1em;
}
.toc-header h2 { font-size: 1.2rem; }
.toc-header button {
font-size: 0.85rem;
padding: 4px 8px;
margin-left: 4px;
cursor: pointer;
}
#toc {
list-style: none;
}
.toc-item {
margin: 4px 0;
cursor: pointer;
}
.toc-item[data-level="2"] { margin-left: 12px; }
.toc-item[data-level="3"] { margin-left: 24px; }
.toc-item[data-level="4"] { margin-left: 32px; }
.toc-item:hover { text-decoration: underline; }
/* ========== BOOK CONTENT ========== */
.book-section { margin-bottom: 2em; }
.book-section h1,
.book-section h2,
.book-section h3,
.book-section h4 {
margin-top: 1em; margin-bottom: 0.5em;
font-family: 'Helvetica Neue', sans-serif;
color: #222;
}
.section-content {
display: none;
margin-left: 10px;
}
.section-content p {
text-indent: 1em;
margin-bottom: 1em;
text-align: justify;
}
</style>
</head>
<body>
<!-- Burger & Side Nav -->
<button id="burger">☰</button>
<nav class="side-nav" id="sideNav">
<div class="toc-header">
<h2>Contents</h2>
<div>
<button id="openAll">Open All</button>
<button id="collapseAll">Collapse All</button>
</div>
</div>
<ul id="toc"></ul>
</nav>
<!-- Main Content Area -->
<div class="main" id="mainContent">
<h1>Welcome to Your Guide</h1>
<p>Click any item in the Table of Contents to explore each section.</p>
<div id="bookContainer"></div>
</div>
<script>
// ==== SAMPLE BOOK DATA ====
const bookContent = [
{
id: 'sec1',
level: 1,
title: 'The Foundations of Healthy Living',
content: 'To live a healthy life, one must begin with understanding the pillars of well-being: proper nutrition, regular activity, and mental balance.',
children: [
{
id: 'sec1-1',
level: 2,
title: 'Nutrition and Whole Foods',
content: 'Nutrition forms the building blocks of our physical health. Focus on whole, unprocessed foods, a rainbow of fruits and vegetables, and quality proteins.',
children: []
},
{
id: 'sec1-2',
level: 2,
title: 'Physical Activity',
content: 'Regular exercise is key to maintaining vitality and longevity. Whether it’s cardio, strength, or flexibility work—consistency is king.',
children: []
}
]
},
{
id: 'sec2',
level: 1,
title: 'Productivity Strategies for a Meaningful Day',
content: 'Crafting a productive routine enables us to achieve our goals with clarity. Start by aligning tasks with your core values.',
children: [
{
id: 'sec2-1',
level: 2,
title: 'Time Management',
content: 'Successful individuals prioritize tasks and allocate time blocks effectively. A simple daily plan can dramatically boost focus.',
children: [
{
id: 'sec2-1-1',
level: 3,
title: 'The Pomodoro Technique',
content: 'Work in focused 25-minute intervals with 5-minute breaks to sustain concentration and minimize burnout.',
children: []
}
]
},
{
id: 'sec2-2',
level: 2,
title: 'Goal Setting',
content: 'Define clear, achievable goals to direct your efforts and measure progress. Use SMART criteria to keep them on track.',
children: []
}
]
}
];
// ==== UTILITY TO BUILD TOC ====
function buildTOC(items, parentUl) {
items.forEach(item => {
const li = document.createElement('li');
li.textContent = item.title;
li.className = 'toc-item';
li.dataset.target = item.id;
li.dataset.level = item.level;
parentUl.appendChild(li);
li.addEventListener('click', () => {
const section = document.getElementById(item.id);
const content = section.querySelector('.section-content');
content.style.display = (content.style.display === 'block') ? 'none' : 'block';
});
if (item.children && item.children.length) {
buildTOC(item.children, parentUl);
}
});
}
// ==== UTILITY TO BUILD CONTENT SECTIONS ====
function buildContent(items, container) {
items.forEach(item => {
const sec = document.createElement('section');
sec.id = item.id;
sec.className = 'book-section';
// heading
const h = document.createElement('h' + item.level);
h.textContent = item.title;
sec.appendChild(h);
// content
const div = document.createElement('div');
div.className = 'section-content';
const p = document.createElement('p');
p.textContent = item.content;
div.appendChild(p);
sec.appendChild(div);
container.appendChild(sec);
if (item.children && item.children.length) {
buildContent(item.children, container);
}
});
}
// ==== INITIALIZE ====
const tocUl = document.getElementById('toc');
const bookContainer = document.getElementById('bookContainer');
buildTOC(bookContent, tocUl);
buildContent(bookContent, bookContainer);
// ==== TOGGLE SIDE NAV ====
const burger = document.getElementById('burger');
const sideNav = document.getElementById('sideNav');
const mainContent = document.getElementById('mainContent');
function handleResize() {
if (window.innerWidth >= 768) {
sideNav.classList.add('open');
mainContent.classList.add('desktop-open');
} else {
sideNav.classList.remove('open');
mainContent.classList.remove('desktop-open');
}
}
window.addEventListener('resize', handleResize);
handleResize();
burger.addEventListener('click', () => {
sideNav.classList.toggle('open');
if (window.innerWidth >= 768) {
mainContent.classList.toggle('desktop-open');
}
});
// ==== OPEN ALL / COLLAPSE ALL ====
document.getElementById('openAll').addEventListener('click', () => {
document.querySelectorAll('.section-content').forEach(d => d.style.display = 'block');
});
document.getElementById('collapseAll').addEventListener('click', () => {
document.querySelectorAll('.section-content').forEach(d => d.style.display = 'none');
});
</script>
</body>
</html>
How it works:
Data: The bookContent array holds every chapter/subchapter with id, level, title, content, and nested children.
TOC: Recursively built into a single <ul>; clicking an item toggles its corresponding section.
Content: Pre-rendered sections are hidden initially (.section-content {display: none;}).
Burger + Responsive: On small screens the nav slides in/out; on desktop it’s always visible and shifts the main content.
Open All / Collapse All buttons reveal or hide all sections at once.
Feel free to extend the bookContent array with more chapters and deeper nesting! 🎉