DEV Community

Zoya Khan
Zoya Khan

Posted on • Updated on

HTML Code for Random Name Generator

You can use this code to make your own random name generator for free:

<!DOCTYPE html>
    <title>Random Name Generator</title>
    <link rel="stylesheet" href="style.css">
    <h1>Random Name Generator</h1>
    <p id="name"></p>
    <button onclick="generateName()">Generate</button>
    <script src="script.js"></script>
Enter fullscreen mode Exit fullscreen mode
body {
  font-family: Arial, sans-serif;
  text-align: center;

h1 {
  font-size: 2rem;

p {
  font-size: 1.5rem;
  margin-top: 2rem;

button {
  font-size: 1.2rem;
  padding: 0.5rem 1rem;
  margin-top: 2rem;
  border: none;
  background-color: #007bff;
  color: #fff;
  border-radius: 5px;
  cursor: pointer;

button:hover {
  background-color: #0062cc;
Enter fullscreen mode Exit fullscreen mode
function generateName() {
  const names = [
    "Alice", "Bob", "Charlie", "David", "Eve", "Frank",
    "Grace", "Heidi", "Ivan", "Judy", "Kevin", "Linda"

  const index = Math.floor(Math.random() * names.length);
  const name = names[index];

  const nameElement = document.getElementById("name");
  nameElement.innerText = name;
Enter fullscreen mode Exit fullscreen mode

This code is generated by Daniel Davis from

Top comments (2)

szabgab profile image
Gabor Szabo

Just a heads up that you can add highlighting to the code blocks if you'd like. Just change:

code block with no colors example

... to specify the language:

code block with colors example

More details in our editor guide!

zoyakhanblogger profile image
Zoya Khan

Thank you :)