Could CSS alone achieve this sub menu position?

Sorry for the lack of clarity in my question, I am struggling to come up with a concise description.

I am currently working on creating a drop-down menu using unordered lists, which is structured like this....

<ul id="menu">
    <li>Menu Item</li>
    <li>Menu Item
        <ul class="sub-menu">
            <li>Sub Menu Item</li>        
            <li>Sub Menu Item</li>
        </ul>
    </li>
    <li>Menu Item</li>
</ul>

My concern is whether it is possible to achieve the desired positioning behavior (I believe images would help clarify).

Case 1

The sub-menu should be centered within its parent list item, even if it is wider than the parent.

Case 2

The sub-menu needs to be centered, but there may not be enough space on the left side of the root list, causing it to align with the edge.

Case 3

Similar to Case 2, but this time the issue is with the right edge.

I believe these scenarios can be solved by tackling two challenging CSS problems.

  1. How to center a sub-menu within its container, regardless of size difference.
  2. How to ensure the submenu stays within the boundaries of the root list element.

Due to time constraints, I will implement this functionality in Javascript. However, I am curious about achieving this solely through CSS for future projects.

You can experiment with a JSFiddle here if you have any ideas.

Answer №1

Check out this link for a helpful example

A new approach is to wrap your submenus in a div, allowing you to easily position them using the text-align property.

To customize the appearance of the submenu background, consider removing the gray background and adding a background color only to the ul inside the div.submenu.

The concept involves utilizing a div that spans the width of your main menu to contain the submenu. By manipulating the display inline with the text-align property, you can align the submenu to the left, center, or right within the available space.

Update

Explore this updated option here

This revised solution accommodates a larger number of menu items compared to the previous version. To achieve this, we specify a submenu three times the width of a single menu item (in this case, 300% wide) and center it by applying a negative left margin equivalent to one menu item width (-100%). The list items within the submenu are horizontally centered using a similar method as before.

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

Using pagination with tables in HTML: A beginner's guide

I attempted to implement a paging system within a table following the instructions in this article, but unfortunately, it doesn't seem to be functioning correctly. Expected Output: https://i.sstatic.net/4ODNf.png Current Output: https://i.sstatic. ...

Why does my navbar seem to turn transparent when I hover over the cards?

Throughout this question, I will be using the following Codepen as a reference: https://codepen.io/zarif-codes/pen/OJOBmLV In my project for creating a product landing page (an FCC assignment), I've implemented a fixed navbar. Below is the HTML code ...

Differences in CSS between IE10 and Chrome are causing compatibility issues

Website in question: What is causing the difference in display between IE 10 and other browsers? The website appears to function correctly on Chrome: IE 10: ...

Achieve the ideal text styling with the power of CSS

I am trying to retrieve the names of the VICE PRESIDENT and PRESIDENT using CSS selectors. For the VICE PRESIDENT, we can use the following method: div:contains(VICE PRESIDENT) However, when attempting to select the PRESIDENT using the same method, we ...

What is the best way to create an animation for a dynamic menu background image

While hovering over a list item, I want to create an animation where the background image appears to zoom out. $(function () { var image = $('.menu').find('img').attr('src'); $('.menu ul li').mouseover(fun ...

Tips for aligning text in the middle of a line

I am currently attempting to design a page separator that includes text nestled between two horizontal lines. Despite my efforts, I am struggling to achieve the desired effect of having the text contained within these lines. You can view my progress in JS ...

The background-size property in CSS does not seem to properly display on iPhones

Hello there! I'm facing an issue with my CSS code when it comes to displaying the image on Safari browser. It works perfectly fine on other browsers, but for some reason on Safari, the image doesn't show up correctly. I tried using a media query ...

Ways to modify the appearance of tiptap-vuetify

I have implemented tiptap-vuetify (available at https://github.com/iliyaZelenko/tiptap-vuetify) as my wysiwyg editor. However, I am not satisfied with the default styling. Since it does not accept class or style as props, I am looking for ways to customi ...

What steps should be followed to construct a window identical to the one depicted in the attached image using HTML and CSS?

Check out this link to see the window style I'm trying to recreate using HTML, CSS, and Javascript. No Jquery needed. Thank you in advance. ...

Having issues with the appendTo function in your jQuery script?

I am currently facing an issue with adding a paragraph to an h2 tag using an id selector. Despite my efforts, it seems like the solution is not functioning as expected. Can someone assist me in resolving this problem? To demonstrate the problem, I have cr ...

Ensure that the footer is always positioned at the bottom of the page according

I've been struggling to get my footer to stay at the bottom of the page based on content. I've tried several CSS techniques and strategies, but I'm still encountering one issue. footer { width: 100%; padding: 25px 0 25px; backgr ...

additional space within bootstrap columns

I'm looking to eliminate the empty space above the 4th span and replace it with the next span. Edit: Without adding a new row. Here's the fiddle: http://jsfiddle.net/Dzh5E/7/ <div class="main-app"> <div class="row-fluid"> ...

Achieving a dynamic scrollable table that adjusts its height based on its parent container (HTML + Bootstrap)

I am trying to create a table that can be scrolled vertically using the standard overflow: auto property without specifying a set height for the table's container. While Bootstrap provides a class called table-responsive for handling horizontal scrol ...

How can I center two images using a hover effect in WordPress?

When the mouse hovers over an image, it changes to another image. Everything works smoothly except for one issue - the image is not centered. Below is the code I am currently using: HTML: <figure> <a> <img class="hover" src="na ...

Ways to expand the width of a b-tooltip?

When using b-tooltip tags from BootstrapVue to display information, I am looking to customize the width of the tooltip for longer text messages and adjust the text alignment. How can I achieve this? Is there a way to style it accordingly? <b-button i ...

The height set to 100% is causing issues with the padding-bottom property

Currently, I have a setup that includes: A div with width set to 100% and height set to 100% An SVG element inside the div with default width and height of 100% My issue is that when applying padding to the div, the left, right, and top padding seem to w ...

Achieving consistent height for child divs in AngularJS when one div's size is changed dynamically using ng-repeat

For my project, I have set up a parent div with two child divs inside. The first child div contains a list generated using ng-repeat and has a border around it. This div's height adjusts based on the list items. Now, I need the second child div to mat ...

Issue with Bootstrap rows overlapping on smaller screens

Basically, the issue I'm facing is that when I reduce the size of my screen, the columns drop below and start overlapping with the row beneath them. I've been trying to figure out if there's a simple solution that I'm overlooking. But ...

Adding a sign at the center of a map in React-Leaflet

One of the features I added to the map is a center indicator sign. <MapContainer fullscreenControl={true} center={center} zoom={18} maxNativeZoom = {22} maxZoom={22} classNa ...

Utilizing Inline Placement within Email Templates

Seeking assistance in finding an alternative method to achieve the desired functionality in an email template. Having trouble with inline position styling being removed. table { width: 80%; border-spacing: 0; } table tr td.label-dots { positio ...