Learn How to Code a Tic Tac Toe Game in HTML, CSS and JS - Download the Source Files
How to Create a Tic Tac Toe Game Using HTML, CSS, and JS
Tic tac toe is a popular and simple game that involves two players who take turns marking the spaces in a 3x3 grid with X or O. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner. Tic tac toe is also known as noughts and crosses or Xs and Os, and it has a long history that dates back to ancient Egypt and Rome.
tic tac toe html code download
In this article, you will learn how to create your own tic tac toe game using HTML, CSS, and JS. You will use HTML to create the basic structure of the game board, CSS to style the board and the symbols, and JS to add interactivity and logic to the game. You will also learn how to implement the winning strategy and the reset button for the game. By following these steps, you will be able to create a fun and responsive tic tac toe game that you can play on your browser or share with your friends.
HTML section
The first step is to create the HTML file for your tic tac toe game. You can name it index.html or anything you like. In this file, you will create the basic structure of the game board using HTML tags and attributes. You will also link the external CSS and JS files that you will create later.
The HTML file should look something like this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tic Tac Toe Game</title> <link rel="stylesheet" href="style.css"> <link rel="preconnect" href="[18]( <link href="[17]( rel="stylesheet"> </head> <body> <main > <section > <h1>Tic Tac Toe</h1> </section> <section > Player <span >X</span>'s turn </section> <section > <div ></div> <div ></div> <div ></div> <div ></div> <div ></div> <div ></div> <div ></div> <div ></div> <div ></div> </section> <section > <button >Reset Game</button> </section> </main> <script src="script.js"></script> </body> </html>
Let's break down the HTML code and see what each part does:
The <!DOCTYPE html> declaration specifies the HTML version that the document is using.
The <html> element is the root element of the document, and it has a lang attribute that specifies the language of the document.
The <head> element contains the metadata of the document, such as the title, the character encoding, the viewport, and the links to the external files.
The <title> element defines the title of the document that appears on the browser tab.
The <link> elements link the external CSS and JS files to the HTML file. The first link element also links a Google font that we will use for styling the game.
The <body> element contains the visible content of the document, such as the main, section, and div elements.
The <main> element is a semantic element that defines the main content of the document. It has a class attribute that we will use for styling the background of the game.
The <section> elements are semantic elements that define different sections of the document. Each section has a class attribute that we will use for styling and selecting the elements. The first section contains the title of the game, the second section contains the display of the current player's turn, the third section contains the game board, and the fourth section contains the reset button.
The <div> elements are generic containers that we will use for creating and styling the tiles of the game board. Each div element has a class attribute that we will use for styling and selecting the tiles.
The <h1> element defines the main heading of the document, which is also the title of the game.
The <span> element defines a part of text within another element. We will use it to display and style the current player's symbol (X or O).
The <button> element defines a clickable button that we will use to reset the game.
The <script> element links the external JS file to the HTML file. It should be placed at the end of the body element so that it runs after the HTML elements are loaded.
CSS section
The next step is to create the CSS file for your tic tac toe game. You can name it style.css or anything you like. In this file, you will style , and applies some styles to them, such as color.
The .reset selector selects the section element with the class attribute of reset, and applies some styles to it, such as margin-top.
The .reset-button selector selects the button element with the class attribute of reset-button, and applies some styles to it, such as width, height, border, border-radius, font-family, font-size, color, background-color, and cursor.
The .reset-button:hover selector selects the reset-button element when it is hovered over by the mouse pointer, and applies some styles to it, such as transform.
The @media screen and (max-width: 600px) rule defines a media query that applies some styles to the elements when the screen width is less than or equal to 600px. This makes the game responsive and adaptable to different screen sizes.
The @keyframes bounce rule defines an animation that makes the elements bounce by changing their scale property.
The .winner selector selects the tile elements with the class attribute of winner, and applies an animation to them using the animation property.
JS section
The final step is to create the JS file for your tic tac toe game. You can name it script.js or anything you like. In this file, you will add interactivity and logic to the game using JS variables, functions, events, and conditions. You will also implement the winning strategy and the reset button for the game.
How to create a tic tac toe game using HTML, CSS and JavaScript[^1^]
GitHub repositories for tic tac toe game projects in different languages[^2^]
Step-by-step tutorial for making a tic tac toe game with HTML, CSS and JS[^3^]
Tic tac toe game source code download for free
Tic tac toe game online multiplayer with Node.js and Flutter
Tic tac toe game AI integrated with Python and Tkinter
Tic tac toe game with custom shapes and colors in JavaScript
Tic tac toe game with invincible AI using Minimax algorithm in Go
Tic tac toe game with CSS grid and animations
Tic tac toe game with voice recognition and speech synthesis
Tic tac toe game with Firebase and React Native
Tic tac toe game with SVG graphics and D3.js
Tic tac toe game with socket.io and Express.js
Tic tac toe game with Bootstrap and jQuery
Tic tac toe game with React hooks and Redux
Tic tac toe game with Vue.js and Vuex
Tic tac toe game with Angular and RxJS
Tic tac toe game with Svelte and Tailwind CSS
Tic tac toe game with Laravel and Vue.js
Tic tac toe game with Django and React.js
Tic tac toe game with Flask and Jinja2
Tic tac toe game with Ruby on Rails and Stimulus.js
Tic tac toe game with ASP.NET Core and Blazor
Tic tac toe game with PHP and Laravel Livewire
Tic tac toe game with WordPress and Elementor
Tic tac toe game with Drupal and Gatsby.js
Tic tac toe game with Magento and PWA Studio
Tic tac toe game with Shopify and Liquid
Tic tac toe game with Wix and Corvid
Tic tac toe game with Squarespace and Code Injection
Tic tac toe game with Webflow and custom code
Tic tac toe game with Bubble and plugins
Tic tac toe game with App Inventor and blocks
Tic tac toe game with Scratch and sprites
Tic tac toe game with Unity and C#
Tic tac toe game with Unreal Engine and Blueprint
Tic tac toe game with Godot Engine and GDScript
Tic tac toe game with Phaser and TypeScript
Tic tac toe game with Cocos Creator and JavaScript
Tic tac toe game with Pygame and Python
Tic tac toe game with LibGDX and Java
Tic tac toe game with SFML and C++
Tic tac toe game with SDL and C
Tic tac toe game with Love2D and Lua
Tic tac toe game with MonoGame and F#
Tic tac toe game with Corona SDK and Lua
Tic tac toe game with GDevelop and events
Tic tac toe game optimization tips and tricks
Tic tic toc code best practices and standards
The JS file should look something like this:
// Selecting the elements from the HTML file const tiles = document.querySelectorAll(".tile"); const display = document.querySelector(".display"); const displayPlayer = document.querySelector(".display-player"); const resetButton = document.querySelector(".reset-button"); // Declaring some variables for storing the game state let currentPlayer = "X"; // The current player's symbol (X or O) let gameStatus = "Game On"; // The status of the game (Game On or Game Over) let board = ["", "", "", "", "", "", "", "", ""]; // The array that represents the game board // Declaring some constants for storing the winning combinations const winningCombos = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]; // Adding an event listener to each tile eleme