How can I align my dropdown menu to the right using jQuery Mobile?

I am trying to create a dropdown menu on the right side of my navbar, but I'm running into some issues. The element keeps floating left instead of right and the CSS for the li element is not being applied. I must be missing something. Can anyone offer some help or suggestions? Thanks in advance.

HTML code for header:

<div data-role="header" data-theme="b" id="header">
<div data-role="navbar" data-iconpos="bottom" class="nav-custom">
    <ul>
        <li>
            <a href="#" class="back" data-rel="back" data-transition="fade" data-theme="" data-icon="back">
                Back
            </a>
        </li>

        <li>
            <a href="#" id="bars" data-icon="custom" data-iconpos="notext" data-iconshadow="false">&nbsp;</a>
        </li>

    </ul>
         </div>
        <ul id="menu-right">
            <li data-icon="false"><a href="#a1">Option B1</a></li>
            <li data-icon="false"><a href="#a1">Option B2</a></li>
        </ul> 

</div>

Javascript code:

     var windowWidth = (parseInt($(window).width())/2);
    $('#menu-right').css({'width': windowWidth});
    $('#bars').bind('click', function(event) {
        event.preventDefault();
        $("#menu-right").toggle();

    });

CSS code:

    #header #menu-right{        
    display: none; /* Hide */
    z-index:500; /* Ensure visibility over other elements on page */
    margin-top: 0px; /* Bring menu closer to button; not needed on mobile */
}
#header #menu-right li{

    display: block; /* JQM makes a inline-blocks... reset it to block */
}
#header  ul li a{
    white-space: normal; /* Stop long menu names from truncating */
}
#menu-right{
    position: absolute;
    float: right !important;
    margin-right:0.5em; 
}

Link to the JavaScript file:

Jsfiddle

Answer №1

When styling #menu-right in CSS, it is recommended to use

right: 0;

instead of

float: right !important;

Avoid combining absolute positioning with floating as they are not compatible.

To correct the CSS:

Include data-role="listview" data-inset="true" in your ul element with the ID "menu-right".

Answer №2

In my opinion, it would be beneficial to relocate the #menu-right within the plus li element.

<li>
    <a href="#" id="bars" data-icon="custom" data-iconpos="notext" data-iconshadow="false">&nbsp;</a>
    <ul id="menu-right">
        <li data-icon="false"><a href="#a1">Option B1</a></li>
        <li data-icon="false"><a href="#a1">Option B2</a></li>
    </ul> 
</li> 

Additionally, consider updating the CSS:

#menu-right{
    margin:0;
    padding: 0;
}

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

To activate two separate click events, one for an absolute element and another for the element positioned behind it, simply click on the desired absolute element to trigger both actions

Is it possible to trigger click events for both of these elements? position: relative container ELEMENT 1: standard div ELEMENT 2: position: absolute div In real life, element 2 is a transparent backdrop designed to capture clicks on top of the header ...

Tips for verifying the status of an input element following the dynamic addition of a required element

