Is it possible to use CSS to swap out main menu items for submenu items?

I have created a basic vertical menu that looks like this:

Home  Information  Info

I am wondering if it is possible to replace a list item with its own sublist when the item is active. Specifically, I want to change Home to Sub1 and Sub2 when the item is active in order to update the navigation to look like this:

Sub1 Sub2 | Information  Info

Is there a way to achieve this using CSS?


Code Snippet

<ul class="nav">
    <li><a href="#">Home</a></li>

        <ul class="subnav">
            <li><a href="#">Home Sub 1</a></li>
            <li><a href="#">Home Sub 2</a></li>
        </ul>

    <li><a href="#">Informations</a></li>
    <li><a href="#">Contact</a></li>
</ul> 

CSS Styling

ul.nav {
  float:right;

}

ul.nav > li {
  border:1px solid #333;
  display:block;
  float: left;
  line-height:38px;
  margin:0;
  padding:0;
  position:relative;
  padding:20px
}


ul.subnav {
    display:none;
}

Fiddle Link

Answer №1

To start, ensure that the .subnav element is nested inside a li element, like so:

<ul class="nav">
    <li class="with-subnav"><a href="#">Home</a>
        <ul class="subnav">
            <li><a href="#">Home Sub 1</a></li>
            <li><a href="#">Home Sub 2</a></li>
        </ul>
    </li>
    <li><a href="#">Informations</a></li>
    <li><a href="#">Contact</a></li>
</ul>

In your CSS stylesheet, include the following code:

ul.nav > li.with-subnav:hover > a{
    display:none;
}

ul.nav li:hover ul.subnav{
    display:block;
}

View DEMO here

Additional Update:

If you prefer horizontal alignment of elements, you can add this:

ul.subnav li{
    float: left;
}

View DEMO2 here

FINAL FIX:

The ultimate solution to address the issue was combining both classes (active and with-subnav). See the fix in action DEMO link :)

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

Issue with splitting a JavaScript function in JQuery within the Wordpress platform can cause errors

My split function seems to be causing an error, as shown in this console error image. There is a hidden input field within the PHP file: <input class="file-id" name="_file-id" type="hidden" value="<?php echo esc_attr($file_ids); ?>" /> I hav ...

Is it possible to validate link clicks and integrate PHP within JavaScript functions?

Before anyone mentions security concerns, please refrain. Imagine I have an index.php. This page contains several links to 'home.php', each with different data stored in the href attributes. For example: Link 1: 'home.php?data=1&count ...

Tips for utilizing the React Page Scroller with a webpage exceeding 100vh in size

I'm currently facing an issue with React Page Scroller. While most components I'm using are set to take up 100vh and work smoothly with React Page Scroller, the problem arises with the footer, which exceeds the 100vh limit. I attempted to place t ...

Modify the icon in the header of MaterializeCSS Collapsible when it is opened

I'm struggling to figure out how to change the icon of a toggled collapsible element. I have been reviewing their documentation but am having trouble making it work as intended. $('.collaps_roles_permission').collapsible({ accordion: tr ...

Efficiently loading Google Visualizations for core charts by utilizing AJAX within jQueryUI tabs

Loading Google Visualizations via AJAX in jQueryUI tabs After encountering a similar issue, I discovered a solution provided in the link above that partially resolved my problem. I am trying to use Google Visualization core charts within jQueryUI tabs wit ...

Has the default underline in Bootstrap 5 been altered?

When working with Bootstrap 4, I noticed that it sets the default text-decoration to be none. However, in Bootstrap 5, just adding a regular anchor tag results in both blue text and an underline being displayed. I was trying to achieve only showing the u ...

CSS error with contrasting colors, the reason behind it is unknown

Here is the code snippet I am working on: I have set the background color to #f1f5ff and the font color to #181818. Despite thinking that the contrast is good, I am still encountering an error where the text is not visible. I have tried changing 'c ...

Material-UI's JSS does not adhere to the justify content property

Having trouble aligning elements in my React app using material-ui 1.0 and its JSS solutions. The code I've written below should center justify, but it's not working no matter how I configure it. I suspect there might be a naming issue since mat ...

Customizing a material-ui theme with withStyles() in conjunction with withTheme()

After developing a series of reusable components, I started styling them by utilizing classes prop to override the MUI classnames. To streamline the process and prevent repetition in more intricate components, I consolidated common styles into a theme. Eac ...

I have successfully implemented the show animation for the Bootstrap dropdown menu, but now I'm struggling to find a way

I have successfully implemented a CSS animation dropdown menu as shown below: /* Custom CSS for animation */ .dropdown .dropdown-menu { animation-name: example; animation-duration: 1s; } @keyframes example { from { transform: scale(0); o ...

Problem with Internet Explorer

There seems to be an issue with the center content on this website. In Internet Explorer, a few letters are getting cut off by the sidebar. I've tried adding some extra margin to the right, but it didn't solve the problem. It's happening on ...

What is the best way to place an anchor so that it is perfectly aligned between a static header and footer?

I am looking to create a website where the content is placed below a fixed header and footer. Navigation is to be done through anchors, with only the active anchor being displayed while the rest remains hidden by the header and footer (similar to a window) ...

Mediawiki functionality experiencing issues within iframe display

Currently, I am working with MediaWiki built in PHP. Due to certain reasons, I have to embed this application within an iframe. All functionalities are running smoothly except for the Edit link for pages. The issue arises when I try to access the second li ...

Using jQuery UI to style div elements with dynamic titles

Is it possible to style a standard div container using the same styling as jQuery dialogs? I am looking to create a div panel with text content but with the title bar styling similar to that of jQuery UI dialog boxes. Appreciate your assistance in advance ...

Implementing alternating row shades using Bootstrap

I am attempting to apply shading to every other row in a table. Here is the code snippet: dt:nth-child(4n+1) ,dt:nth-child(4n+1) +dd{ background-color:silver; } <div class="col-sm-2 tabularData"> <dl class="dl-horizontal"> <dt> ...

Add a font awesome icon dynamically using JavaScript

I'm currently working on a navigation bar in JavaScript that displays the titles of various links. However, I would like to enhance it by adding small icons next to the text. Can anyone guide me on how to integrate Font Awesome icons into my JavaScrip ...

The span's presence causes the neighboring span to shift downwards

HTML <span class="symbol">$</span> <span class="value">400</span> This code snippet displays the symbols "$" and "400" at the same level in the document. However, if we introduce some styling using CSS: .symbol { font-size: 2e ...

Incorporating JavaScript to dynamically load an HTML page into an iframe

I am attempting to have each option load a unique page within a frame <!DOCTYPE html> <html> <head> <script> function selectCountry() { var mylist=document.getElementById("country"); document.getElementById("frame").src=mylist.opti ...

Content of reusable angular form is not visible on the homepage

I am currently working on creating a reusable Angular form within an Ionic application. Despite following various tutorials and Stack Overflow posts, I have resolved all errors but unfortunately, the content does not display on the parent page. Here is my ...

The placement of my footer is off and not aligned with the bottom of the page

I am struggling to make my footer align with the bottom of the page rather than sticking to the bottom of the browser window and moving along with the scroll. How can I fix this issue? The goal is to have the footer remain out of sight at the bottom of th ...