Why is my element still occupying space even though it has been hidden with a display of none?

Currently, I'm working on a dropdown navigation menu where one of the list items causes space issues when the display property is set to none. Specifically, the 'Book A Table' item isn't meant to be visible in the center div when the screen width is above 1100px. However, it should appear in the dropdown menu when the screen width is below 1100px. Upon inspecting the element in Chrome, there's a green line indicating that the item still occupies space even when hidden.

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Dropdown Menu</title>
        <link href="styles.css" rel="stylesheet">
        <link href='https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a58554253595554497a08140b140e">[email protected]</a>/css/boxicons.min.css' rel='stylesheet'>
    </head>

    <body>
        
        <header class="header">

            <div class="toggle-button" onclick="myFunction();">
                <span class="bar1"></span>
                <span class="bar2"></span>
                <span class="bar3"></span>   
            </div>

            <div class="logo">
                <a href="index.html"><img src="darklogo.png" alt=""></a>
            </div>


            <nav id="nav">
                <ul>
                    <li><a href="#">Home</a></li>
                    <li><a href="#">Menu</a></li>
                    <li><a href="#">About Us</a></li>
                    <li><a href="#">Contact Us</a></li>
                    <li><a href="#" class="bookatabledd">Book A Table</a></li>
                </ul>
            </nav>

            <div class="bookatable">Book A Table &nbsp ></div>


        </header>

        <script src="main.js"></script>

    </body>

</html>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Arial, Helvetica, sans-serif;
}


header {
    display: flex;
    text-align: center;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 5px 60px;
    background-color: #212223;
    position: relative;
}


.logo img {
    width: 190px;
}

.bookatabledd {
    display: none;
}

nav ul li {
    display: inline-block;
    padding: 5px 25px;
}

nav ul li a{
    display: inline-block;
    font-weight: bold;
    font-size: 20px;
    list-style-type: none;
}

nav ul li a:hover {
    color: rgb(174, 17, 174);
}

nav ul li a{
    text-decoration: none;
    display: inline-block;
    color: white;
    font-weight: 600;
}


.toggle-button{
    width: 35px;
    position: absolute;
    right: 40px;
    top: 45px;
    display: none;
    cursor: pointer;
}

.toggle-button span {
    display: inline-block;
    width: 100%;
    height: 2px;
    background-color: white;
    float: left;
    margin-bottom: 8px;
}

.bookatable {
    color: #fff;
    background-color: #555556;
    border-radius: 6px;
    text-transform: uppercase;
    font-weight: 600;
    font-size: 18px;
    line-height: 1;
    text-align: center;
    padding: 9px 6px 8px 8px;
    transition-duration: 0.6s;
    transition-timing-function: ease-in-out;
    letter-spacing: -.3px;
    cursor: pointer;
}

.bookatable:hover {
    cursor: pointer;
    background-color: #909092;
    color: white;
}





/* ---- BREAKPOINTS ---- */

@media (max-width: 1100px) {

    header {
        display: flex;
        flex-direction: column;
        text-align: center;
        position: relative;
        justify-content: space-between;
        padding: 5px 20px 5px;
    }

    .logo {
        align-self: flex-start;
    }
    
    .toggle-button{
        display: block;
    }
    
    nav{
        display: none;
        width: 100%;
        border-top: 1px solid white;
        padding-top: 20px 0;
        margin-top: 30px;
    }

    nav ul li{
        padding: 15px 0px;
        width: 100%;
    }

    nav ul {
        text-align: center;
    }

    nav.show{
        display: flex;
        flex-direction: column;
        flex-wrap: nowrap;
    }

    .bookatable {
        display: none;
    }

    .bookatabledd {
        display: block;
    }
}

enter image description here

Answer №1

Your CSS file includes the following code:

nav ul li{
    padding: 15px 0px;
    width: 100%;
}

This means that, even when the child element containing the 'Book a table' item is not displayed, its parent element in the navbar still maintains padding that takes up space.

Below is the corrected code:

li#hidden {
  padding: 0;
}

// Ensure it has padding when page width is below 1100px

@media (max-width: 1100px) {
 // ...
 li#hidden {
    padding: 15px;
  }
}
<nav id="nav">
                <ul>
                    <li><a href="#">Home</a></li>
                    <li><a href="#">Menu</a></li>
                    <li><a href="#">About Us</a></li>
                    <li><a href="#">Contact Us</a></li>
                    <!-- The item below must not have padding -->
                    <li id ="hidden"><a href="#" class="bookatabledd">Book A Table</a></li>
                </ul>
            </nav>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create a JavaScript script within a CSS file

I'm attempting to create this using only CSS: Codepen and I would like to achieve the same effect but solely with CSS. This is what my CSS looks like: .text{ --a: calc(var(--a);*0.82+(1.5-var(--b);)/10); --b: calc(var(--b);+var(--a);); transfor ...

