Guide to placing a badge above an icon

I've been trying to place a badge over a fontawesome icon, but I'm having trouble aligning it properly. No matter what I do, it always seems to end up either above or below the icon. My goal is to have the badge shown directly on top of the icon.

If anyone can provide some insight into what I might be overlooking, I would greatly appreciate it.

Thank You!

@import url('//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css');
.menutoggle {
    float: left;
    width: 50px;
    height: 52px;
    font-size: 22px;
    cursor: pointer;
    color: #1d2939;
    border-right: 1px solid #eee;
    border-left: 1px solid #eee;
    -moz-transition: all 0.2s ease-out 0s;
    -webkit-transition: all 0.2s ease-out 0s;
    transition: all 0.2s ease-out 0s;
}

.menutoggle i {
    padding:15px;
  padding-bottom:0px;
}

.menutoggle:hover {
    color: #1d2939;
    background-color: #f7f7f7;
}

.badge{
  display:inline-block;
  min-width:10px;
  padding:3px 7px;
  font-size:12px;
  font-weight:700;
  line-height:1;
  color:#fff;
  text-align:center;
  white-space:nowrap;
  vertical-align:baseline;
  background-color:#777;
  border-radius:10px
}
<ul>
        <li>
           <div class="menutoggle"> 
               <i class="fa fa-cog"></i>
             <span class="badge">1</span>
           </div>
        </li>
</ul>

Answer №1

It is important to position badge elements relative to their parent container menutoggle. To achieve this, apply position: relative; to .menutoggle and use position: absolute; for badges with specific top and left/right values.

@import url('//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css');
.menutoggle {
    position: relative;
    float: left;
    width: 50px;
    height: 52px;
    font-size: 22px;
    cursor: pointer;
    color: #1d2939;
    border-right: 1px solid #eee;
    border-left: 1px solid #eee;
    -moz-transition: all 0.2s ease-out 0s;
    -webkit-transition: all 0.2s ease-out 0s;
    transition: all 0.2s ease-out 0s;
}
.menutoggle i {
    padding:15px;
    padding-bottom:0px;
}
.menutoggle:hover {
    color: #1d2939;
    background-color: #f7f7f7;
}
.badge {
    position: absolute;
    top: 0;
    right: 0;
    display:inline-block;
    min-width:10px;
    padding:3px 7px;
    font-size:12px;
    font-weight:700;
    line-height:1;
    color:#fff;
    text-align:center;
    white-space:nowrap;
    vertical-align:baseline;
    background-color:#777;
    border-radius:10px;
}
<ul>
    <li>
        <div class="menutoggle"> 
            <i class="fa fa-cog"></i>
            <span class="badge">1</span>
        </div>
    </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

Using AJAX to upload images to a MySQL database with PHP

I'm interested in experimenting with uploading an image using PHP and MySQL. My approach involves using a form to send data via AJAX. Here is my HTML code: <input type="file" name="logo" id="logo" class="styled"> <textarea rows="5" cols="5" ...

Is the imported style file not properly scoped?

Whenever I attempt to import a CSS file using this method, the styling is not properly scoped. Is it necessary to write the styles within a style tag for them to work correctly? I have been experimenting with this in Vue-cli. <style scoped> @im ...

Having trouble locating the nivoslider IE8 jQuery bug, can't seem to track it

I am encountering an issue with the nivoslider script in IE8 and I am having trouble identifying what is causing it. The nivoslider works perfectly fine in all other browsers except for IE8. Any help or guidance on this matter would be greatly appreciated ...

Error encountered while attempting to save my document as a PDF due to a CSS issue

I recently created an HTML page to display data from a MySQL database, and I'm pleased to say that everything is functioning correctly. body { width: 100%; height: 100%; margin: 0; padding: 0; background-color: #F0F0F0; font: ...

What is the best way to access the second div element from a request?

This is an example of HTML code: <div class="f_fs13"> <div class="bg_blue">first</div> <div>range 5</div> <div> Here is the code I wrot ...

