Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptGenerate a Random Birthday Wishes using JavaScript

Generate a Random Birthday Wishes using JavaScript

In this article, we are going to learn how to create a webpage that generates random Birthday wishes using HTML, CSS, and JavaScript.

Approach: To generate a random birthday wish we will follow the below-given steps.

  • HTML Code: In this section, we will create a basic structure of the HTML file i.e. it contains <html>, <head>, <body> tags, etc. 
  • CSS Code: It is used to set the styles to the HTML document. Here, we set the width, height & position of the document. We will use a CSS media query to set the styles on different sizes of screens.
  • JavaScript Code: Now we want to display a random birthday message to someone and this can be done through JavaScript. In this section, we store all messages in an array variable and then use an array.length property to check the size of the array. After that use math.random() function to generate a random number to display the random message. 

Example: In this example, we are using the above-explained approach.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Page Title</title>
</head>
 
<body>
    <div class="body-bg"></div>
    <div class="container">
        <div class="container-data">
            <div id="msg"></div>
        </div>
    </div>
</body>
</html>


CSS




.body-bg {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: radial-gradient(#00E7BD, #013A4E);
    transition: all 0.5s;
}
   
.container {
    width: 80%;
    height: 80%;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: linear-gradient(#00E7BD, #013A4E);
    box-shadow: 0 0 20px 2px #013A4E;
}
   
.container-data {
    padding: 24px;
    position: absolute;
    top: 50%;
    left: 50%;
    width: 80%;
    transform: translateX(-50%) translateY(-50%);
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 3vw;
}
   
@media only screen and (max-width: 763px) {
    .container-data {
        padding: 24px;
        position: absolute;
        top: 50%;
        left: 50%;
        width: 80%;
        transform: translateX(-50%) translateY(-50%);
        text-align: center;
        color: #fff;
        font-family: Arial;
        font-size: 6vw;
    }
}
   
@media only screen and (max-height: 423px) {
    .container-data {
        padding: 24px;
        position: absolute;
        top: 50%;
        left: 50%;
        width: 80%;
        transform: translateX(-50%) translateY(-50%);
        text-align: center;
        color: #fff;
        font-family: Arial;
        font-size: 4vw;
    }
}


Javascript




var messages = ["Happy birthday to GFG",
    "Happy birthday to neveropen",
    "Happy birthday to Geeks"];
 
var i = messages.length;
var s = Math.floor(Math.random() * i);
 
document.getElementById("msg")
    .innerHTML = '" ' + messages[s] + ' "';


Output: Click here to check the output live.

 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments