I'll create a basic single-page application structure for you. Here's a simple example using vanilla JavaScript, HTML, and CSS:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My SPA</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<nav>
<a href="#home" class="nav-link">Home</a>
<a href="#about" class="nav-link">About</a>
<a href="#contact" class="nav-link">Contact</a>
</nav>
<main id="app">
<!-- Content will be dynamically loaded here -->
</main>
<script src="app.js"></script>
</body>
</html>
app.js
// Define routes and their content
const routes = {
home: {
title: 'Home',
content: `
<h1>Welcome Home</h1>
<p>This is the home page of our single page application.</p>
`
},
about: {
title: 'About',
content: `
<h1>About Us</h1>
<p>Learn more about our single page application.</p>
`
},
contact: {
title: 'Contact',
content: `
<h1>Contact Us</h1>
<p>Get in touch with us.</p>
<form id="contact-form">
<input type="email" placeholder="Your email" required>
<textarea placeholder="Your message" required></textarea>
<button type="submit">Send</button>
</form>
`
}
};
// Router function
function router() {
// Get the current hash or default to 'home'
const hash = window.location.hash.slice(1) || 'home';
const route = routes[hash];
if (route) {
// Update page title
document.title = `${route.title} - My SPA`;
// Update content
document.getElementById('app').innerHTML = route.content;
// Update active nav link
document.querySelectorAll('.nav-link').forEach(link => {
link.classList.toggle('active', link.getAttribute('href') === `#${hash}`);
});
// Attach event listeners if needed
if (hash === 'contact') {
const form = document.getElementById('contact-form');
form.addEventListener('submit', (e) => {
e.preventDefault();
alert('Form submitted!');
});
}
} else {
// 404 page
document.getElementById('app').innerHTML = '<h1>404 - Page Not Found</h1>';
}
}
// Listen for hash changes
window.addEventListener('hashchange', router);
// Initial load
window.addEventListener('DOMContentLoaded', router);
styles.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
}
nav {
background-color: #333;
padding: 1rem;
position: sticky;
top: 0;
z-index: 100;
}
nav a {
color: white;
text-decoration: none;
padding: 0.5rem 1rem;
margin-right: 1rem;
border-radius: 4px;
transition: background-color 0.3s;
}
nav a:hover {
background-color: #555;
}
nav a.active {
background-color: #007bff;
}
main {
max-width: 800px;
margin: 2rem auto;
padding: 0 1rem;
}
h1 {
margin-bottom: 1rem;
color: #333;
}
form {
display: flex;
flex-direction: column;
gap: 1rem;
margin-top: 1rem;
}
input, textarea {
padding: 0.5rem;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1rem;
}
button {
padding: 0.5rem 1rem;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background-color: #0056b3;
}
This basic SPA includes:
- Client-side routing using hash-based navigation
- Dynamic content loading without page refreshes
- Active navigation state highlighting
- Form handling example on the contact page
- Responsive design basics
To enhance this further, you might want to add:
- A JavaScript framework like React, Vue, or Angular
- A build tool like Webpack or Vite
- API integration for dynamic data
- State management
- More sophisticated routing with libraries like React Router
- CSS preprocessors or frameworks
- Testing setup
Would you like me to show you how to implement any of these enhancements or convert this to use a specific framework?