Aligning text and lists using Bootstrap on both desktop and mobile devices

I want to format a text with a list similar to the images provided https://i.stack.imgur.com/6giDH.png for desktop and tablet view, but for mobile display I would like it to look like this https://i.stack.imgur.com/7zSXi.png This is my current approach ...

javascript - Modify the text color of the final word entered

I am currently working on a text editor project and one of the requirements is to change the font color of the last word typed based on whether it is a keyword or not. Despite trying various solutions, I have yet to find one that works as intended. Here is ...

Local storage behaving unexpectedly with HTML inputs

CODE: $(document).ready(function () { window.levelsSaved = 1; $('#button1').click() { levelsSaved = [1, 2] } function checkLevels() { localStorage.savedLevels = JSON.stringify(levelsSaved); if (localStorage.savedLevels == [1, 2] ...

Is it possible to modify the icon within a toggle button?

I'm facing a problem with a button that contains an icon, and I'm using jQuery to toggle the visibility of a div. The issue is that I can only switch the icon from plus to minus by clicking directly on the icon, rather than anywhere on the butto ...

What are some strategies for increasing efficiency in my drag and drop process?

Update #2 Wow, transition was stuck at .35s. My CSS just wasn't updating properly :( UPDATE: Anyone know if requestAnimationFrame could help with this? I've got a rotating image reel and I want users to be able to swipe left or right to switch ...

Jquery ripple effect does not function properly with background-attachment: fixed style

Is there a way to make the effect ripples () work with background-attachment: fixed? I'm trying to achieve this effect while keeping the background fixed in place. Any suggestions on how to make this work? background-attachment: fixed; ...

I am having trouble aligning these elements next to each other

Can anyone help me figure out why these elements won't center on the page? <ul id="contact"> <h1> Contact </h1> <p> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2e6d0c7c3cfd1d6cdd5cac7c7ced1 ...

The div element is not extending all the way to the left side of the browser window

I'm having trouble positioning a menu at the top of my webpage following a tutorial. The menu icon doesn't seem to reach all the way to the left side of the page. Despite multiple attempts to adjust the margin and padding settings, I can't ...

The div on the right side is constantly adjusting its height, while the div on the left remains at a fixed height. They need to be

In the PHP code provided below, you will find the script responsible for generating the dynamic content on the right side of the page. The section tag and its associated content are dynamically created within this script. while($row = mysqli_fetch_object( ...

Engage in a sequence of actions by tapping and retapping on a div element

Is there a way to toggle the color of an element from black to orange with a single click and back again with another click? Additionally, I have set up a hover effect in CSS that changes the color to orange when hovered over. In my current code, the elem ...

Guide to creating a PHP loop in the footer of a Magento website

Hey Developers! I have been using the following commands in footer.phtml to retrieve all my cms/blocks in the Magento's footer. <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('home')->toHtml() ...

Displaying image issues persist on Safari browsers for Mac and iPhone

I am currently working on a website and facing an issue with the responsive image display on Safari for Mac/iPhone. Here is the code snippet I have been using: <head> <meta charset="utf-8"> <meta http-equiv="Content-Language" co ...

Utilizing Media Queries with Dynamic Vue Properties

On one of my website pages, I always have a Div element with a Background Image that changes based on Media Queries (for example, I fetch a smaller resolution from my CDN on mobile phones). However, since I retrieve the Image URL on Page Load, I need to s ...

How can I center two divs within a wrapper div without also centering all the text inside?

Is there a method other than using display: inline-block and text-align:center that allows us to center two divs inside a wrapper div? I prefer not to have my text centered. ...

What method can be employed to eliminate choice selection lacking any value?

Currently, I am encountering difficulties in my attempt to remove or hide the first option value from a ransack code. Would you be able to assist me in addressing this concern? Here is the HTML CODE that I am working with: <select id="q_c_0_a_0_name" ...