Issues with the menu pop-up functionality arise when utilizing the CSS class .active

When I have this code, the ".active" function doesn't work when clicked. However, if I change the div class from "top-menu-popup" to "top-menu-popup active" when opening the page, the menu is already activated.

<div class="top-menu-popup">
            <div class="top-menu-arrow" style="margin-right: 17.5px;"></div>
            <div class="top-menu-item home" style="display: block;">Homepage</div>
            <div class="context-menu-divider homepage" style="display: none;"></div>
            <div class="top-menu-item contains-submenu">
                "CARS"
                <div class="top-submenu-popup">
                    <div class="top-menu-item volkswagen">Volkswagen</div>
                    <div class="top-menu-item mercedes">Mercedes</div>
                    <div class="top-menu-item ford">Ford Mustang</div>
                    <div class="top-menu-item ferrari">Ferrari</div>
                    <div class="top-menu-item dodge">Dodge</div>
                    <div class="top-menu-item lincoln">Lincoln</div>
                    <div class="top-menu-item jaguar">Jaguar</div>
                </div>
            </div>
        </div>

.top-menu-popup {
    min-width: 178px;
    position: absolute;
    background-color: white;
    -webkit-border-bottom-left-radius: 7px;
    -moz-border-radius-bottomleft: 7px;
    border-bottom-left-radius: 7px;
    border: 2px solid rgba(0, 0, 0, .3);
    border-top: 0;
    border-right: 0;
    -webkit-background-clip: padding-box;
    background-clip: padding-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    z-index: 500;
    padding: 5px 0;
    min-height: 40px;
    top: 50px;
    right: 0;
    display: none;
}

.top-menu-popup.active {
    display: block;
}

Answer №1

My best guess is that you are referring to the ::active pseudo class:

.top-menu-popup {
    min-width: 178px;
    position: absolute;
    background-color: white;
    -webkit-border-bottom-left-radius: 7px;
    -moz-border-radius-bottomleft: 7px;
    border-bottom-left-radius: 7px;
    border: 2px solid rgba(0, 0, 0, .3);
    border-top: 0;
    border-right: 0;
    -webkit-background-clip: padding-box;
    background-clip: padding-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    z-index: 500;
    padding: 5px 0;
    min-height: 40px;
    top: 50px;
    right: 0;
    display: none;
}

html:active .top-menu-popup {
    display: block;
}
Click Anywhere...
<div class="top-menu-popup">
            <div class="top-menu-arrow" style="margin-right: 17.5px;"></div>
            <div class="top-menu-item home" style="display: block;">Homepage</div>
            <div class="context-menu-divider homepage" style="display: none;"></div>
            <div class="top-menu-item contains-submenu">
                "MASINI"
                <div class="top-submenu-popup">
                    <div class="top-menu-item volkswagen">Volkswagen</div>
                    <div class="top-menu-item mercedes">Mercedes</div>
                    <div class="top-menu-item ford">Ford Mustang</div>
                    <div class="top-menu-item ferrari">Ferrari</div>
                    <div class="top-menu-item dodge">Dodge</div>
                    <div class="top-menu-item lincoln">Lincoln</div>
                    <div class="top-menu-item jaguar">Jaguar</div>
                </div>
            </div>
        </div>

Answer №2

This code snippet demonstrates how to create a menu structure:

<ul class="horz">
    <li><a href="#">Item 1</a>
        <ul class="vert">
            <li><a href="#">Sub item 11</a></li>
            <li><a href="#">Sub item 12</a></li>
        </ul>
    </li>
    <li><a href="#">Item 2</a>
        <ul class="vert">
            <li><a href="#">Sub item 21</a></li>
            <li><a href="#">Sub item 22</a></li>
        </ul>
    </li>
    <li><a href="#">Item 3</a></li>
    <li><a href="#">Item 4</a></li>
    </ul>

You can style the menu using the following CSS:

ul { margin:0; padding:0 }
.horz > li { display:inline-block; position:relative; padding:0 20px; }
.vert { margin-left:1rem; }
.vert li { line-height:100%; }

ul .vert {
    position: absolute;
    display: none;
    list-style: none;
    width:100%;
}

ul .vert li {
    padding:5px 0;
}

ul > li.active > .vert,
ul > li:hover > .vert {
    display: block; 
}

a:hover { color:red; }

The content will be displayed when hovering over a li element or

div.top-menu-item.contains-submenu
. The menu can also have a permanent submenu by adding the .active class to a specific li.

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

"Adding an Image to Another Image in HTML or JavaScript: A Step-by-Step

Hello there! I'm currently working on creating a status bar where I can add an image after another image. Let me explain my idea clearly. So, I have two images in GIF format: one is white and 10x10px, and the other one is black and also 10x10px. On ...

How to access the current class in Sass without referencing the parent class

.site-header .basket position: relative &-container width: 100% padding: 0 18px $basket: & &.opened #{$basket}-link border: 1px solid #e5e5e5 ...

