Submenu alignment issue in RTL mode

I am currently utilizing bootstrap 4.4.1 for my template and I am attempting to implement a right-to-left (RTL) Nav bar for an Arabic website, ensuring the menu displays from right to left.

In my code, the fifth item from the right has a submenu which is not aligning correctly with the left side of the parent menu, as demonstrated in both the example and screenshot. If I add right :0; to the .submenu, it shifts the menu all the way to the extreme right.

Additionally, the menu does not remain open when hovering over it; it simply disappears when trying to move to child items.

I am uncertain about what mistake I might be making...

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

The desired menu layout can be observed in the image below:

https://i.sstatic.net/0h59s.jpg

https://codepen.io/KGuide/pen/gOpdLPR

.menu{direction:rtl;}
.header-menu{background:#EEE6D3; min-height:30px; text-align:center;  font-size:18px;}
.header-menu ul {
margin: 0;
list-style: none;
position: relative;
}

... (Code continues)
... (Additional Code continues)

Answer №1

Simply switch display: inherit; to block.

.header-menu ul li:hover ul {
  display: block;
}

Update: To resolve the quick hover problem, consider removing the top and padding properties from .header-menu ul ul:

.header-menu ul ul {
  display: none;
  position: absolute;
  /* top: 50px */
  background: #eee6d3;
  padding: 0 /* remove padding */
}

https://codepen.io/awran5/pen/vYOzyJm


However, I recommend exploring Bootstrap-rtl

<link href="https://hamidheidarinia.github.io/bootstrap-rtl/4.2/assets/css/bootstrap-rtl.min.css" rel="stylesheet" />
<script src="https://hamidheidarinia.github.io/bootstrap-rtl/4.2/assets/js/jquery.min.js"></script>
<script src="https://hamidheidarinia.github.io/bootstrap-rtl/4.2/assets/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>


<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div class="dropdown-menu
                                                                            dropdown-menu-left" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

Answer №2

Understanding Positioning

The issue at hand revolves around grasping the functionality of CSS properties like position: relative and position: absolute.

When an absolutely positioned element is nested within a relatively positioned container, it leverages the relative positioning to establish its boundaries, offering potential advantages for layout.

To address your specific scenario, consider implementing the following adjustments -

.header-menu ul li {
    display: inline-block;
    font-size: 18px;
    font-weight: 600;
    position: relative; /* new attribute */
}

.header-menu ul ul {
    display: none;
    position: absolute;
    top: 50px;
    right: 0; /* new attribute */
    padding: 0; /* additional property to prevent style conflicts */
    background: #EEE6D3;
}

By positioning .header-menu ul ul absolutely beneath .header-menu ul li, which has relative positioning, the submenu aligns with its parent's rules and remains anchored to the right. I trust this clarifies your understanding.

Solution for Hover Issues (not applicable anymore but noted for future queries)

In case of the secondary problem regarding hovering over submenu items, consider these solutions -

  1. Avoid adding background directly to .header-menu ul ul
  2. Transfer the background styling to .header-menu ul ul li
  3. Add slight padding to the top of .header-menu ul ul
  4. Reduce the top property of .header-menu ul ul by about 2 pixels`
.header-menu ul ul {
    display: none;
    position: absolute;
    top: 48px;
    right: 0;
    padding: 2px 0 0 0;
}

.header-menu ul ul li {
    min-width: 150px;
    float: none;
    display: list-item;
    position: relative;
    background: #EEE6D3;
}

Explanation: The challenge arises from losing hover state when moving over submenu items. The provided code aims to maintain continuous focus on the submenu by adjusting its positioning and padding to keep the cursor within the menu structure.

By staying within the hierarchical boundaries while hovering, the submenu items remain visible without disappearing abruptly.

Codepen Example: https://codepen.io/ardethian/pen/MWwqbQM

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 modal window function on iOS 8

I have a mobile app available on the store that was created using the Ratchet framework. Everything was working smoothly until the release of iOS 8. Now, when I test the app on iOS 8, the title bars for modals are not appearing. Although the controls like ...

Transferring information from Django function to Javascript without the need for refreshing the page

I am currently working with a Django function. @csrf_exempt def postdata(request): r = requests.post(url ,headers=headers, auth=auth, data=json.dumps(data)) return HttpResponse(r) My goal is to pass the variable 'r', which contains a di ...

Customizing Material UI Select for background and focus colors

I am looking to customize the appearance of the select component by changing the background color to "grey", as well as adjusting the label and border colors from blue to a different color when clicking on the select box. Can anyone assist me with this? B ...

Upload of file successfully completed even though limit was set; input remains unchanged

I seem to be facing an issue where even after confirming with the ok in the window.alert message, the file name still appears next to Choose File. It seems like the file is still being uploaded to the input. How can I prevent this from happening? <inp ...

Bootstrap 5 introduces an innovative animated hamburger icon that transforms into an X symbol when clicked

I have encountered an unusual behavior when trying to animate the hamburger button using Bootstrap 5. Previously, with Bootstrap 4, everything worked smoothly. However, in Bootstrap 5, I noticed that the icon initially displays as 'X' and then sw ...

I am facing issues with my Angular CRUD application when trying to update data through the API, as it is not functioning properly as a Single

After successfully implementing the CRUD Application In Angular Using API, I encountered a slight issue. When updating values, they do not reflect instantly without reloading the page. Here's a glimpse into my code: Below is my app.component.ts: imp ...

Mobile FPS suffering due to slide-out drawer performance issues

My application features a drawer menu that functions smoothly on desktop, but experiences issues on mobile devices with noticeable lag. Within the header, I have implemented a boolean value that toggles between true and false upon clicking the hamburger i ...

What is the best way to make the block rotate each time the button is clicked?

Specifically, I am having trouble getting the block to rotate properly. Currently, the block only rotates once when clicked, but I want it to rotate each time I click in both directions. Any suggestions on how to achieve this? var now = 10; $('. ...

Is it possible to enable auto play on HTML5 Video for iPad similar to BBC iPlayer?

Can HTML5 Videos automatically play on iPads? I used to believe that it wasn't possible because of iOS restrictions, but after trying BBC iPlayer on my iPad, I noticed that the videos did autoplay upon loading. Could this indicate that it is indeed ...

Using jQuery to retrieve the tag name of the selected element

What is a simple method for retrieving a tag name? For instance, if I input $('a') into a function, how can I extract the tag name 'a'? ...

Tips on concealing and deactivating the fullscreen option in flowplayer

Could someone please assist me in resolving this issue? I've been attempting to hide the fullscreen button from the flowplayer, but so far, I haven't found a solution. Below is my JavaScript code: <script> $f("player", "flowplayer/flowpla ...

Tips for implementing this code for effective DAO and CRUD operations

As I venture into coding CRUD operations for the first time, I encountered an issue while testing the update functionality: The error message "Dao.get() missing 1 required positional argument: 'id'" is puzzling me. Where could I have gone wrong? ...

Every form submission leads to an increment in the number of ajax calls being made

When attempting to submit my form using ajax, I encountered an issue with the code. Initially, the ajax call does not trigger on the first click. I have to click it a second time for the ajax to work. Additionally, with each submission, the number of aja ...

Angular2 can enhance your webpage with a <div> element that covers the entire screen

While working on my Angular2 application, I encountered a challenging issue that I can't seem to resolve or find a solution for. Take a look at this image of my page: https://i.stack.imgur.com/b6KlV.png Code: content.component.html <app-header& ...

Placing a Label Next to an Input Field using Bootstrap 4

Is there a way to position the label right next to the input form? I've attempted different methods without success. Could you please review my code below? <div class="modal-body"> <form class="form-horizontal"> <div cl ...

Using Data URIs can negatively impact the loading speed of a webpage

I created a custom script to replace all inline images with data URIs in order to minimize http requests and speed up loading times on mobile devices. Surprisingly, I noticed a decrease in loading speed. Could it be because the HTML file was larger (aroun ...

Tips on placing two tags within one cell in a HTML Bootstrap table

I am a beginner in html and bootstrap, and I am trying to create a table. However, I noticed that the input fields are not aligning properly within the cells. Even when I tried adjusting the width to 100%, the % sign ended up on a separate row. Can anyone ...

What could be the reason my checkbox won't check using jQuery?

Currently, I am using kojs within my Magento 2 project. I have implemented a method that successfully shows and hides an input box based on user interaction. However, despite the function working as expected, the checkbox does not display its checkmark. D ...

Achieve consistent column heights with flexbox styling

I have a solution for achieving equal height columns using the CSS property display:table. Here is an example: .row { display: table; width: 100%; } .col{ display: table-cell; } However, in my case, I am using flexbox and the row class has ...

The Hamburger Menu in the Bootstrap Navbar is failing to collapse

My website uses Bootstrap v5.3 and I've added a navbar, but the hamburger icon is not collapsing on mobile devices when clicked. Can someone please review my code to see if there are any issues? I can't figure out what's wrong. <!DOCTY ...