Learn how to create an electronics website using HTML, CSS, and JavaScript. Follow simple steps for a beginner-friendly project with modern design tips.

Table of Contents
Are you planning to create an electronics website but don’t know where to begin? In this blog, we will show you how to make a simple and responsive electronics website using HTML, CSS, and JavaScript. You don’t need any advanced skills. Just basic knowledge and a text editor are enough to get started.
Creating a website like this is perfect for beginners who want to learn how to build a product showcase for electronics like mobiles, laptops, smartwatches, and more.
Prerequisites
Before we begin, here’s what you need:
- A computer or laptop
- A text editor (like VS Code or Notepad++)
- A web browser (like Chrome or Firefox)
- Basic knowledge of HTML, CSS, and JavaScript
Source Code
Step 1 (HTML Code):
To get started, we will first need to create a basic HTML file. In this file, we will include the main structure for our electronics website. Let's break down the HTML code:
<!DOCTYPE html>
and <html>
<!DOCTYPE html>
: Declares this document is HTML5.<html lang="en">
: The root tag, specifies language as English.
<head>
Section
Contains metadata and links to stylesheets.
Meta Tags:
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Sets character encoding and makes the site mobile-friendly.
Title:
<title>ElectroHub - Your Tech Destination</title>
Bootstrap CSS:
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" ... />
Font Awesome:
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" ... />
Google Fonts:
<link href="https://fonts.googleapis.com/css2?family=Inter:..." rel="stylesheet" />
Custom Stylesheet:
<link rel="stylesheet" href="styles.css" />
<body>
Section
Navbar (<nav>
tag)
A responsive, fixed-top navigation bar using Bootstrap:
- Brand: ElectroHub with a lightning bolt icon.
- Links: Home, Products, Categories, Contact
- Button: "Shop Now"
It collapses into a hamburger menu on smaller screens.
Hero Section (<section id="hero">
)
The landing area with:
- Heading: "Discover the Future of Tech"
- Paragraph describing the products
- Call-to-action button: "Explore Products"
Products Section (<section id="products">
)
Displays Featured Products in a Bootstrap grid layout.
Each product has:
- Image (hosted via Unsplash)
- Title (e.g., "Smartphone X")
- Description
- Price
- "Buy Now" button
Products are structured using:
<div class="col-lg-3 col-md-6 d-flex"> ... </div>
This ensures responsiveness: 4 items per row on large screens, 2 per row on medium screens.
Each product is inside a .product-card with .card-body.
New Arrivals Section (Starts with <section id="new-arrivals">
)
This section follows the same structure as "Featured Products" but showcases newly added products. (Only partially shown in your snippet.)
Step 2 (CSS Code):
Once the basic HTML structure of the website is in place, the next step is to add styling to the electronics website using CSS. Let me break it down into simpler parts:
1. Root Variables
:root {
--primary-color: #4a148c;
--primary-hover-color: #38006b;
--accent-color: #ffab00;
--accent-hover-color: #ff8f00;
...
}
Purpose: Sets global color variables (like primary, accent, text, background colors) for easy reuse throughout the CSS file.
2. General Styling
body {
font-family: 'Inter', sans-serif;
color: var(--text-color);
}
h1, h2, ... h6 { font-weight: 600; }
a { color: var(--primary-color); }
a:hover { color: var(--primary-hover-color); }
- Applies a clean font, color, and styles headings and links.
- Links change color on hover.
3. Sections
.section-padding { padding: 80px 0; }
.section-heading { text-align: center; ... }
.section-heading::after { ... }
- Adds padding around sections.
- Underlines headings with a colored bar using ::after.
4. Buttons
.btn-primary { background-color: var(--primary-color); ... }
.btn-secondary { background-color: var(--accent-color); ... }
- Primary buttons: purple with white text.
- Secondary buttons: yellow with dark text.
- Both animate slightly on hover with transform and box-shadow.
5. Navbar
.navbar { background-color: rgba(255,255,255,0.9); ... }
.navbar.sticky { position: fixed; top: 0; ... }
- Transparent white navbar with blur.
- Sticks to top on scroll using .sticky.
6. Hero Section
#hero {
background: url(...) no-repeat center center;
background-size: cover;
color: white;
padding: 180px 0;
position: relative;
}
#hero::before { background-color: rgba(0, 0, 0, 0.5); }
- Big full-width section with a background image and dark overlay.
- Centered content with a large heading and paragraph text.
7. Product Cards
.product-card { box-shadow, hover effects, flex layout ... }
.product-card img { aspect-ratio: 1 / 1; ... }
- Product boxes with shadows and a nice layout.
- Images are square and fill the width.
8. Categories Section
.category-card { background, icon, title, hover ... }
- Clean card design with icon and text.
- Changes color and shadow on hover.
9. About Section
#about img { border-radius, shadow, responsive ... }
- Shows images with rounded corners and shadows.
10. Testimonials Slider
#testimonials { background-color: lavender ... }
.testimonial-slide { display: none; }
.testimonial-slide.active { display: block; }
- Section with a light background and sliding testimonials.
- Only one slide is visible at a time.
:root {
--primary-color: #4a148c; /* Deep Purple */
--primary-hover-color: #38006b; /* Darker Purple */
--accent-color: #ffab00; /* Gold/Yellow */
--accent-hover-color: #ff8f00; /* Darker Gold/Orange */
--light-bg: #f8f9fa; /* Light Gray */
--dark-bg: #343a40; /* Dark Gray */
--text-color: #333;
--light-text-color: #adb5bd;
--white-color: #ffffff;
--testimonial-bg: #ede7f6; /* Light Lavender/Purple */
}
/* General Styling */
body {
font-family: 'Inter', sans-serif;
color: var(--text-color);
line-height: 1.6;
}
h1, h2, h3, h4, h5, h6 {
font-weight: 600;
}
a {
text-decoration: none;
color: var(--primary-color);
}
a:hover {
color: var(--primary-hover-color);
}
.section-padding {
padding: 80px 0;
}
.section-heading {
text-align: center;
margin-bottom: 50px;
font-weight: 700;
position: relative;
padding-bottom: 15px;
}
/* Underline effect for headings */
.section-heading::after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 80px;
height: 4px;
background-color: var(--primary-color); /* Use primary color */
border-radius: 2px;
}
.btn-primary {
background-color: var(--primary-color);
border-color: var(--primary-color);
color: var(--white-color); /* Ensure text is white */
padding: 10px 25px;
font-weight: 500;
border-radius: 50px; /* Pill shape */
transition: all 0.3s ease;
}
.btn-primary:hover {
background-color: var(--primary-hover-color);
border-color: var(--primary-hover-color);
color: var(--white-color);
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.btn-secondary {
background-color: var(--accent-color); /* Use accent color */
border-color: var(--accent-color);
padding: 8px 20px;
font-weight: 500;
border-radius: 50px;
transition: all 0.3s ease;
color: var(--text-color); /* Use dark text for contrast */
}
.btn-secondary:hover {
background-color: var(--accent-hover-color);
border-color: var(--accent-hover-color);
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
color: var(--text-color);
}
/* Navbar */
.navbar {
transition: background-color 0.3s ease, padding 0.3s ease;
background-color: rgba(255, 255, 255, 0.9);
backdrop-filter: blur(5px);
padding: 10px 0;
}
.navbar-brand {
font-weight: 700;
font-size: 1.5rem;
color: var(--primary-color) !important; /* Use primary color */
}
.navbar-brand i {
margin-right: 5px;
}
.navbar-nav .nav-link {
color: #555;
font-weight: 500;
margin: 0 10px;
transition: color 0.3s ease;
}
.navbar-nav .nav-link:hover,
.navbar-nav .nav-link.active {
color: var(--primary-color); /* Use primary color */
}
.navbar.sticky {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: var(--white-color);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
z-index: 1030;
padding: 5px 0;
}
/* Hero Section */
#hero {
/* Keep existing background, overlay adds darkness */
background: url('https://images.unsplash.com/photo-1669937401489-fb1ebb31d762?q=80&w=1470&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D') no-repeat center center; /* Updated placeholder */
background-size: cover;
color: var(--white-color);
text-align: center;
padding: 180px 0;
position: relative;
}
#hero::before { /* Overlay */
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5); /* Dark overlay */
}
#hero .container {
position: relative; /* Ensure content is above overlay */
z-index: 2;
}
#hero h1 {
font-size: 3.5rem;
font-weight: 700;
margin-bottom: 20px;
text-shadow: 1px 1px 3px rgba(0,0,0,0.7);
}
#hero p {
font-size: 1.2rem;
margin-bottom: 30px;
max-width: 600px;
margin-left: auto;
margin-right: auto;
text-shadow: 1px 1px 2px rgba(0,0,0,0.7);
}
/* Hero button uses primary style */
/* Products Section (Shared Styles) */
#products, #new-arrivals {
background-color: var(--light-bg);
}
#categories {
background-color: var(--white-color);
}
.product-card {
border: none;
border-radius: 15px;
overflow: hidden;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
transition: transform 0.3s ease, box-shadow 0.3s ease;
background-color: var(--white-color);
height: 100%;
display: flex;
flex-direction: column;
}
.product-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(74, 20, 140, 0.12); /* Use subtle primary color shadow */
}
.product-card img {
aspect-ratio: 1 / 1;
object-fit: cover;
max-height: 250px;
width: 100%;
}
.product-card .card-body {
padding: 25px;
text-align: center;
flex-grow: 1;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.product-card .card-title {
font-weight: 600;
margin-bottom: 10px;
font-size: 1.1rem;
}
.product-card .card-text {
color: #6c757d; /* Keep secondary text gray */
font-size: 0.9rem;
margin-bottom: 15px;
flex-grow: 1;
}
.product-card .price {
font-size: 1.2rem;
font-weight: 700;
color: var(--primary-color); /* Use primary color for price */
margin-bottom: 20px;
}
/* Product card button uses secondary (accent) style */
/* Shop by Category Section */
.category-card {
background-color: var(--white-color);
border-radius: 15px;
padding: 30px 20px;
text-align: center;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
transition: transform 0.3s ease, box-shadow 0.3s ease;
border: 1px solid #eee;
height: 100%;
}
.category-card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 20px rgba(74, 20, 140, 0.1); /* Use subtle primary color shadow */
border-color: var(--primary-color);
}
.category-card i {
font-size: 3rem;
color: var(--primary-color); /* Use primary color for icons */
margin-bottom: 20px;
display: block;
}
.category-card h5 {
font-weight: 600;
margin-bottom: 0;
color: var(--text-color);
}
.category-card a {
color: inherit;
text-decoration: none;
}
.category-card a:hover h5 {
color: var(--primary-hover-color); /* Darker primary on hover */
}
/* About Section */
#about {
background-color: var(--white-color); /* Ensure white background */
}
#about img {
border-radius: 15px;
max-width: 100%;
height: auto;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
/* About button uses primary style */
/* Testimonials Section */
#testimonials {
background-color: var(--testimonial-bg); /* Use light lavender background */
}
.testimonial-slider {
position: relative;
max-width: 800px;
margin: auto;
overflow: hidden;
border-radius: 15px;
background-color: var(--white-color);
padding: 40px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
min-height: 280px;
display: flex;
align-items: center;
}
.slider-content {
width: 100%;
}
.testimonial-slide {
display: none;
text-align: center;
animation: fadeIn 0.5s ease-in-out;
}
.testimonial-slide.active {
display: block;
}
.testimonial-slide img {
width: 80px;
height: 80px;
border-radius: 50%;
margin-bottom: 20px;
object-fit: cover;
border: 3px solid #eee;
}
.testimonial-slide blockquote {
font-style: italic;
color: #555;
margin-bottom: 15px;
font-size: 1.1rem;
}
.testimonial-slide .author {
font-weight: 600;
color: var(--text-color);
}
.slider-controls {
position: absolute;
top: 50%;
width: 100%;
display: flex;
justify-content: space-between;
transform: translateY(-50%);
padding: 0 10px;
left: 0;
}
.slider-controls button {
background-color: rgba(0, 0, 0, 0.3);
color: var(--white-color);
border: none;
border-radius: 50%;
width: 40px;
height: 40px;
font-size: 1.2rem;
cursor: pointer;
transition: background-color 0.3s ease;
z-index: 5;
}
.slider-controls button:hover {
background-color: rgba(0, 0, 0, 0.6);
}
@keyframes fadeIn {
from { opacity: 0; transform: scale(0.95); }
to { opacity: 1; transform: scale(1); }
}
/* Footer */
footer {
background-color: var(--dark-bg);
color: var(--light-text-color);
padding-top: 60px;
padding-bottom: 30px;
}
footer h5 {
color: var(--white-color);
margin-bottom: 20px;
font-weight: 600;
}
footer p, footer ul {
font-size: 0.9rem;
}
footer ul {
list-style: none;
padding-left: 0;
}
footer ul li a {
color: var(--light-text-color);
transition: color 0.3s ease;
display: inline-block;
margin-bottom: 8px;
}
footer ul li a:hover {
color: var(--white-color);
}
footer .social-icons a {
color: var(--light-text-color);
font-size: 1.3rem;
margin-right: 15px;
transition: color 0.3s ease;
}
footer .social-icons a:hover {
color: var(--accent-color); /* Use accent color on hover */
}
footer .footer-bottom {
border-top: 1px solid #495057;
padding-top: 20px;
margin-top: 40px;
text-align: center;
font-size: 0.85rem;
}
/* Smooth Scroll */
html {
scroll-behavior: smooth;
}
Step 3 (JavaScript Code):
Finally, we need to create a function in JavaScript. Let’s break this JavaScript code down into four main features:
1. Sticky Navbar
Purpose: Makes the navigation bar "stick" to the top of the screen when scrolling down.
const navbar = document.getElementById('navbar');
if (navbar) {
const stickyOffset = navbar.offsetTop + 100;
function handleScroll() {
if (window.pageYOffset > stickyOffset) {
navbar.classList.add('sticky');
} else {
navbar.classList.remove('sticky');
}
}
window.addEventListener('scroll', handleScroll);
}
Explanation:
- Gets the navbar element by its ID.
- Calculates when the navbar should become sticky (offsetTop + 100 is used as a buffer).
- On scroll, if you've scrolled past that offset, it adds a .sticky class to the navbar, otherwise, it removes it.
2. Testimonial Slider
Purpose: Displays customer testimonials in a slider format that auto-rotates every 5 seconds.
const testimonials = [ ... ]; // Array of testimonial objects
let currentSlide = 0;
const sliderContent = document.querySelector('.testimonial-slider .slider-content');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
let autoSlideInterval;
Functions Used:
1. renderSlider()
- Dynamically creates slide elements from the testimonial array and injects them into the slider container.
2. showSlide(index)
- Displays only the slide at the given index.
- Handles wrap-around: if index goes beyond range, it wraps to start/end.
3. startAutoSlide() & stopAutoSlide()
- startAutoSlide() sets up an interval to change slides every 5 seconds.
- stopAutoSlide() clears the interval (used when manually navigating).
4. Manual Controls
- nextBtn and prevBtn let users manually navigate slides and stop auto-sliding.
renderSlider(); // Setup all slides
showSlide(currentSlide); // Show first slide
startAutoSlide(); // Start rotating
3. Update Footer Year
Purpose: Auto-updates the year in the footer to the current year.
const currentYearSpan = document.getElementById('currentYear');
if (currentYearSpan) {
currentYearSpan.textContent = new Date().getFullYear();
}
Example: If the year is 2025, it sets currentYearSpan.textContent to 2025.
4. Highlight Active Navbar Link on Scroll
Purpose: Highlights the nav link based on the section currently in view.
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('.navbar-nav .nav-link[href^="#"]');
Function: changeNavOnScroll()
- Loops through each section and checks which one is currently in view.
- Adds the active class to the corresponding nav link.
- Handles special case when you're at the top of the page (#hero).
- Also handles edge case when you're at the bottom of the page.
window.addEventListener('scroll', changeNavOnScroll);
changeNavOnScroll(); // Run once in case page loads mid-scroll
// --- Sticky Navbar ---
const navbar = document.getElementById('navbar');
// Make sure navbar exists before adding listener
if (navbar) {
const stickyOffset = navbar.offsetTop + 100; // Add a buffer
function handleScroll() {
if (window.pageYOffset > stickyOffset) {
navbar.classList.add('sticky');
} else {
navbar.classList.remove('sticky');
}
}
window.addEventListener('scroll', handleScroll);
}
// --- Testimonial Slider ---
const testimonials = [
{
img: 'https://images.unsplash.com/photo-1642364861013-2c33f2dcfbcf', // Updated placeholder
quote:
'Amazing selection and fast shipping! Found exactly what I needed for my new setup.',
author: 'Alex Johnson',
},
{
img: 'https://images.unsplash.com/photo-1659353220441-9207b962a133', // Updated placeholder
quote:
'The customer service was top-notch. They helped me choose the perfect laptop.',
author: 'Maria Garcia',
},
{
img: 'https://images.unsplash.com/photo-1733796941440-9935f13a1cec', // Updated placeholder
quote:
'Great prices and the quality of the smartwatch exceeded my expectations. Highly recommend!',
author: 'David Smith',
},
{
img: 'https://images.unsplash.com/photo-1700832161143-de261675534b', // Updated placeholder
quote:
'ElectroHub is my go-to for tech gadgets. Always reliable and great deals.',
author: 'Sam Lee',
},
];
let currentSlide = 0;
const sliderContent = document.querySelector(
'.testimonial-slider .slider-content'
);
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
let autoSlideInterval; // Variable to hold the interval ID
function renderSlider() {
if (!sliderContent) return; // Exit if slider content area doesn't exist
sliderContent.innerHTML = ''; // Clear existing slides
testimonials.forEach((testimonial, index) => {
const slideDiv = document.createElement('div');
slideDiv.classList.add('testimonial-slide');
// No need to add 'active' here, showSlide handles it
slideDiv.innerHTML = `
<img src="${testimonial.img}" alt="Customer ${testimonial.author}">
<blockquote>"${testimonial.quote}"</blockquote>
<p class="author">- ${testimonial.author}</p>
`;
sliderContent.appendChild(slideDiv);
});
}
function showSlide(index) {
if (!sliderContent) return; // Exit if slider content area doesn't exist
const slides = sliderContent.querySelectorAll('.testimonial-slide');
if (slides.length === 0) return; // Exit if no slides rendered
if (index >= slides.length) {
currentSlide = 0;
} else if (index < 0) {
currentSlide = slides.length - 1;
} else {
currentSlide = index;
}
// Hide all slides
slides.forEach((slide) => slide.classList.remove('active'));
// Show the current slide
slides[currentSlide].classList.add('active');
}
function startAutoSlide() {
stopAutoSlide(); // Clear existing interval first
autoSlideInterval = setInterval(() => {
showSlide(currentSlide + 1);
}, 5000); // Change slide every 5 seconds
}
function stopAutoSlide() {
clearInterval(autoSlideInterval);
}
// Event Listeners for slider controls
if (nextBtn && prevBtn) {
nextBtn.addEventListener('click', () => {
showSlide(currentSlide + 1);
stopAutoSlide(); // Stop auto-slide on manual control
});
prevBtn.addEventListener('click', () => {
showSlide(currentSlide - 1);
stopAutoSlide(); // Stop auto-slide on manual control
});
}
// Initial setup for slider
renderSlider(); // Create the slides first
showSlide(currentSlide); // Then show the initial active slide
startAutoSlide(); // Start auto-sliding
// --- Update Footer Year ---
const currentYearSpan = document.getElementById('currentYear');
if (currentYearSpan) {
currentYearSpan.textContent = new Date().getFullYear();
}
// --- Navbar Active Link highlighting on Scroll ---
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('.navbar-nav .nav-link[href^="#"]');
function changeNavOnScroll() {
if (!navLinks || navLinks.length === 0) return; // Exit if no nav links found
let currentSection = '';
const offset = navbar ? navbar.offsetHeight + 20 : 100; // Offset based on navbar height
sections.forEach((section) => {
const sectionTop = section.offsetTop;
// Check if scroll position is within the section (adjust offset as needed)
if (pageYOffset >= sectionTop - offset) {
currentSection = section.getAttribute('id');
}
});
// If scrolled past the last section, keep the last link active
if (
window.innerHeight + window.pageYOffset >=
document.body.offsetHeight - 50
) {
// Near bottom
const lastSection = sections[sections.length - 1];
if (lastSection) currentSection = lastSection.id;
}
navLinks.forEach((link) => {
link.classList.remove('active');
const linkHref = link.getAttribute('href');
// Check if the link's href matches the current section ID
if (linkHref === `#${currentSection}`) {
link.classList.add('active');
}
});
// Special case for top of page
if (pageYOffset < sections[0].offsetTop - offset) {
// Before the first section
navLinks.forEach((link) => link.classList.remove('active'));
const homeLink = document.querySelector(
'.navbar-nav .nav-link[href="#hero"]'
);
if (homeLink) homeLink.classList.add('active');
}
}
window.addEventListener('scroll', changeNavOnScroll);
// Initial check in case page loads scrolled down
changeNavOnScroll();
Final Output:

Conclusion:
Creating an electronics website using HTML, CSS, and JavaScript is simple when you follow the right steps. You can easily customize the design, add more products, or even link it to a shopping cart later.
This project is a great way to practice frontend web development and build your portfolio. Whether you are a student or hobbyist, this tutorial will help you build confidence and skills.
That’s a wrap!
I hope you enjoyed this post. Now, with these examples, you can create your own amazing page.
Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee
And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!
Thanks!
Faraz 😊