The differences between using the opacity attribute in HTML and the opacity property

There are two distinct methods for adjusting opacity in HTML: <div opacity="0.5"></div> and <div style="opacity: 0.5;"></div> I am familiar with setting these values in JavaScript as well: div.setAttribute("opacity", 0.5); and ...

Website Navigation: A guide to organizing columns and rows for optimal user experience

Just reaching out with a question regarding my coding process. Currently, I am in the midst of translating the website's navigation design into code. The attached image showcases the navigation layout consisting of 10 rows and 8 columns. Below is a s ...

What is the best way to remove table cells from a TableBody?

Currently, I am in the process of designing a table to display data retrieved from my backend server. To accomplish this, I have opted to utilize the Table component provided by Material UI. The data I retrieve may either be empty or contain an object. My ...

How can I prevent a span in CSS from stretching its parent element?

Utilizing the twitter bootstrap thumbnails successfully, we have implemented a description feature inspired by the bootstrap documentation. The distinction lies in our decision not to specify a size (e.g., .span4) for our thumbnails; instead, the content d ...

Problem with Bootstrap container-fluid overlapping with floated element

I am struggling with the layout of my card body, which includes a floating logo and rows enclosed in a container-fluid. While everything looks great in Chrome, I am facing alignment issues in Edge and Firefox. I have considered using absolute positioning ...

What is the best way to prevent 2 divs from overlapping on a webpage?

I'm having trouble getting the smaller div to display right below the larger red div. I tried using the display block css property, but it doesn't seem to be working as expected. Unfortunately, I can't provide an image here, but you can vie ...

What are the key differences between using role and class selectors in HTML 5 and CSS3 to create two sidebars with three

My decision to eliminate all ids from my CSS and transition to HTML 5 has led me to question the use of roles in markup. In the past, IDs were used to style sidebars individually within a single selector for both. Here is an example: <div id="sidebar" ...

When the jQuery Div is moved to the right, it gradually shrinks in size, yet remains unchanged when

I have been making updates to this page, which you can view here. When you select "Let's Get Started" and then proceed with the right arrows, the divs smoothly move to the left without shrinking. However, when clicking on the back or left arrows, the ...

Ways to modify div content depending on the size of the screen

When the screen size becomes small, my goal is to modify the content within the row divs of the indices_container. Currently, all screen sizes display the index name, price, 1-day change, and year-to-date (YTD) change for each row. I only want to show the ...

Issue with the radio button functionality: it can be clicked on but does not change the background

I'm having trouble with creating inline radio buttons using bootstrap. Neither of the options are being selected. I've attempted various solutions such as adjusting the attributes for, name, and trying different ways to call the bootstrap functi ...

How to add a line break within the :after pseudo-element using CSS

I've been struggling to split text inside an :after pseudo-element of a label that receives its content from a data- attribute. I've attempted using word-break, as well as playing around with width, max-width, and position:relative, but nothing s ...

Having trouble with the flexbox flex-direction: column property not functioning properly with the textarea element in Firefox?

Inconsistencies between Chrome and Firefox have been noticed in the following example. While the footer height is set to height:40px and appears fine in Chrome, it seems smaller in Firefox, around 35px. Any specific reasons for this difference? Link to t ...

Having trouble with Flowbite dropdown functionality in Next.js 12.* with Tailwind CSS

Hello, I am a newcomer to Next.js and currently working on a project using Tailwind/Flowbite. I copied and pasted the Flowbite search input with dropdown code from , but unfortunately, the dropdown is not functioning as expected. I have followed the docum ...

Why is it that aligning items in a row doesn't seem to function properly when I apply a custom class to a container-fluid in Bootstrap 4?

Recently, I started delving into the world of front end development. After mastering the basics of HTML5 and CSS3, I ventured into Bootstrap 4. My current project involves creating a replica of the Facebook login page. To achieve a full-width design, I uti ...

Typewriter Effect: The caret is advancing too quickly

I am trying to achieve a typewriter effect on my text, but the blinking cursor is extending further than the actual text. See Image for Problem Here is the code I am using: HTML: <div class="title"> <h1>Hi, I'm ConnerDE< ...

Implementing a Custom CSS Theme in JavaFX

Hey everyone, I stumbled upon this amazing JavaFX theme called Flatter on . I've been attempting to implement it for a couple of hours now, but I'm struggling with the process. Can someone provide me with a detailed guide on how to activate thi ...

Attempting to replicate the flipping motion of a card by applying the transform property to rotate a div element, ultimately revealing a different

I am attempting to create a series of divs that simulate the flipping action of a notecard. Unfortunately, when I hover over the div, it turns white because I have set the display property to none. I am unsure how to make the other div (.back) visible. Y ...

Modify the width measurement from pixels to percentage using JavaScript

Looking for some help with a content slider on my website at this link: . I want to make it responsive so that it adjusts to 100% width. I managed to make it responsive by tweaking some code in the developer tools, but now I'm stuck. The bottom two p ...