Displaying main content after loading with loading GIF

I’m trying to display a loading GIF while my web page is loading, and then hide the loading GIF and display the main content once the page has finished loading. I’ve tried using JavaScript to achieve this, but I’m encountering an issue where the main content doesn’t display after the loading GIF disappears. Here’s my code:


window.addEventListener("load", function () {
    var loading = document.getElementById("loading");
    loading.style.display = "none";

    var mainContent = document.querySelector(".l-main");
    mainContent.style.display = "block"; // This line doesn't seem to work


<!DOCTYPE html>
<html lang="en">
    <!-- Head content -->
    <div id="loading">
        <!-- Loading GIF content -->
    <main class="l-main">
        <!-- Main content -->
    <script src="landingpage.js"></script>

I expect the main content to be displayed after the loading GIF disappears, but it doesn’t seem to be working. Can someone please help me identify what I’m doing wrong or suggest a better approach to achieve this? Thank you!


How i hidden the .l-main:

        /* Styles for the loading screen */
        #loading {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgb(0, 0, 0);
            display: flex;
            justify-content: center;
            align-items: center;

        /* Hide the main content initially */
        .l-main {
            display: none;
            display: none;