Open In App

Create a Single Page Application using HTML CSS & JavaScript

Last Updated : 25 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In this article, we are going to design and build a cool and user-friendly single-page application (SPA) using just HTML, CSS, and JavaScript. A single-page application contains multiple pages which can be navigated or visited without loading the page every time. This makes things faster and more interesting.

Preview Image:

SPA
Final Output

Approach:

  • Firstly, we will create a folder with the project name and create the HTML(e.g index.html) , CSS (e.g style.css) , JavaScript files, Images folder and a separate file for the responsive CSS if required.
  • Now, use the different HTML tags like section, header, meta, title, head,div, img etc to structure the web page. and includes a header, navigation bar, main content area, and a script tag for our JavaScript file.
  • Add some Style using CSS to the HTML code to make our frontend look good. We'll choose colors and layouts that look modern and inviting. Add style mainly to the image and beautify the header. and we will be using a gradient color to give a greenish look to the header.
  • Add the JavaScript to change content dynamically. let's add some JavaScript magic. The changeContent function will be our main things, updating the content based on what the user clicks.
  • You can create a navigation system that dynamically loads content based on user interactions.

Example: The below code example explains how you can use the HTML, CSS and JavaScript to create a Single Page Application.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
	<link rel="stylesheet" href="style.css">
	<title>SPA app</title>
</head>

<body>
	<div id="app">
		<header>
			<h1>
				GeeksforGeeks
			</h1>
			<h1>
				Single Page Application Using
				HTML, CSS & JavaScript
			</h1>
		</header>
		<nav>
			<ul>
				<li>
					<a href="#" onclick=
					"changeContent('home')">
						Home
					</a>
				</li>
				<li>
					<a href="#" onclick=
					"changeContent('about')">
						About
					</a>
				</li>
				<li>
					<a href="#" onclick=
					"changeContent('contact')">
						Contact
					</a>
				</li>
			</ul>
		</nav>
		<main>
			<div id="content">

				<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-12.png">
				<h2>Welcome to Geeks for Geeks</h2>
				<p>
					Geeks for Geeks is a portal for computer
					science enthusiasts, providing a wide range of
					tutorials, articles, and resources.
				</p>
				<p>
					Visit the GeeksforGeeks portal
					<a href="https://www.geeksforgeeks.org/" target="_blank">
						here
					</a>.
				</p>
			</div>
		</main>
	</div>
	<script src="script.js"></script>
</body>

</html>
CSS JavaScript

Output:


Next Article

Similar Reads

three90RightbarBannerImg