How to align text to the right in a Bootstrap 5 card header

I'm facing an issue with a Bootstrap 5 card where I want to align certain content in the header to the right and some to the left. Despite my efforts, everything remains left-aligned. Here's a snippet of my code:

  <div class="card">
    <div class="card-header">
      <a href="/users">Users</a>
      <span class="text-end text-muted">
             48
      </span>
    </div>
    <div class="card-body">
       <p>Card content</p>
    </div>
  </div>

I've tried using text-end, float-right, and pull-right on the span element but it hasn't worked. Everything remains left-aligned.

Thank you for any assistance you can provide.

Most examples online are based on Bootstrap 4, but I specifically need help with Bootstrap 5 as previous solutions have not been effective.

Answer №1

If you're looking to achieve this effect, check out the power of Flexbox.
Simply add .d-flex to the .card-header and then apply .ms-auto to the <span> that you want positioned on the right side.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c2e23232b383a24253e6572716f73642d36392628797f7936313735">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PgdjTmRZZiCAngseXB83DfGTowi0iMjiWaeVhhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">

<div class="card">
  <div class="card-header d-flex">
    <a href="/users">Users</a>
    <span class="ms-auto text-muted">48</span>
  </div>
  <div class="card-body">
    <p>Card content</p>
  </div>
</div>

Answer №2

If you're looking for Bootstrap 5 solutions, there are similar ones available here. The new version of Bootstrap has replaced instances of left and right with start and end to support right-to-left languages...

In your situation, the simplest approach would be to utilize float-end...

    <div class="card">
        <div class="card-header">
            <a href="/users">Users</a>
            <span class="float-end text-muted"> 48 </span>
        </div>
        <div class="card-body">
            <p>Card content</p>
        </div>
    </div>

Alternatively, you can make the card-header a flexbox by using d-flex and then adding auto margin (ms-auto)...

   <div class="card">
        <div class="card-header d-flex">
            <a href="/users">Users</a>
            <span class="ms-auto text-muted"> 48 </span>
        </div>
        <div class="card-body">
            <p>Card content</p>
        </div>
   </div>

Check out the demo here

Answer №3

<div class="section ">
    <div class=" section-header">
        <a href="/categories">Categories</a>
        <span class="float-end text-muted">
            23
        </span>
    </div>
    <div class="section-body">
        <p>Section content</p>
    </div>
</div>

You can even shift the header contents to the right side

  <div class="section ">
        <div class=" section-header text-end">
            <a href="/categories">Categories</a>
            <span class="float-end text-muted">
                23
            </span>
        </div>
        <div class="section-body">
            <p>Section content</p>
        </div>
    </div>

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

PHP - WebCalendar - Show or Hide Field According to Selected Item in Dropdown List

Working with the WebCalendar app found at to make some personalized adjustments to how extra fields function. I've set up two additional fields: one is a dropdown list of counties, and the other is a text field. Within the dropdown list, there is an ...

Achieve a full-width span for a single item in a CSS grid without any alterations to the HTML code

How can I make the first item in this grid span 100% without changing the HTML structure? The first item is assigned an extra class ".sub" Is it possible to achieve this? Click here for more information <div class="fd-col fd-5col"> <div class= ...

Selenium's get_attribute method is not providing a value

