Random Emojis Using HTML, CSS & JavaScript

5 min read
Random Emojis Demo

Project Overview

This project demonstrates a simple "Random Emoji Generator" using HTML, CSS, and JavaScript. It features a large emoji display that changes to a new random emoji whenever the user hovers over it. This interactive element provides a fun and engaging visual experience, showcasing basic JavaScript event handling and array manipulation for dynamic content updates.

The design uses a neutral gray background to make the colorful emojis stand out. The core of the functionality lies in JavaScript, which maintains an array of various emojis and, on a `mouseover` event, randomly selects one from the array to update the `innerHTML` of the emoji display element. CSS is used to style the emoji for a large, prominent display, and includes a subtle grayscale filter and scale transformation on hover to enhance the visual feedback. This project is an excellent example of combining HTML structure, CSS styling, and JavaScript event listeners for interactive UI elements.

HTML Structure

The HTML structure for this random emoji generator is concise. It is contained within a `main` element with the class `emoji-generator`. Inside, there are two `section` elements: `display-section` and `history-section`. The `display-section` contains a `div` with a `generateButton` and a `div` with `id="emojiDisplay"` which initially displays a default emoji. The `history-section` contains a `div` for `historyPreview`, a `clearHistoryButton`, and another `div` with `id="emojiHistory"` to display a history of generated emojis. All interactive elements have `aria-label` for accessibility. A `script` tag links to an external JavaScript file (`app.js`) to handle the emoji generation and history logic. This structure provides a clear and accessible interface for the emoji generator.


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Random Emoji Generator</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <main class="emoji-generator">
      <section class="display-section">
        <div class="button-group">
          <button id="generateButton" class="action-btn generate-btn">Generate Emoji</button>
        </div>
        <div 
          id="emojiDisplay" 
          class="emoji-display" 
          aria-label="Random emoji display"
        >&#128514;</div>
      </section>

      <section class="history-section">
        <div id="historyPreview" class="history-preview"></div>
        <button id="clearHistoryButton" class="action-btn clear-btn">Clear History</button>
        <div 
          id="emojiHistory" 
          class="emoji-history" 
          aria-label="Emoji history"
        ></div>
      </section>
    </main>

    <script src="app.js"></script>
  </body>
</html>
                        

CSS Styling

The CSS for this project styles the emoji generator for a clean and interactive appearance. The `body` is set to take up the full viewport height, centering its content with a light gray (`#ccc`) background. The main emoji display element (with `id="emoji"`, though the HTML uses `emojiDisplay`) is styled with a large `font-size` for prominence. It initially has a `filter: grayscale(1)` applied, making it appear black and white. A `transition` property is set for `transform` and `filter` with a duration of `200ms` for smooth animations. On `hover`, the emoji scales up (`transform: scale(1.3)`) and the grayscale filter is removed (`filter: grayscale(0)`), revealing its full color. This combination of styles provides a visually engaging and responsive effect for the emoji.


body {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #ccc;
}

#emoji {
  font-size: 10rem;
  filter: grayscale(1);
  transition-property: transfrom, filter;
  transition-duration: 200ms;
  cursor: pointer;
}

#emoji:hover {
  transform: scale(1.3);
  filter: grayscale(0);
}

                        

JavaScript

The JavaScript for this project handles the core logic of generating random emojis. It first selects the emoji display element (referred to as `btn` in the script, but `emojiDisplay` in HTML, with `id="emoji"` in the original script) and defines an array named `emojis` containing a variety of emoji characters. An event listener is attached to the `btn` element for the `mouseover` event. When the mouse hovers over the emoji, the event listener triggers a function that randomly selects an emoji from the `emojis` array using `Math.random()` and `Math.floor()`. The `innerHTML` of the `btn` element is then updated with the newly selected random emoji, creating the dynamic effect.


const btn = document.querySelector("#emoji");
const emojis = [
  "๐Ÿ˜†",
  "๐Ÿ˜…",
  "๐Ÿคฃ",
  "๐Ÿ˜‚",
  "๐Ÿ˜€",
  "๐Ÿค‘",
  "๐Ÿคจ",
  "๐Ÿ™‚",
  "๐Ÿ˜Š",
  "๐Ÿ˜—",
  "๏ฟฝ",
  "๐Ÿ˜",
  "๐Ÿคฅ",
  "๐Ÿ˜ด",
  "๐Ÿฅบ",
  "๐Ÿ˜ง",
  "๐Ÿ˜‡",
  "๐Ÿ˜ณ",
  "๐Ÿ™ƒ",
  "๐Ÿฅด",
  "๐Ÿง",
  "๐Ÿคจ",
  "๐Ÿ˜’",
  "๐Ÿค”",
  "๐Ÿคญ",
  "๐Ÿฅฐ",
  "๐Ÿค",
  "๐Ÿ‘€",
  "๐Ÿค”",
  "๐Ÿคช",
  "๐Ÿฅฒ",
  "๐Ÿ˜ƒ",
  "๐Ÿ˜",
  "๐Ÿ˜ฌ",
];

btn.addEventListener("mouseover", () => {
  btn.innerHTML = emojis[Math.floor(Math.random() * emojis.length)];
});

                        

Live Demo: Random Emoji Generator

Watch a live demonstration of the Random Emoji Generator below.

Final Thoughts

This "Random Emoji Generator" project provides a fun and simple example of dynamic content updates using HTML, CSS, and JavaScript. It effectively demonstrates how to use arrays to store data, `Math.random()` for random selection, and event listeners to trigger interactive changes. This project is a great starting point for understanding basic JavaScript manipulation of the DOM and creating engaging user interfaces.

You can easily customize this component by:

  • Expanding the `emojis` array to include a wider variety of emojis or other characters.
  • Changing the event listener from `mouseover` to `click` for a different interaction.
  • Adding a "history" feature to display previously generated emojis.
  • Implementing a "copy to clipboard" feature for the displayed emoji.
  • Enhancing the CSS with more complex animations or styling for the emoji display.

Pro Tip

For more complex random selections or to avoid repetition, you might consider shuffling the `emojis` array and then iterating through it, or keeping track of recently displayed emojis to ensure variety. This can enhance the "randomness" and user experience over time.

Ready to Use This Project?

Click the button below to download the full source code. Download will be ready in 10 seconds.

Stay Updated

Receive coding tips and resources updates. No spam.

We respect your privacy. Unsubscribe at any time.