Experiencing a problem with my vertical navigation in CSS

I have been attempting to create a vertical navigation menu for tablet and mobile devices. Despite setting the position of the second-level .depth2 ul element to static, it still seems to be floating around as if it were positioned absolutely within the parent elements. The translucent white boxes represent the .depth2 elements. How can I resolve this issue?

https://i.sstatic.net/yDulM.png

 <div class="header_con">
            <span><a href="#"></a></span>
            <p><a href="#"><img src="images/logo.png" alt="logo"></a></p>
            <nav>
                <ul class="depth1">
                    <li><a href="#">코로나-19</a>
                        <ul class="depth2">
                            <li><a href="#">손세정제</a></li>
                            <li><a href="#">소독제</a></li>
                        </ul>
                    </li>
                    <li><a href="#">컬렉션</a>
                        <ul class="depth2">
                            <li><a href="#"><small>new</small>클레멘타인 앤 바질</a></li>
                            <li><a href="#"><small>new</small>베티버 앤 바이올렛</a></li>
                            <li><a href="#">아로마테라피 시너지</a></li>
                            <li><a href="#">허벌리스트</a></li>
                            <li><a href="#">풋 리바이버</a></li>
                            <li><a href="#">인블룸</a></li>
                            <li><a href="#">크리스탈크러쉬</a></li>
                            <li><a href="#">배스타임</a></li>
                            <li><a href="#">선물세트/ 기프트세트</a></li>
                        </ul>
                    </li>
                    <li><a href="#">오일</a>
                        <ul class="depth2">
                            <li><a href="#">페이셜오일</a></li>
                            <li><a href="#">멀티오일</a></li>
                        </ul>
                    </li>
                    .
                    .
                    .
                </ul>
            </nav>
    </div>
</header>