I have implemented a required field for all input elements in a specific state using the following code: for (var i = 0; i < data.length; i++) { // activate required function v = data[i].Required; if (v) document.ge ...

How to Adjust Overlapping Text in CSS?

Currently, I am facing an issue with an element overlapping in my CSS file. On a regular browser, one line of text appears fine but on mobile devices, it overlaps into two lines. This problem is specifically for the mobile version of the website, particula ...

transferring information from a JavaScript variable to an EJS variable

I am currently storing a value in the 'items' variable within a script tag in an HTML document. However, I now need to pass this data from 'items' to the back end in order to store it in a database. I attempted to make 'selectedTea ...

Animate photos with identical class names individually using CSS

Check out this snippet: <img class='img-logo' src={logo} alt="Logo" /> <img class='img-logo' src={logo} alt="Logo" /> <img class='img-logo' src={logo} alt="Logo" /> <img class ...

Place the image at right:0px position, but it won't display

I am trying to position an image at the end of a sentence within a div element. No matter what I try, the image always appears next to the end of my text. Here is the code I am using: <div id='xi' class='xc'>Text comes here and ...

Arranging elements in a single line using Bootstrap 4

I need to align links in a row at the bottom, such as "Link 1 left bottom", "Link 2 center bottom", "Link 3 right bottom" but "Link 2" is not centered on PC and mobile devices. HTML: <div> <a id="contact" hre ...

Exploring a React JS option for filtering repeated elements, as an alternative to Angular

While using Angular JS, I came across the 'filter' option within ng-repeat which is useful for live search. Here's an example: <div ng-repeat="std in stdData | filter:search"> <!-- Std Items go here. --> </div> &l ...

onServiceConnected not being invoked

I'm currently working on a Cordova plugin that is responsible for managing expansion files in Android. The reading of an expansion file is functioning properly, however, I am encountering issues with the downloading process. Below is the code snippet ...

Transforming the style of a particular element within React using Array().fill().map's method

Looking for a way to dynamically change the color of an array in React + styled jsx? Let's say you want to change the color when the number of elements in the array is a multiple of 4 (e.g. the index is 4, 8, etc.) Is there a clever solution to achi ...

Styling images with text alignment in CSS

I'm trying to figure out how to align an image to the left, some text to the top right, and some text to the bottom right. The parent div contains a background image. <div id="parent" style="background: url(bg.jpg) repeat-x left top"> <i ...

Tips for showcasing HTML code retrieved from a fetch request in a Vue component

Trying to integrate HTML code received from an API into my VueJS code. The API provides the following: <p>blah blah blah</p> <ul> <li> 1 </li> <li> 2 </li> </ul> Saving this code snippet into a variable, but ...

Switching the horizontal tab design from Bootstrap 3 to Bootstrap 4

I'm currently working on adapting a code snippet from bootsnip that creates horizontal Bootstrap tabs with material-style design. I've encountered some challenges in converting it to Bootstrap 4, specifically in updating the CSS to use .nav-item ...

Can someone explain the extent of `Bundle savedInstanceState`?

When it comes to preserving the instance state of activities in Android, we utilize onSaveInstanceState(Bundle outState). To properly implement this, it's crucial to have a distinct string key for every variable stored within outState. I'm curi ...

Preserve HTML element states upon refreshing the page

On my webpage, I have draggable and resizable DIVs that I want to save the state of so they remain the same after a page refresh. This functionality is seen in features like Facebook chat where open windows remain open even after refreshing the page. Can ...

Adjusting height in CSS can be done dynamically based on the content within

Currently, I am working on a project where I need the submenu to adjust based on content. The issue is that right now, the submenu has a fixed capacity of 4 items. For example, when you click on 'sale', it displays 4 submenu items perfectly. Howe ...

PHP File Tutorial: Displaying Many-to-Many Relationship in an Android Database

I'm currently working on a cooking app that organizes recipes into categories. For example, when a user selects the category BREAKFAST, they should see all recipes assigned to BREAKFAST. However, at the moment, the app is displaying the same recipes f ...

Setting the z-index for Bootstrap dropdown menus on containers that are generated dynamically

When using the Bootstrap 3 dropdown-menu within a dynamically generated container, I am encountering an issue where the dropdown-menu appears behind the newly created elements. Please refer to the image for visual clarification. The container item has pos ...

Enhancing ajax requests with headers in Ember.js

My goal is to configure request headers for my emberjs application. However, when setting it up in the initializer, the client_id appears as [object Object] instead of the actual value. This is the initializer that runs when the application starts. Apikey ...

How can I use AngularJS to initiate code to run after the view and controller have successfully synchronized a specific change?

I am currently using AJAX to load date ranges into a pair of SELECTs (managed with AngularJS ng-repeat), which are then transformed into a slider using jQuery UI's selectToUISlider. My concern is that selectToUISlider may behave unexpectedly if the SE ...