Below is the code I am currently working with: url="https://www.betexplorer.com/soccer/poland/ekstraklasa/lks-lodz-lechia-gdansk/fgQY4hAD/" browser = webdriver.Chrome() browser.get(url) time.sleep(1.5) trs = browser.find_elements_by_xpath(".//div[@id=&a ...

Get rid of the blue dots that appear on anchor tags

I am facing a strange issue with the anchors on my webpage. There are a few anchor tags in my body: <a href="link1.html"></a> <a href="link2.html"></a> <a href="link3.html"><img src="img1.png" /></a> Interestingl ...

Using CSS to overlay a fixed element with a specific z-index

HTML: <div class="wrap"> <div class="fixed"></div> </div> <div class="cover"></div> CSS: .cover{z-index: 10;} .wrap{position: relative; z-index: 1;} .fixed{position: fixed; display: block; z-index: 1;} Example: ...

Positives and negatives images for accordion menu

I have successfully created an accordion list using HTML, CSS, and JavaScript. However, I would like to enhance it by adding a plus and minus picture in the left corner of the heading. Is there a way to achieve this functionality? I have two images that I ...

When using IE6, images may momentarily appear stretched when set to height:auto

I have set the height to auto, however I am observing that small thumbnail images are being momentarily stretched vertically in Internet Explorer 6 before adjusting to their correct height. It is worth mentioning that the image tag in the HTML appears as ...

CSS transitioning can be tricky when trying to fade an image or video with a solid background alongside an HTML element that shares the same background color

When attempting to apply a fade in/out effect to an image or video with a solid background color and using the same transition for the opacity of an html element with a matching background color, a noticeable "square" appears around the image/video. Why d ...

The CSS header remains the same size regardless of the size of the dynamic table

Is there a way to set the header in an XHTML page to be as wide as the page itself? I used width:100%, but the issue is that I have a dynamic table and when it grows bigger, the header doesn't expand. How can I solve this problem? #header{ top: 0 ...

Adjust the minimum height and width when resizing the window

Apologies in advance if this has already been addressed, but I have tried solutions from other sources without success. My Objective: I aim to set the minimum height and width of a div based on the current dimensions of the document. This should trigger ...

The background of the div fails to appear

Why is my div's background not displaying properly? Check out the code below: <div style=" width:676px; height:230px; display: inline; margin: 0 0 0 0; background-image: url('http://www.goodsstall.co.uk/ekmps/shops/goo ...

What is causing the input boxes to exceed their container boundaries so drastically?

My plan was to organize 4 input text boxes in 4 equal columns on a single row of the form. Using Bootstrap's grid column classes, I set col--3 for medium and large screens for those four inputs. For small and extra-small sizes, I designated them as co ...

Ensure portrait orientation images have a restricted width to maintain their aspect ratio

On a responsive site, I have set up flexslider to showcase both landscape and portrait orientation images. The smoothHeight: true option is enabled to adjust the height according to the images. However, at the largest media query where the flexslider cont ...

The arrangement of elements in an inline-block is determined by the width of the section

I have been attempting to replicate the layout shown in the image for quite some time. The elements (.element) are aligned vertically but they are not centered, instead they stick to the left side. My current code is as follows: HTML: <div id="_tec ...

Flask website does not refresh after sending a POST request

I'm facing a challenge with implementing Flask to display text on an HTML page after a POST request. Even though the POST request returns a 200 code and I can see it in the terminal upon button click, the template with the result text is not being ren ...

Step-by-step guide to activating vertical scrolling for select tiers in a multi-layered fly-out navigation

Currently working on a multi-level flyout menu to be integrated into a daterange picker for selecting time periods. Check out the jsfiddle link here: https://jsfiddle.net/6t72hd4x/1/ I attempted to set overflow-y: auto; in the .inner-list class to handle ...

Autofill feature for styling with CSS within a JavaScript document

Is there a way to enable CSS autocomplete in JavaScript for VS Code? I can easily work with CSS, but the auto suggestions are not showing up when coding in JS. For instance, when typing document.queryselector("myclass")., nothing shows up after the dot li ...

Leveraging flask/jinja for incorporating dynamic content without the need for an HTML table

Although I'm not well-versed in web development, I hope this question is on the right track. In my flask app, I'm trying to create a section that showcases albums/cards in a similar style as seen on this example. My goal is to display multiple ...

Place a PNG image within a div container carousel

Despite my efforts, I have been unable to find an example of what I am trying to achieve. I have a carousel and my goal is to insert a PNG image into the carousel in such a way that it slightly exceeds the top of the div like this: https://i.sstatic.net/M ...

Warning: HTML input values are being cleared when added

I've been working on some code that adds an input field when a button is clicked. When the limit reaches 5 (counter), it displays a bootstrap warning. The issue I'm facing is that after hitting "add field" more than 5 times, the values in the fie ...