navigation CSS for pc

    header nav { width: 750px;  position: absolute; left: 50%; margin-left: -360px;}
    header ul.depth1>li{float: left; width: 70px; margin: 0 0.2%; font-size: 0.9em; 
      font-weight: bold; text-align: center;}
    header ul.depth2{display: none; position: absolute; z-index: 100; width: 200px; margin-top: 20px;
       background: #fff; text-align: left; padding: 20px; font-weight: normal; font-size: 0.8em;}
    header ul.depth2>li {margin-bottom: 12px;}

navigation CSS for tablet

    header div.header_con nav {/* display: none;*/ position: fixed; top: 0; right: 0; bottom: 0; 
       left: 0; width: 100%; height: 100%; background: #0b223b; z-index: 110; margin-left: 0;}
    header div.header_con nav a{ color: white;}
    header div.header_con nav .depth1{display: block; color: white; height: 100%;}
    header div.header_con nav .depth1 li {float: none ;width: 100%; height: 50px; line-height: 50px; 
       border-bottom: 1px solid #999; }
    header div.header_con nav .depth2 {display: block; position: static; width: 100%; height: auto;
       margin-top: 0; padding: 0; background: rgba(255,255,255,0.4); }
    header div.header_con nav .depth2 li {padding: 0 0 0 20px;}

Answer №1

Make sure to change the CSS property from position:static to position:relative in your tablet stylesheet for

header div.header_con nav .depth2
. It's important to note that both static and absolute positioning can disrupt the layout of elements on a webpage, so use them judiciously.

Additionally, consider removing the height:50px rule from

header div.header_con nav .depth1 li
, as the height of the first level list item should account for the total height of any nested unordered lists within it.

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

Fixed position of the element within a parent stacking context

How can I ensure that the #icon appears below the #dropdown in the following example? https://i.stack.imgur.com/GoBcR.png The position: fixed of #container seems to be creating a new stacking context for its children. Changing the position value fixes th ...

Implementing bootstrap columns while displaying individual components one after the other

Here is the html code I am currently working with: <label>Search:</label> <input type="text" id="list_search" class="form-control col-8"> <button class="btn btn-info col-2">Search</button> It's interesting to note that ...

Tips for applying CSS to background images

Just working with one div here <div id="particles-js" > and in my CSS, I've got this: #particles-js{ width: 100%; height: 100%; background-color: #b61924; background-image: url("../js/image.jpg"); background-size: cover; backg ...

"Improvements required for the search and filter functionality in the JSON

My JSON function retrieves image data and displays it in panels. Here is an example of the code: $.getJSON('iproducts.json',function(products){ var output = ""; $.each(products.appleitems, function(i, product) { output += "<di ...

Centering elements in CSS with a full-width approach

My goal is to centralize all the elements on a webpage so that when the browser size is modified, the content shifts accordingly. Currently, I am using the following CSS code: margin: 0px auto; width: 670px; However, I am facing a challenge in trying to ...

Can a pledge be honored at a precise moment?

I have implemented transitions on my web page. When clicked, everything fades out to an opacity of 0 over a duration of 1 second. Then, a new page is swapped in and everything fades back in to an opacity of 1 over another 1-second duration. The issue aris ...

Can text be formatted differently based on its length and whether it wraps onto multiple lines?

Looking to display book titles on a webpage with varying lengths. Some are short enough to fit in one line, while others are longer and wrap onto two lines. Is it possible to apply different styles (such as color or line-height) for these two cases using ...

Maintaining equal heights for divs in jQuery upon resizing

I have been attempting to dynamically set the height of all my divs to match the highest div upon page load and window resize. Below is the code snippet I am using: // function to equalize column heights function eqColumn(){ if ($(window).width() > ...

Tag editor assessing the input's width for SO

I've been working on a tag editor similar to Stack Overflow's and it's coming along nicely. The only issue I'm facing is calculating the width of the textbox after a tag has been selected by the user. For each tag added, I try to adjus ...

The v-for directive is displaying my list in a single row with multiple columns instead of in a single column with multiple rows

Can someone please assist in rendering my list as shown below: A B C The current rendering looks like this: 1: A 2: B 3: C Below is the code snippet: To-Do List: <input type="text" class = "todo" placeholder = "Next Item" v-on:keyup.enter="add ...

Styling with react-jss based on intricate conditionals

After experimenting with react-jss for a few weeks, I am faced with the challenge of styling components efficiently. With numerous conditionals in these components, I am striving to avoid creating an excess of new components that essentially share the same ...

Creating a Scrollbar within a Bootstrap Dropdown: a Complete Guide

Is there a way to create a scrollbar within a bootstrap 4 dropdown selection? I am facing an issue where the dropdown does not fully load when there are too many files. My solution is to display only 5 files when the dropdown is clicked and include a scrol ...

Improving iframe functionality in AngularJS

I've been experimenting with AngularJS to dynamically update an iframe whenever a query is triggered. It works like this: a query is made, the embed link is fetched from the database, and then it's used in the iframe alongside the title and video ...

BorderWoocommerce

Something odd has appeared around my shopping cart total ("WARENKORB SUMME") today. Could you please take a look at the screenshot below? Any idea how to remove it? This is for a Wordpress/WooCommerce store. I haven't made any recent changes - I su ...

Having trouble with Tailwind not being applied properly when running the `npx build` command which is resulting

I'm encountering an issue with my style.css file while running the 'npm build:css' command in my node/express js project. This is causing the tailwind css to not be applied to my views. Can anyone help me understand why this is happening? np ...

Adding picture to table using JavaScript

I am currently developing a Web application using the Play Framework technology. In one of my scala.html views, I have successfully displayed an image with the following code: <img src="@routes.Assets.at("images/image/1003.png")" width="30" height="30" ...

How to create a clickable link that functions as a file upload button

I am working on a project where I need to create a link that acts as a file input when clicked. Here is the code for the link: <a id="upload-file">Upload your photo</a> Is there a way to make this link function as a browse file input? Any ...

Using Styled Components to Implement Background Images in a React Application

I'm currently attempting to pass a background image using a prop, but I'm encountering an issue where it's indicating that url is undefined. const CardImage = styled.div` height: auto; width: 100%; background-size: c ...

Troubleshooting tips for resolving encoding issues during XML to JSON conversion

I am currently working with an XML file and I need to convert it into JSON format using Node.js and then display it in HTML. Below is the content of the XML file: <?xml version="1.0" encoding="windows-1251" ?> <ROWDATA> &l ...

The responsiveness of Bootstrap 5 footer needs improvement

I am currently developing a website using Bootstrap 5, HTML, and CSS. I've encountered an issue where the columns in the footer don't align properly when the screen size is reduced. Surprisingly, the columns line up correctly on mobile-sized and ...