Random Joke Generator In HTML, CSS & JavaScript

Create Random Joke Generator In HTML, CSS & JavaScript

Create Random Joke Generator In HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Random Quote Generator which will generate a joke randomly which will be funny for the users. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Random Joke Generator Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Random Joke Generator Project.

Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Random Joke Generator Project.

At last, we will use JS (JavaScript) which will add logic to make the Random Joke Generator Project responsive from the user end.

In this blog post, we will discuss Creating a Random Joke Generator Using HTML, CSS & JavaScript with complete source code so you can just copy and paste them into your own project. Happy exploring and learning !!

I hope you have got an idea about the project.

HTML Code for Random Joke Generator

First, we’ll start with creating the structure of the Random Joke Generator project for that as you can see in the above code we have used all the necessary elements & attributes to set up the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Random Joke Generator</title>
    <!--Google Font-->
    <link href="https://fonts.googleapis.com/css2?family=Rubik:wght@400;600&display=swap" rel="stylesheet">
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="wrapper">
        <span>😂</span>
        <p id="joke"></p>
        <button id="btn">Get Random Joke</button>
    </div>

    <!-- Script -->
    <script src="script.js"></script>
</body>
</html>

Create GitHub Username Search Using JavaScript

CSS Code for Random Joke Generator

Second, comes the CSS code which we have styled for the structure we have padded as well as aligned the Random Joke Generator project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now, let’s code the JavaScript part to make it responsive.

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: "Rubik",sans-serif;
}
body{
    background-color: #fab22e;
}
.wrapper{
    width: 80vmin;
    padding: 50px 40px;
    background-color: #15161a;
    position: absolute;
    transform: translate(-50%, -50%);
    top: 50%;
    left: 50%;
    border-radius: 5px;
    box-shadow: 20px 20px 40px rgba(97,63,0,0.4);
}
span{
    display: block;
    text-align: center;
    font-size: 100px;
}
p{
    font-size: 16px;
    color: #ffffff;
    font-weight: 400;
    text-align: center;
    word-wrap: break-word;
    line-height: 35px;
    margin: 30px 0;
    opacity: 0;
}
.fade{
    opacity: 1;
    transition: opacity 1.5s;
}
button{
    display: block;
    background-color: #fab22e;
    border: none;
    padding: 5px;
    font-size: 18px;
    color: #171721;
    font-weight: 600;
    padding: 12px 25px;
    margin: 0 auto;
    border-radius: 5px;
    cursor: pointer;
    outline: none;
}

Automatic Multiple Image Slider in HTML CSS

JavaScript Code for Random Joke Generator

Last stage of the project the JavaScript which we added the logic and coded as per the requirement with some conditions. Let us see the Final Output of the project Random Joke Generator using HTML, CSS & JavaScript.

const jokeContainer = document.getElementById("joke");
const btn = document.getElementById("btn");
const url = "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&type=single";

let getJoke = () => {
    jokeContainer.classList.remove("fade");
    fetch(url)
    .then(data => data.json())
    .then(item =>{
        jokeContainer.textContent = `${item.joke}`;
        jokeContainer.classList.add("fade");
    });
}
btn.addEventListener("click",getJoke);
getJoke();

Final Output


We have successfully created our Random Joke Generator using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Thank You For Visiting!!!

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply