Align the subtext to the right and center it vertically within a Bootstrap dropdown menu item

Is there a way to vertically align small captions on the right side of a dropdown menu, next to each item's text? The issue is that in the example below, the numbers are not properly aligned.

https://i.stack.imgur.com/NZigW.png

The numbers 123 and 12345 appear at the top instead of being aligned with their respective lines. Additionally, 1234567 wraps to a new line which is undesirable.

I was able to achieve the desired effect without using Bootstrap, but incorporating it into the code causes problems. Unfortunately, platforms like jsfiddle stretch the example width when inputting the code, making it difficult to pinpoint the issue. Here is the code snippet:

<head><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<style>
.ih {
    font-size: 85%;
    float: right;
}
.name {
    float:left;
}
</style>
</head>
<body>
<ul class="nav navbar-nav">
<li class="dropdown open">
<ul class="dropdown-menu" id="a">
<li>
<a href="#">
<div class="name">blah blah</div>
<div class="ih">123</div>
</a>
</li>
<li>
<a href="#">
<div class="name">short</div>
<div class="ih">12345</div>
</a>
</li>
<li>
<a href="#">
<div class="name">really really really long</div>
<div class="ih">1234567</div>
</a>
</li>
</ul>
</li>
</ul>
</body>

Answer №1

If you find yourself needing to utilize Bootstrap, it is recommended to leverage Bootstrap components and styles rather than trying to create your own layout from scratch. By customizing the existing Bootstrap elements, it becomes easier to maintain your code in the long run.

To achieve your desired outcome, a simple solution would be to treat your dropdown as a list group. Apply the "list-group" class to your "dropdown-menu" element and change it from a "ul" to a "div". Then ensure that your "a" tags have the "list-group-item" class with "pull-right" on the numbers.

In the provided fiddle example, adjust the splitter to observe how the design behaves across different screen widths. For a better view of the code snippet, expand it to full-screen mode.

Example Fiddle: http://jsfiddle.net/abhitalks/1o5o29jy/

Snippet:

.dropdown-menu { padding: 0px; }
.dropdown-menu, .list-group { border-width: 0px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <ul class="nav navbar-nav">
            <li><a href="#">Link</a></li>
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">
                    Dropdown <span class="caret"></span>
                </a>
                <div class="dropdown-menu list-group">
                    <a class="list-group-item" href="#">
                        <span class="pull-right">123</span>
                        <span>Caption</span>
                    </a>
                    <a class="list-group-item" href="#">
                        <span class="pull-right">12345</span>
                        <span>Description</span>
                    </a>
                    <a class="list-group-item" href="#">
                        <span class="pull-right">99</span>
                        <span>Very long description</span>
                    </a>
                </div>
            </li>
        </ul>
    </div>
</nav>

Answer №2

It appears that the issue at hand is due to the lack of space for rendering on the same line.

To address this, there are two possible solutions:

Option 1# You could increase the width of the .navbar-nav class to accommodate the additional text. However, if you prefer a more dynamic approach, I recommend trying the alternative method outlined in option 2.

Option 2# Incorporate the extra text within the .name class and set its opacity to 0 using opacity:0;. Instead of floating right for the .ih class, utilize

position:absolute; right:0; top:6px;
, and apply the following CSS styles.

The modified code snippet can be seen below.

.dropdown-menu li a {
    position:relative;
}
.ih {
    font-size: 85%;
    position: absolute;
    right:0;
    top:6px;
}
.ih-dummy { /* new class */
    margin-left:15px;
    font-size:85%;
    opacity:0;    
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<ul class="nav navbar-nav trial">
    <li class="dropdown open">
        <ul class="dropdown-menu" id="a">
            <li><a href="#">
            <div class="name">blah blah<span class="ih-dummy">123</span></div>
            <div class="ih">123</div>
          </a>

            </li>
            <li><a href="#">
            <div class="name">short<span class="ih-dummy">12345</span></div>
            <div class="ih">12345</div>
          </a>

            </li>
            <li><a href="#">
            <div class="name">really really really long<span class="ih-dummy">1234567</span></div>
            <div class="ih">1234567</div>
          </a>

            </li>
        </ul>
    </li>
</ul>

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

Begin the div at a width of 0 pixels and expand it towards the right

My current project involves opening a row of blocks with an animation. The desired outcome is to transition from 2 blocks to 3 blocks by clicking a button. Specifically, the block that needs to be added should appear in the middle and fade in from left to ...

What could be causing the malfunction of my navbar and carousel functionality?

I am currently facing some challenges with the carousel in my HTML code. I am unable to get it to display images properly. Additionally, there is an issue with the background color of my navigation bar - it doesn't stay at 100% width. Also, when using ...

Is it possible to display a thumbnail image in a separate full-sized window by using CSS or JavaScript?

I am currently utilizing a program called WebWorks 2020.1 that automatically creates <img> tags from my FrameMaker source input when published to DHTML. Unfortunately, I do not have the ability to directly modify the HTML <img> or <a> tag ...

Using JavaScript, redirect to a specified URL once a valid password has been entered on an HTML form

Need help with JavaScript for password validation and URL redirection. I'm new to JS and used some resources like Password correct? then redirect & Adding an onclick function to go to url in javascript?. Here's my current code: ...

What are the different methods to display information in Angular?

list.component.ts import { Component, OnInit } from '@angular/core'; import { StudentAPIService } from 'src/app/services/student-api.service'; import { StudentModel } from 'src/app/model/student'; @Component({ selector: &ap ...

Is there a way to format a block of text so that right-to-left text aligns to the right, while left-to-right text aligns

I have a content block that includes text in both left-to-right and right-to-left languages, and I need to ensure that both display correctly. If the user begins with left-to-right text, it should be displayed from left to right with left alignment If the ...

Ensure that the navigation menu is consistently displayed properly, without any of its contents extending beyond the

I am experiencing an issue with the navigation menu on my website. Everything looks great in normal view, but when I resize the browser window to a smaller width, the menu ends up overflowing below the main content. I want the navigation menu to always dis ...

Troubleshooting bower installation issues on a Mac operating system

zandeMacBook-Pro:reactQa root# bower -v 1.5.2 zandeMacBook-Pro:reactQa root# sudo bower init bower ESUDO Cannot be run with sudo Additional error details: Since bower is a user command, there is no need to execute it with superuser permissions. If ...

Tips for creating a fixed element with ScrollTrigger and GSAP

How can I prevent the right div from jumping on top of the left div when using a scroll trigger to make the left div's position fixed? gsap.registerPlugin(ScrollTrigger); const tlfour = gsap.timeline({ scrollTrigger: { trigger: ".ma ...

working with html data in a bootstrap modal using javascript

Utilizing an id, I pass data to a bootstrap modal and link that id with stored data. $("#fruit").html($(this).data("fruit")); In addition, I am appending data to the bootstrap modal $('#mymodal').find('.modal-body').append('< ...

What could be causing the malfunction of the Facebook iframe like button?

Even though it functions offline, there seems to be an issue with its performance online. Here is the code that was used: <iframe src="http://www.facebook.com/plugins/like.php?app_id=125925834166578&amp;href=http%3A%2F%2Fwww.facebook.com%2FBaradei. ...

Extract Information from a Website

Is there a way to extract data from another website using JavaScript and save it into a .TXT or possibly an XML file? If JavaScript is not the best solution, I am open to other suggestions. I am specifically interested in extracting the price and item na ...

"CSS: Mastering the Art of Color Curves

Is there a way to achieve this with just HTML and CSS? ...

Binding HTML Elements to JavaScript Values

Currently, I am working on a project with .Net and Vue. However, I am facing an issue in binding a value in a label. <li v-for="restaurant in vHolidays.data" > <form id="1"> <div> ...

Make the child div images disappear gradually and at the same time, make an overlapping parent div image appear using Javascript

Check out my attempt at solving this challenge on jsfiddle: http://jsfiddle.net/oca3L32h/ In the scenario, there are 3 divs within a main div. Each of these divs contains an image and they overlap each other. By using radio buttons, the visibility of the ...

Preloading images before loading a div using JavaScript

Can you walk me through implementing object first and then mergeObject in JavaScript? I have an interesting scenario where I need to display the original list followed by a merged list after a short delay. How can I achieve this using JavaScript? Specific ...

What is the best way to ensure my footer remains at the bottom of the page even when scrolled?

I'm struggling to get my footer to stay at the bottom of a page that requires scrolling. The code I have only seems to work on pages without scrolling, causing the footer to display in the middle of the page instead of at the bottom where it belongs. ...

Preventing the cascading effects of past hovers with AngularJS

I am working with AngularJS and have the following HTML code snippet. <div class="row" style="margin-left:60px; text-align:center;"> <div class="col-xs-1 " style="margin-left:25px;width:10px; " ng-repeat="image_thumb_id in image_thumbnail_ ...

How to choose a single checkbox in Ionic 2 and retrieve its value

On my single screen, I have about 20 questions and answers retrieved from a database. To display the options, I am using checkboxes but struggling to implement the desired functionality. What I want is to allow only one checkbox selection out of 4 options ...

What causes Safari to display a black border around resized videos?

I've come across a strange issue while using Safari. Whenever I try to play an MP4 video with dimensions of 1920 * 1080, and resize the Safari window to a width of 937px, something strange happens: https://i.stack.imgur.com/oI50i.png A black border ...