Place the badge in the upper right-hand corner using Bootstrap 4

Currently, I am working on creating a NavBar in bootstrap with React. My goal is to add a badge in the top right corner of the button.

This is how it looks like now:

https://i.sstatic.net/04oKy.png

<div>
                        <button className="btn btn-primary btn-lg navbar-btn">
                            <i class="fas fa-file-invoice-dollar fa-lg"></i>
                            <span class="badge badge-success">1</span>
                        </button>
                        <button className="btn btn-primary btn-lg navbar-btn">
                            <i class="fas fa-envelope fa-lg"></i>
                        </button>
                        <button className="btn btn-primary btn-lg navbar-btn">
                            <i class="fas fa-cogs fa-lg"></i>
                        </button>
                        <button className="btn btn-outline-danger btn-lg" onClick={this.logoutClicked}>
                            <i class="fas fa-sign-out-alt fa-lg"></i>
                        </button>
                    </div>

My objective is to achieve this look:

https://i.sstatic.net/478Eo.png

Answer №1

No additional CSS required. Simply enclose the badge span within a sup element...

<sup><span class="badge badge-success">1</span></sup>

https://www.example.com/link123

Just so you know, using reboot allows this functionality to work by adding the following styles to the sup tag...

sup {
    top: -.5em;
    position: relative;
    font-size: 75%;
    line-height: 0;
    vertical-align: baseline;
}

Answer №2

To achieve this effect, you need to apply position: relative; to the parent element, and then use something like

position absolute; top: -5px; right: -5px;
on the child element.

.holder{
 position: relative;
}

.label.label-awesome{
 background-color: yellow;
 position: absolute; 
 top: -5px;
 right: -5px;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">

<div>
  <button class="btn btn-primary btn-lg component-holder">
      <i class="fas fa-file-invoice-dollar fa-lg"></i>
      <span class="label label-awesome">1</span>
  </button>
  <button class="btn btn-primary btn-lg navbar-btn">
      <i class="fas fa-envelope fa-lg"></i>
  </button>
  <button class="btn btn-primary btn-lg navbar-btn">
      <i class="fas fa-cogs fa-lg"></i>
  </button>
  <button class="btn btn-outline-danger btn-lg" onClick={this.logoutClicked}>
      <i class="fas fa-sign-out-alt fa-lg"></i>
  </button>
</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

Having trouble getting CSS animation to work on Mozilla using mozAnimationName?

<style> @keyframes shake { 0% { -moz-transform:scale(0);opacity:0; } 25% { -moz-transform:scale(1.3);opacity:1; } 50% { -moz-transform:scale(0.7);opacity:1; } 75% { -moz-transform:scale(2); ...

Using Higher Order Components (HOC) in combination with Redux compose and Typescript

I am trying to leverage two Higher Order Components (HOC) with Redux compose, but the compiler is not generating the correct types. The Compose function is defined in the Redux source code here source code. To better understand how the code works, you ca ...

AngularJS Metronic sidebar sub menu collapses after clicking and does not stay open

Upon clicking the sidebar, a brief appearance of the submenu followed by its immediate disappearance seems to be the core issue at hand. The fact that Layout.js (slideUp func.) is triggered twice feels nonsensical I meticulously combed through the settin ...

Setting a fixed time for a preloader is a simple and effective way to

Is there a way to specify a specific duration for the preloader on my website? I find that after the images have preloaded, I am unable to see any animations in the home section. $(window).load(function() { $('#preloader').fadeOut( ...

Tips for transferring an object as a prop in React with the help of typescript

Learning TypeScript has been a challenge for me, as I struggle to grasp the concepts. Despite the countless times this question has been asked before, I find it difficult to apply the solutions I come across to my own code. To make matters worse, the synta ...

showcase every value upon submission in the form with options to edit and delete

Is it possible to display all values submitted in a form with edit and delete buttons? Currently, only one value is being displayed at a time. Whenever a new value is displayed, it replaces the old one. How can this be fixed? You can fin ...

Initiate a fetch request in React to an external API upon clicking a card

Just a quick question, I'm trying to implement a fetch request in my app that should only happen after clicking on a card. However, I'm having some issues as the fetch request is happening every time instead of just when clicked. useEffect(() ...

A reliable approach for dynamically altering SVG graphics

It seems like IE10 does not support CSS transformations for SVGs, only attribute transformations. Here is an example: <svg><rect id="myrect" width="200" height="200"></rect></svg> setTimeout(function() { var r = document.getE ...

Learn how to display array elements in an alternating layout using React

I am currently working with an array of objects that contain both content and image data. const items = [ { id: 1, content: "some content", img: "img_url" }, { id: 2, content: "so ...

New release of NextJS: server-exclusive framework

Are there methods available to define code in typescript files that will ensure they are never sent to the client? Certain frameworks, like svelte, offer this guarantee by using a specific naming convention with the suffix .server.ts. You can find more inf ...

Is there a way to incorporate an 'ended' feature into the .animate() function?

I'm currently working on a jQuery statement where I want to activate pointer events once the button animation is finished. However, I'm unsure how to incorporate an ended or complete function. Any suggestions on how to approach this? $('#ce ...

Reorder elements using CSS when they do not share the same immediate parent container

I am looking to rearrange the order of some items on my page using JS or CSS. The project I am working on is written with ReactJS. Here is the basic structure: <div class="parent"> <form> <div class="header"></di ...

The getStaticProps function in NextJS is failing to trigger

I've been attempting to make getStaticProps return data, but for some reason the function is not being called at all. Despite consulting the official documentation, examples, and various Stack Overflow answers, I still can't seem to get it workin ...

What is the process of adding background color to text?

Is there a more efficient way to style this text without relying on the span tag? I am looking to add a background color to my text. Code: .subject { color: gold; background: gray; } <h2>Some Subjects</h2> <ol> <li style="bac ...

What is the best way to ensure FireFox recognizes the accurate height of my divs?

Check out my code snippet on this fiddle http://jsfiddle.net/9m1Lba6u/ The code works perfectly on Google Chrome and Safari, displaying all numbers from 1 to the last one. However, when viewed in FireFox, it only shows numbers up until 99. Could there be ...

Leveraging the power of Material-UI and React, modify the appearance of the "carrot" icon within the

Currently implementing MUI's textfield based on the information found at this link: https://mui.com/components/text-fields/. While there are numerous resources available for changing the font of the input text, I have not come across any documentation ...

Using React Native to implement Firebase onSnapshot with FlatList pagination

INTRODUCTION I have a collection of items stored in FireStore with the "date" property. On the client side, I'm using a FlatList to display these items ordered by date, starting with the most recent item at the top. The challenge I'm facing is ...

Disabling the submission button in the absence of any input or data

I am currently experiencing an issue where the submit button is active and displays an error message when clicked without any data. I would like to rectify this situation by disabling the submit button until there is valid data input. < ...

Ensure that icons are evenly spaced, regardless of whether they display a notification badge or not

Although not as abundant as depicted in the image below, it is more evident that I am seeking a couple of font awesome icons overlaid with a bootstrap badge to display notifications. The two issues that I am encountering are: There is excessive spacing ...

When the page is refreshed in Firebase React js, the array data is rewritten

I am facing an issue with adding a list of topics to the firebase database. Each user should be able to create their own unique list of topics, but currently when I refresh the page and try to add another item, the entire list in the database gets erased ...