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

Tips for enabling background scrolling when Popover is displayed: (React, React Native, Native-Base)

I'm utilizing the Popover component from NativeBase to design my popover: const SettingsButton: React.FC<Props> = () => { return ( <Popover trigger={(triggerProps) => { return ( ...

Trouble with stroke width in Svg bezier path while using a mask

I'm having trouble hiding part of a path using a mask. When I apply the mask, the stroke width doesn't seem to work anymore. Any suggestions on why this is happening and how to fix it? svg { position: absolute; width: 100%; height: 100%; ...

There was a lack of dynamic content on the webpage when using the view/template engine (Handlebars)

I'm currently using the Handlebars template engine in my project. Despite not encountering any errors in the console, I'm facing an issue with displaying dynamic content in the index.hbs file that is rendered from app.js. app.js const express = ...

What is the best way to change the date format of a JSON string to a custom format?

Hello, I am seeking advice on how to convert a JSON string date from a response into the format of "8/24/2016". I attempted to use a dateFilter.js file, but encountered errors. Here is my attempted code: Below is the dateFilter.js code that resulted in an ...

Adjust element based on the position of another element (sticky)

Is it possible to rotate a text block so that it remains anchored to the bottom of a red rectangle, even when the rectangle is rotated? Similar to how it works in Figma, but simpler. For example, if I rotate the rectangle by 180 degrees, the text should be ...

"Displaying <canvas> at its true resolution even when zoomed in, thanks to Retina technology

I am currently working with a <canvas> element to create vector graphics and I want to ensure that it displays in the highest quality possible while using minimal resources across various viewing conditions. Specifically, I aim to achieve a 1:1 mappi ...

Choose the initial unordered list within a specific division through Jquery

In a div, there is a ul. Inside a li, there is another ul. The task is to select only the first ul inside the div using jQuery. The HTML markup: <div class="parent"> <div class="clearfix"> <div class="another-div"> <ul cl ...

Is it possible to generate HTML form fields with fixed positions when using Orbeon forms?

Is there a way in Orbeon forms to position input fields at specific coordinates, relative to a background image? I currently have a paper form that fills an entire page. My goal is to use the scanned image of this form as a background and then overlay inp ...

Step-by-step guide on creating a blank horizontal line in an HTML table

Can anyone help me figure out how to draw a horizontal line connecting from td 1 to td 4 without relying on content CSS? I'm envisioning a progress bar like the one shown in the following image: https://i.sstatic.net/on8Bi.png <table> < ...

The HTML checkbox remains unchanged even after the form is submitted

On a button click, I have a form that shows and hides when the close button is clicked. Inside the form, there is an HTML checkbox. When I check the checkbox, then close the form and reopen it by clicking the button again, the checkbox remains checked, whi ...

Chrome Canary's behavior with CSS border-radius on tiny objects

Is it possible to achieve CSS border-radius on really small objects in Chrome Canary? This experiment with a 3px high line and a 2px border radius, when zoomed in at 600%, doesn't show much difference: It works perfectly fine in regular Google Chrom ...

What could be the reason for the hover class not functioning properly in Tailwind?

Looking for some help with styling a button in Next.js using Tailwind. Specifically, I want the background color of the button to change when hovered over. I've been experimenting with code like this: <button className="rounded-full bg-gradie ...

Troubleshooting a unique CSS bug in jQuery mouseover functionality

Check out this pen: https://codepen.io/anon/pen/eKzEVX?editors=1111 I recently created a Form Select in Laravel: {!! Form::select('status_id', $statuses, $post->status_id, ['class' => 'form-control post-sub-items-label &apo ...

Choose class based on the retrieved information

Good morning, I am working on dynamically setting the class of a td element based on data fetched from a database to reflect any changes or updates. One of the fields returned has four possible options, and I need to modify the CSS class of that field acc ...

What steps can I take to make the cards in bootstrap 5.3 responsive for mobile devices?

I'm struggling with my code and need some help. I've been editing it, but things seem to be getting worse instead of better. Specifically, I can't figure out how to set up my container, row, and columns in Bootstrap properly. My site uses ca ...

Issue with combining overflow-x, Firefox, and JavaScript

In Firefox, an issue arises that does not occur in other browsers. To see the problem in action, please visit this example page: -removed- Try selecting a page other than the home page. The window will scroll to your selection. You can then scroll down u ...

Seeking to achieve perfect vertical alignment of two columns within zurb foundation

I'm currently working with Zurb Foundation alongside Sass Compass, though this issue can extend to any CSS framework. Here's an example of the code I have: <div class="row"> <div class="small-6 column">...</div> <di ...

Tips for calculating the canvas-relative mouse position on a CSS 3D transformed canvas

Recently decided to challenge myself by experimenting with drawing on 3D transformed canvases. After some trial and error, I managed to get it partially working. const m4 = twgl.m4; [...document.querySelectorAll('canvas')].forEach((canvas) =& ...

Arranging the placement of an arrow within an HTML dropdown menu

I'm struggling with positioning my custom arrow in select elements. Each select has a different width, and I need to ensure that the distance from the right edge of the select to the arrow is consistent across all of them. Currently, I am using backg ...

Puggy, the friendly pup, loves nothing more than going for a stroll through

I'm attempting to display a PUG page in NodeJS, but I am encountering issues with jQuery. script(language='javascript', src='js/bootstrap.min.js') //operating properly script(src='https://code.jquery.com/jquery-3.5.1.slim.m ...