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

Adjust the size of icon passed as props in React

Below is the component I am working on: import ListItemButton from '@mui/material/ListItemButton'; import ListItemIcon from '@mui/material/ListItemIcon'; import Tooltip from '@mui/material/Tooltip'; const MenuItem = ({data, o ...

Is it possible to import a component from a production build of create-react-app?

Has anyone tried importing a component from a production build of create-react-app? Situation: I have one CRA project that has been built for production. Inside this project, there is a component named ExampleButton. Now, I am working on a second CRA pro ...

Choose a specific <div> element from an external page using Ajax/PHP

I'm encountering a small issue. I am currently utilizing Ajax to dynamically load content from another page into a <div> element after detecting a change in a <select>. However, my specific requirement is to only load a particular <div& ...

Download personalized HTML design

Situation When exporting to HTML, the resulting code appears as follows: <html> <head> <title></title> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> <style type="text/css" ...

Adding margin padding to a Material UI Navbar: Step-by-step guide

Hey there, I've added buttons to my Navbar from Mui. However, I'm running into an issue where the margin and padding won't change no matter what I do. I'm trying to create some space between them. Please see the code below: import { use ...

JavaScript strangeness

I am currently working on a dynamic page loaded with ajax. Here is the code that the ' $.get' jQuery function calls (located in an external HTML page): <script type="text/javascript"> $(function() { $('button').sb_animateBut ...

Best practice for retrieving the $scope object inside the ng-view directive

Check out my plunk. Incorporating ngRoute into my project. I want to increase a value using ng-click, and upon clicking "Show total", the ng-view template should display the updated value. However, it seems like the scope is not being accessed as expecte ...

Expanding the padding of the selected item to create more breathing room

Upon examining the following: https://jsfiddle.net/h8kmove5/27/ Let's say you select 9 at the bottom. The display shows 8 9 10. My intention is to create some additional space on either side of 9, or any other active item for that matter. This way, ...

Adjust the background color of the date and time selection tool

Trying to customize the background of a datetime picker created using jquery.pttimeselect.js. Despite linking the correct css file (jquery.pttimeselect.css), I am unable to change the background color. Any suggestions on how to successfully modify the ba ...

Issue encountered: difficulty with vertical alignment in table cell containing both text input and image

I am facing an issue with aligning two items (an input text and an image) within a cell of a table. Despite trying various methods like using vertical-align:middle, margin top, padding, and setting height to 100%, I haven't been able to achieve the de ...

Using Typeof in an IF statement is successful, but attempting to use ELSE with the same

My goal is to create a JavaScript variable called str. In case the ID idofnet doesn't exist, I would like to prompt the user for a value to assign to str. However, if idofnet does exist, then I want to retrieve its value and assign it to str. This is ...

Is the presence of the selenium webdriver being registered?

Is there a method to detect if a website can tell I am using a bot while running Selenium WebDriver in Python or Puppeteer in JavaScript? Are there any resources that indicate whether a bot test would be failed, such as Cloudflare or Captcha? Appreciate a ...

Creating a tree-view in Vue.js that includes clickable components which trigger a Vue.js modal to open up

I have a unique requirement to implement a tree-view feature in Vue-JS for displaying JSON data. However, I need to enhance this by triggering a VueJS modal when any of the data fields in the JSON view are clicked. I have explored various npm modules that ...

GSAP also brings scale transformations to life through its animation capabilities

I have an SVG graphic and I'm looking to make four elements appear in place. I've been using GSAP, but the elements seem to be flying into place rather than scaling up. Here's the code snippet I've been using: gsap.fromTo( ...

Encountering errors when trying to render views in Node/Express is a common

I have set up my nodejs/express application like this; app.set('views', __dirname + './views'); app.set('view engine', 'ejs'); After that, I created a route as shown below app.get('/page/MyPage', functio ...

Ways to identify if the text entered in a text area is right-to-left justified

Within a textarea, users can input text in English (or any other left-to-right language) or in a right-to-left language. If the user types in a right-to-left language, they must press Right-shift + ctrl to align the text to the right. However, on modern O ...

Enhance mix-blend mode or filtering effects in SCSS using an SVG component in VueJS

I am currently developing a fixed navbar at the top of a webpage with several SVGs inside anchor tags. I want the SVGs to appear black when displayed over lighter colored divs (other Vue components) and white when displayed over darker colored ones. The ba ...

Display a collection of pictures from a directory on a website using JavaScript

I am having trouble displaying a collection of images from a specific folder using JavaScript/jQuery. Below is the code snippet I am working with: $(document).ready(function(){ var dir = "images/"; // specified folder location var fileextension ...

What could be the reason for the css problems being caused by my flash banner?

My flash banner ad is being displayed on a website as an advertisement at the bottom of the page. Whenever the banner ad is shown, the scroll bar disappears. The following CSS code appears when the flash banner is active: body {margin: 0; padding: 0; over ...

AngularJS mobile navigation menu toggle not functioning properly with the close feature

I am currently developing a simple Single Page Application (SPA) using an HTML template. The template includes a mobile navigation menu, but I am facing issues with it not closing when navigating through routes in AngularJS. Can anyone provide guidance on ...