Dynamic value updates using jQuery input type formulas

I need help with a form that has two inputs: The first input allows the user to enter an amount, such as 1000. The second input is read-only and should display the value of the first input plus 1%. For example, if the user types in 1000 in the first fie ...

Strategies for avoiding unused style tags in React components

Expanding beyond React, I'm unsure if React itself is the culprit of this issue. In a React environment with TypeScript, I utilize CSS imports in component files to have specific stylesheets for each component. I assumed these styles would only be ad ...

Using jQuery to trigger an action when a user clicks on a regular audio element

Is there a way to detect a click on the default audio element of a browser using jQuery? I'm having trouble getting it to work in Chrome. $('audio').click(function(){ alert("You have clicked on an audio player"); }); <script ...

SVG set to fill currentColor but does not dynamically change in high contrast mode

I'm currently working with in-line SVGs on my website. Here's an example: svg { fill: currentColor; } <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" height="25px" viewBox="-13 0 120 30" width="74px"> < ...

Should the letter 'p' be inserted into the letter 'a'?

After viewing a video on Bootstrap, I noticed an unusual occurrence where there was a p element nested inside an a element. Is this considered acceptable in terms of HTML semantics? ...

Styling in Svelte/TS does not change when applied through a foreach loop

I've been experimenting with creating a unique "bubble" effect on one of my websites, but I'm facing difficulty changing the styling in a foreach loop. Despite no errors showing up in the console, I'm at a loss as to how to effectively debu ...

In Javascript, merge two objects while maintaining their original references

Here's a basic illustration of my goal: data = {name: 'fred'}; newData = {}; newData.name = data.name; newData.name = 'ted'; console.log(data.name); // I expect this to be ted instead of fred I am wondering if it is feasible i ...

Removing a dynamic component in Angular

Utilizing Angular dynamic components, I have successfully implemented a system to display toaster notifications through the creation of dynamic components. To achieve this, I have utilized the following: - ComponentFactoryResolve - EmbeddedViewRef - Ap ...

How come my links aren't initiating jQuery click events?

Today, I decided to experiment with jQuery and encountered an issue. On my webpage, there are multiple links displayed in the format shown below: <a class="a_link" id="a_id<#>" href="#">Click me</a> The value <#> is a number gener ...

vuejs function for modifying an existing note

Method for editing an existing note with Vue.js in a web application This particular application enables users to perform the following tasks: Create a new note View a list of all created notes Edit an existing note Delete an existing note Prog ...

What could be causing the malfunction in the cloning of the carousel item?

My goal was to create a carousel that displays multiple images in one slide. However, I encountered an issue where once the fourth image is reached, the other three images are forcibly hidden. I want to give credit to the original creator of this code snip ...

Issue with onclick event not being triggered by Javascript function inside constructor

My current challenge involves implementing a function called makeHighlight within the SmartButton constructor. The purpose of this function is to execute whenever I click on the SmartButton object, which is represented by an image element. To achieve thi ...

Caution: It is essential for each child within a list to possess a distinct "key" property - React Native issue alert

Hello everyone, I am currently working on building a list in an app using react-native. I have been following a tutorial video and my code is identical to the instructor's, but I keep encountering an error that says each child in the list needs a key ...

Steps for embedding a font in a .pptx file

While working on creating a .pptx file using ASPOSE.Slides, I encountered some issues with embedding fonts. As an alternative option, I am looking for suggestions on how to embed custom fonts in a .pptx file using Apache POI or other methods. If you have ...

Steps to transforming a standard array into a multidimensional array

I have a JSON array that is generated after submitting a form using the formSerialize function. Here is an example of how the array looks: [ {"name":"client_management-testmonitoring","value":"0"}, {"name":"client_operations-testmonitoring","v ...

Creating synthetic data using the Faker library

I'm currently developing a script that utilizes the faker and JSON-Schema-Faker libraries to generate test data. I am specifically interested in examples involving "schema inheritance" and optional fields. For instance, I have a 'user' obje ...

Why isn't my CSS float behaving as I anticipated?

I'm currently working on a website and facing an issue. The CSS Float property is not behaving as expected. Here is my HTML Code: <div class="slider-menu"> <div class="slider-box"> <img src="agac.jpg"> & ...

How can we leverage JavaScript to create logistic regression models for datasets and derive the beta-coefficients?

Exploring Logistic Regression in JavaScript In my current project, I am looking to fit a multi-variate logistic regression model to a dataset using JavaScript. My main goal is to extract the beta-coefficients for this model. Can anyone provide guidance on ...

The text displayed on the image is experiencing fluctuations

I am experiencing an issue on my website where the post text fluctuates rapidly when the mouse hovers over the image. I have a site where I display the post text on mouseover of the image. Can someone please help me solve this problem? You can test it by ...