Simple Games Using Html Css And Javascript

Simple Games Using Html Css And Javascript

6 min read Jun 20, 2024
Simple Games Using Html Css And Javascript

Simple Games Using HTML, CSS, and JavaScript

This article will guide you through the process of creating simple games using HTML, CSS, and JavaScript. Whether you're a beginner or looking to brush up on your coding skills, this is a great way to learn the basics of web development while having fun.

Setting Up Your Project

  1. Create a new folder: This will house all the files for your game.
  2. Create three files:
    • index.html: This will be the main file that displays your game.
    • style.css: This file will hold the styling for your game's appearance.
    • script.js: This file will contain all the JavaScript logic for your game.

Basic Structure (index.html)

    Simple Game


  • <!DOCTYPE html>: This line declares the document as HTML5.
  • <html>: The root element of the HTML document.
  • <head>: Contains metadata about the document, like the title.
  • <link rel="stylesheet" href="style.css">: Links the CSS file for styling.
  • <body>: Contains the visible content of your game.
  • <script src="script.js">: Links the JavaScript file for game logic.

Styling (style.css)

You can start with basic styling for your game elements. Here's a simple example:

body {
    background-color: #f0f0f0;
    font-family: sans-serif;
    text-align: center;

#game-container {
    width: 500px;
    height: 400px;
    border: 2px solid black;
    margin: 50px auto;
    background-color: white;

Game Logic (script.js)

This is where the heart of your game lies. Here's an example of a simple "guess the number" game:

let randomNumber = Math.floor(Math.random() * 100) + 1;
let guessesLeft = 10;

document.getElementById("guessButton").addEventListener("click", function() {
    let guess = parseInt(document.getElementById("guessInput").value);

    if (guess === randomNumber) {
        alert("Congratulations! You guessed the number!");
    } else if (guess < randomNumber) {
        alert("Too low! Guesses left: " + (guessesLeft - 1));
    } else {
        alert("Too high! Guesses left: " + (guessesLeft - 1));


    if (guessesLeft === 0) {
        alert("You ran out of guesses! The number was " + randomNumber);

This code:

  1. Generates a random number between 1 and 100.
  2. Sets the number of guesses allowed to 10.
  3. Adds an event listener to the "guessButton".
  4. When the button is clicked:
    • It reads the user's guess.
    • It compares the guess to the random number and provides feedback.
    • It decrements the number of guesses left.
    • It checks if the user has run out of guesses.

Adding Elements (index.html)

You'll need to add HTML elements for the game's interface:

Guess the Number

I've chosen a number between 1 and 100. Can you guess it?

This code adds a heading, a paragraph, an input field, a button, and a paragraph for displaying messages.

Expanding Your Game

Once you have the basics down, you can expand your game by adding:

  • More levels: Increase the range of the random number.
  • Visual elements: Use images or animations to enhance the game.
  • Sound effects: Add sounds for winning, losing, or other events.
  • Scoring system: Track the user's score and display it.
  • Multiple game modes: Create different variations of the game.


Creating simple games with HTML, CSS, and JavaScript is a fun and rewarding way to learn web development. Start with a basic idea and gradually add complexity to create your own unique game! Remember to keep experimenting and have fun!