How To Use Javascript fadeIn? How To Make fadeIn & fadeOut?

fadeIn” in JavaScript refers to gradually making an element visible by smoothly increasing its opacity from transparent to opaque. Though not a built-in function, you can achieve this effect by adjusting an element’s CSS properties over time using JavaScript.

The article explains how to create a fadeIn effect step by step, including setting up the HTML structure, styling the element, and adding JavaScript logic.

It also introduces a fadeOut effect and provides an easy-to-follow guide for implementing both transitions, allowing you to enhance your website with subtle and engaging animations.

What is fadeIn in JavaScript?

In JavaScript, there isn’t a ready-made tool called “fadeIn” that makes things appear slowly. But developers often use the idea of “fadeIn” when they want something on a website to gradually show up.

It’s like when you slowly turn up the brightness of a light. Imagine you have a picture on a webpage, and you want it to appear smoothly instead of just popping up suddenly.

You can use JavaScript to make it slowly become more visible, like how a fog fades away to reveal something.

This is done by slowly changing the see-through level of the picture, so it goes from being almost invisible to fully clear. That’s the “fadeIn” effect in a nutshell!

How to make pure Javascript fadein?

Here’s a step-by-step breakdown of the provided example program that demonstrates how to create a fadeIn effect in JavaScript:

Step 1: HTML Document Structure

Start by setting up the basic structure of an HTML document. This includes declaring the document type, specifying the language, and setting the viewport for responsive design. Also, give your webpage a title that will appear in the browser tab.

   <!DOCTYPE html>
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>FadeIn Example</title>
   </head>
   <body>
       <!-- Content of your webpage will go here -->
   </body>
   </html>

Step 2: Styling with CSS

Inside the <head> section, add a <style> block to define the styling for the element you want to apply the fadeIn effect to. In this case, we’re styling a <div> element with the ID fadeElement.

   <style>
       /* Styling for the element */
       #fadeElement {
           width: 200px;
           height: 200px;
           background-color: #3498db;
           color: white;
           text-align: center;
           line-height: 200px;
           font-size: 20px;
           opacity: 0; /* Initial opacity set to 0 for fade effect */
       }
   </style>

Step 3: Content and Elements

In the <body> section, you can add the content of your webpage. For this example, we’ve included a “Fade In” button and a <div> element with the ID fadeElement.

   <body>
       <!-- Your website content -->
       <button id="fadeInButton">Fade In</button>
       <div id="fadeElement">FadeIn Example</div>
   </body>

Step 4: JavaScript Functionality

To create the fadeIn effect, you’ll use JavaScript. Inside the <body> section, just before the closing </body> tag, add a <script> block to include your JavaScript code.

   <script>
       const fadeInButton = document.getElementById('fadeInButton');
       const fadeElement = document.getElementById('fadeElement');

       fadeInButton.addEventListener('click', () => {
           fadeIn();
       });

       function fadeIn() {
           let opacity = 0;
           const interval = setInterval(() => {
               if (opacity < 1) {
                   opacity += 0.05; // Increase opacity by 0.05 in each step
                   fadeElement.style.opacity = opacity; // Apply new opacity to the element
               } else {
                   clearInterval(interval); // Stop the interval when opacity reaches 1
               }
           }, 50); // Run the interval every 50 milliseconds
       }
   </script>

Step 5: JavaScript Logic:

Within the <script> block, the JavaScript code listens for a click event on the “Fade In” button. When clicked, the fadeIn() function is triggered. This function gradually increases the opacity of the fadeElement by a small amount in each step, creating the fadeIn effect.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FadeIn Example</title>
<style>
    /* Styling for the element */
    #fadeElement {
        width: 200px;
        height: 200px;
        background-color: #3498db;
        color: white;
        text-align: center;
        line-height: 200px;
        font-size: 20px;
        opacity: 0;
    }
</style>
</head>
<body>
    <!-- Your website content -->
    <button id="fadeInButton">Fade In</button>
    <div id="fadeElement">FadeIn Example</div>
    
    <script>
        const fadeInButton = document.getElementById('fadeInButton');
        const fadeElement = document.getElementById('fadeElement');
        
        fadeInButton.addEventListener('click', () => {
            fadeIn();
        });
        
        function fadeIn() {
            let opacity = 0;
            const interval = setInterval(() => {
                if (opacity < 1) {
                    opacity += 0.05;
                    fadeElement.style.opacity = opacity;
                } else {
                    clearInterval(interval);
                }
            }, 50);
        }
    </script>
</body>
</html>

Result:

How to create fadeIn and fadeout in JavaScript?

