give me codes for a basic website with a navbar containing 'home' , 'about', 'contact' .…
- User
give me codes for a basic website with a navbar containing 'home' , 'about', 'contact' . codes in html, css, javascripts separately
- Assistant
Below is the code for a basic website with a navigation bar (navbar) containing the links "Home", "About", and "Contact". I've provided the code in three separate parts: HTML, CSS, and JavaScript. This setup assumes you have three files:
index.html,styles.css, andscript.js. Place them in the same directory, and link them appropriately in the HTML file.The navbar is styled to be horizontal with a dark background, and the JavaScript adds basic interactivity by highlighting the active link when clicked. The links scroll to corresponding sections on the same page (e.g., clicking "Home" scrolls to the home section).
1. HTML Code (index.html)
This file contains the structure of the website, including the navbar and sections for "Home", "About", and "Contact". It links to the CSS and JavaScript files.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Basic Website</title> <link rel="stylesheet" href="styles.css"> </head> <body> <nav> <ul> <li><a href="#home">Home</a></li> <li><a href="#about">About</a></li> <li><a href="#contact">Contact</a></li> </ul> </nav> <section id="home"> <h1>Home Section</h1> <p>Welcome to the home page. This is a basic website example.</p> </section> <section id="about"> <h1>About Section</h1> <p>Here you can find information about us.</p> </section> <section id="contact"> <h1>Contact Section</h1> <p>Contact us through this section. (e.g., email or phone)</p> </section> <script src="script.js"></script> </body> </html>2. CSS Code (styles.css)
This file styles the navbar and the page elements. It makes the navbar horizontal, adds colors, and defines the active state for the clicked link.
/* Reset some default styles for better layout */ body { margin: 0; font-family: Arial, sans-serif; } /* Style the navbar */ nav { background-color: #333; overflow: hidden; } nav ul { list-style-type: none; margin: 0; padding: 0; } nav li { float: left; } nav li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } nav li a:hover { background-color: #111; } /* Style for active link */ nav li a.active { background-color: #4CAF50; color: white; } /* Style the sections */ section { padding: 20px; margin: 20px; border: 1px solid #ddd; background-color: #f9f9f9; }3. JavaScript Code (script.js)
This file adds interactivity to the navbar. It sets the "Home" link as active by default and updates the active class when a navbar link is clicked. This highlights the current section's link.
document.addEventListener('DOMContentLoaded', function() { // Set home link as active by default document.querySelector('nav a[href="#home"]').classList.add('active'); // Get all navbar links const links = document.querySelectorAll('nav a'); // Add click event to each link links.forEach(link => { link.addEventListener('click', function() { // Remove active class from all links links.forEach(l => l.classList.remove('active')); // Add active class to the clicked link this.classList.add('active'); }); }); });How to Use:
- Create three files in the same folder:
index.html(copy the HTML code)styles.css(copy the CSS code)script.js(copy the JavaScript code)
- Open
index.htmlin a web browser to see the website. - The navbar links will scroll to the respective sections, and the active link will be highlighted when clicked.
This is a minimal example. If you want to add more features (e.g., smooth scrolling or external page links), let me know for enhancements!
- Create three files in the same folder:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.