Here’s a step-by-step breakdown of the provided example program that demonstrates how to create fadeIn and fadeOut effects in JavaScript:

  1. HTML Document Structure:
    Start by setting up the basic structure of an HTML document. Declare the document type, specify the language, and set the viewport for responsive design. Also, give your webpage a title that will appear in the browser tab.
   <!DOCTYPE html>
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>FadeIn and FadeOut Example</title>
   </head>
   <body>
       <!-- Content of your webpage will go here -->
   </body>
   </html>

  1. Styling with CSS:
    Inside the <head> section, add a <style> block to define the styling for the element you want to apply the fadeIn and fadeOut effects too. In this case, we’re styling a <div> element with the ID fadeElement.
   <style>
       /* Styling for the element */
       #fadeElement {
           width: 200px;
           height: 200px;
           background-color: #3498db;
           color: white;
           text-align: center;
           line-height: 200px;
           font-size: 20px;
           display: none; /* Initially hidden */
       }
   </style>

  1. Content and Elements:
    In the <body> section, add the content of your webpage. For this example, we’ve included two buttons: “Fade In” and “Fade Out,” and a <div> element with the ID fadeElement.
   <body>
       <!-- Your website content -->
       <button id="fadeInButton">Fade In</button>
       <button id="fadeOutButton">Fade Out</button>
       <div id="fadeElement">FadeIn and FadeOut Example</div>
   </body>

  1. JavaScript Functionality:
    To create the fadeIn and fadeOut effects, you’ll use JavaScript. Inside the <body> section, just before the closing </body> tag, add a <script> block to include your JavaScript code.
   <script>
       const fadeInButton = document.getElementById('fadeInButton');
       const fadeOutButton = document.getElementById('fadeOutButton');
       const fadeElement = document.getElementById('fadeElement');

       // Event listener for the "Fade In" button
       fadeInButton.addEventListener('click', () => {
           fadeElement.style.display = 'block'; // Show the element
           fadeIn(); // Call the fadeIn function
       });

       // Event listener for the "Fade Out" button
       fadeOutButton.addEventListener('click', () => {
           fadeOut(); // Call the fadeOut function
       });

       // Function to implement the fadeIn effect
       function fadeIn() {
           let opacity = 0;
           const interval = setInterval(() => {
               if (opacity < 1) {
                   opacity += 0.05; // Increase opacity by 0.05 in each step
                   fadeElement.style.opacity = opacity; // Apply new opacity to the element
               } else {
                   clearInterval(interval); // Stop the interval when opacity reaches 1
               }
           }, 50); // Run the interval every 50 milliseconds
       }

       // Function to implement the fadeOut effect
       function fadeOut() {
           let opacity = 1;
           const interval = setInterval(() => {
               if (opacity > 0) {
                   opacity -= 0.05; // Decrease opacity by 0.05 in each step
                   fadeElement.style.opacity = opacity; // Apply new opacity to the element
               } else {
                   fadeElement.style.display = 'none'; // Hide the element
                   clearInterval(interval); // Stop the interval when opacity reaches 0
               }
           }, 50); // Run the interval every 50 milliseconds
       }
   </script>

  1. JavaScript Logic:
    Within the <script> block, the JavaScript code listens for click events on both the “Fade In” and “Fade Out” buttons. When clicked, the corresponding fadeIn() or fadeOut() function is triggered. These functions gradually adjust the opacity of the fadeElement, creating the desired fadeIn and fadeOut effects.
  2. Testing:
    Save the HTML file and open it in a web browser. You’ll see two buttons: “Fade In” and “Fade Out,” and a <div> element with the text “FadeIn and FadeOut Example.” Clicking the buttons will trigger the fadeIn and fadeOut effects on the element, making it smoothly appear and disappear.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FadeIn and FadeOut Example</title>
<style>
    /* Styling for the element */
    #fadeElement {
        width: 200px;
        height: 200px;
        background-color: #3498db;
        color: white;
        text-align: center;
        line-height: 200px;
        font-size: 20px;
        display: none;
    }
</style>
</head>
<body>
    <!-- Your website content -->
    <button id="fadeInButton">Fade In</button>
    <button id="fadeOutButton">Fade Out</button>
    <div id="fadeElement">FadeIn and FadeOut Example</div>
    
    <script>
        const fadeInButton = document.getElementById('fadeInButton');
        const fadeOutButton = document.getElementById('fadeOutButton');
        const fadeElement = document.getElementById('fadeElement');
        
        fadeInButton.addEventListener('click', () => {
            fadeElement.style.display = 'block';
            fadeIn();
        });
        
        fadeOutButton.addEventListener('click', () => {
            fadeOut();
        });
        
        function fadeIn() {
            let opacity = 0;
            const interval = setInterval(() => {
                if (opacity < 1) {
                    opacity += 0.05;
                    fadeElement.style.opacity = opacity;
                } else {
                    clearInterval(interval);
                }
            }, 50);
        }
        
        function fadeOut() {
            let opacity = 1;
            const interval = setInterval(() => {
                if (opacity > 0) {
                    opacity -= 0.05;
                    fadeElement.style.opacity = opacity;
                } else {
                    fadeElement.style.display = 'none';
                    clearInterval(interval);
                }
            }, 50);
        }
    </script>
</body>
</html>

Result:

I think we already covered everything we need to know about this article trying to convey.

Nevertheless, here are other functions you can learn to enhance your JavaScript skills.

Conclusion

In conclusion, the concept of “fadeIn” in JavaScript refers to an animation or transition effect used to gradually make an element visible by increasing its opacity from fully transparent to fully opaque. While there isn’t a built-in function for fadeIn in JavaScript, it can be achieved by manipulating CSS properties over a specific time interval.

Quick step-by-step summary (click to expand)
  1. What is fadeIn in JavaScript. Read the ‘What is fadeIn in JavaScript?’ section for the details and code.
  2. How to make pure Javascript fadein. Read the ‘How to make pure Javascript fadein?’ section for the details and code.
  3. How to create fadeIn and fadeout in JavaScript. Read the ‘How to create fadeIn and fadeout in JavaScript?’ section for the details and code.
  4. Conclusion. Read the ‘Conclusion’ section for the details and code.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Glay Eliver


Programmer & Technical Writer at PIES IT Solution

Glay Eliver is a programmer and writer at PIES IT Solution, author of over 600 tutorials at itsourcecode.com. Specializes in JavaScript tutorials, Microsoft Office how-tos (Excel, Word, PowerPoint), and Python error debugging covering ImportError, TypeError, AttributeError, ModuleNotFoundError, and JavaScript ReferenceError. Authored several of the site’s highest-traffic Excel and MS Office reference articles.

Expertise: JavaScript · MS Excel · MS Word · MS PowerPoint · Python · Python ImportError · Python TypeError · Python AttributeError · ModuleNotFoundError · JavaScript ReferenceError · Pygame
 · View all posts by Glay Eliver →

Leave a Comment