Place numbers above the image in the top right corner or at the top of the image

I'm struggling to align an image with a number that updates on page load. I want the number to either be on top of the image or in the northeast corner (top right), but so far I can't get it to line up correctly. The best I've achieved is having them spaced apart to the right.

I've checked out examples on Stack Overflow: CSS : Float a number value on the right top corner of an image How to put the number at top right corner of cart icon?

Here's what I have based on the second link above: https://i.sstatic.net/ZrAAs.png

This is my current HTML code:

<li class="nav-item">
     <i class="fa fa-shopping-cart"></i>
     <a href="Cart1.aspx" class="icon-shopping-cart" style="font-size: 25px">
     <label ID="lblCartCount" ForeColor="White"/><img alt="basket" height="30" src="images/basket.png" />3</a>
</li>

Styles:

    <style>

.header{background:rgb(0, 178, 255);color:#fff;}
#lblCartCount {
    font-size: 12px;
    background: #ff0000;
    color: #fff;
    padding: 0 5px;
    vertical-align: top;
}

.bell {
    display: inline-block;
    position: relative;
    width:64px;
}

.bellnumbers {
    position: absolute;
    font-size:12px;
    background-color:red;
    width:14px;
    height:14px;
    color:#fff;
    top: -4px;
    right: -4px;
}
.badge {
  padding-left: 9px;
  padding-right: 9px;
  -webkit-border-radius: 9px;
  -moz-border-radius: 9px;
  border-radius: 9px;
}

.label-warning[href],
.badge-warning[href] {
  background-color: #c67605;
}
#lblCartCount {
    font-size: 12px;
    background: #ff0000;
    color: #fff;
    padding: 0 5px;
    vertical-align: top;
    margin-left: -10px; 
}
</style>


I prefer the transparent number color over a solid background color like in the first example.

I also tried this approach before:
<li class="nav-item">
                            
                            <div class="cart update">       
                                <a class="nav-link" id="cart-link" th:href="@{/cart}" ><img alt="basket" height="30" src="images/basket.png" />
                                <span class="badge badge-warning" id="lblCartCount"><i class="fa" style="font-size:12px">[[${cartItemsSize}]]</i></span></a>
                            </div>
                    
                        </li>

Answer №1

Combine the <img> and the <span> within a div. Then, assign the div the following property and values: position: relative; and width: min-content;. The purpose of the former is to provide an anchor/border for a child element with position: absolute;. The latter ensures that the div only occupies space equivalent to the image's dimensions rather than spanning the entire width of the website.

Lastly, apply the properties and values

position: absolute; top: x; right: y;
to the span element, setting x and y as desired distances from the image border.

.image-wrapper {
  position: relative;
  width: min-content;
}

.image-wrapper span {
  position: absolute;
  top: 5px;
  right: 5px;
}
<div class="image-wrapper">
  <img src="https://via.placeholder.com/150.jpg">
  <span>5</span>
</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

Steps to implement an image zoom function triggered by a button click

I'm working on a school project that requires me to use only html, css, and javascript for creating a website. Currently, I'm designing a landing page with a button that will navigate the user to another page. My goal is to have the background im ...

Updating CSS in response to a button click using JavaScript: A step-by-step guide

Currently, I am in the process of creating a basic website with bootstrap5 (no JS at the moment). One issue I am facing is when I shrink the screen to the point where the navigation bar transforms into a burger button. Whenever I click on the burger button ...

Looking for a simple method to display a popover using conditional if/else statements?

On my website, I am utilizing JavaScript to determine the user's Facebook status. This includes checking if the user is already logged in and has allowed our app, if they are logged in but haven't authorized our application, or if they are not lo ...

PHP Newsletter Creator

Recently, I created a unique php newsletter script in CakePHP that allows users to utilize an in-place editor to generate HTML content for newsletters. While the functionality works well, I have encountered some challenges with a new newsletter design that ...

Tips for expanding Bootstrap 5 text area within tabbed interfaces

I am struggling to properly position a text area within a tab and have it stretch both horizontally and vertically within the card body. Despite trying to use d-flex and align-self-stretch, the tab content div does not seem to respond as expected. My att ...

I am encountering a horizontal scroll bar despite setting the width to 100%

I am just starting out as a beginner in web development. I decided to create a webpage to practice my HTML and CSS skills. However, when I set the width of all elements to 100%, I noticed that I am getting a horizontal scroll bar. I have tried troubleshoot ...

Determining the appropriate size of a carousel based on the viewport dimensions in Bootstrap 4.1.0

Objectives: Adjust the size of the carousel to be 60% x 60% of the viewport. Ensure that all images are the same size as the slideshow. Create a fluid carousel layout. I am struggling to implement these changes. Does anyone have any suggestions? < ...

"Exploring the best locations for storing CSS, JS, and image files in Spring

Having trouble figuring out where to place my public files. I attempted the solution mentioned in this thread on Stack Overflow but it didn't work for me. I've tried putting my public folder in various locations within the WEB-INF directory, but ...

Learn the step-by-step process of cropping and zooming an image using the react-image-crop

I am looking to implement zooming and cropping functionality for an image using react-image-crop instead of react-easy-crop. Currently, the cropping does not take into account the zoom level set by ReactCrop's scale property. I want to be able to zo ...

Using jQuery's .post() method to automatically scroll to the top of

Is there a way to prevent the automatic scrolling to the top of the page when executing the buy function? Here is the code snippet involved: <a href="#" onClick="buy(id)">Buy</a> The buy function utilizes the $.post() function for processing t ...

In Internet Explorer, auto margins in Flexbox do not function properly when using justify-content: center

My table has cells that can contain both icons and text, with the icons appearing to the left of the text. There are different alignment scenarios to consider: Only an icon is present: The icon should be centered Only text is present: The text should be ...

Can you guide me in inserting space between two images along with headers and paragraphs utilizing bootstrap?

Trying to recreate a portfolio design from Codewell for practice, but struggling to create space between images without them stretching and the text disappearing. Even using display: flex and justify-content: space-between ends up stretching the images. I ...

Angular will move the input field cursor to the beginning after typing 4 digits

I'm looking for some guidance with a specific scenario involving a reactive form input field. The field is meant to capture the last four digits of a Social Security Number (SSN), and upon filling it out, an API call is triggered to validate the enter ...

I'm looking to conceal the carousel specifically on mobile devices

Is it possible to hide a Bootstrap 4 carousel on mobile devices? If so, how can I achieve this? <!--Carousel Wrapper--> <div id="carousel-example-3" class="carousel carousel-fade white-text" data-ride="carousel" data-interval="false"> < ...

Striving to generate 20 rows using the select type, and then add them in. (Combining jQuery, Handlebars, and HTML)

With the help of fellow stack users, I was able to figure out how to display 20 DIVs using jQuery. Now, my goal is to customize the second div among the created ones by implementing a "select" style with additional jQuery code to append the "option" flags. ...

Iterating through a set of HTML elements and making updates using a forEach loop

I have encountered an issue while attempting to update a group of HTML elements within a forEach loop. Initially, everything appears to be functioning correctly up to section 3. However, upon adding a new section, the problem arises where the navigation ba ...

Having trouble positioning the Slick Carousel in the middle of the page

Having an issue with centering the Slick Carousel horizontally on my page, can you help? HTML: <!-- logo slider --> <section id="customer-carousel" class="slider responsive container m-5"> <div> <img src=&qu ...

Automatically Modify Uploaded Files on the Server

There is one thing that has caught my attention. I am interested in working with scripts that can automatically edit a file once it's been uploaded. For example, imagine having a form where someone uploads an HTML file. The script would then edit spe ...

Ways to conceal a particular tab when switching to a different tab

My goal is to have a modal display a date/time set by another party when opened by the user. However, I also want to provide an option for the user to propose a new time in the footer so that they can request a different meeting time from the other party ( ...

Greetings, username, following successful login

Upon first accessing the system, the user will be prompted to log in. If the login is successful, the user will be redirected to the index.php page; if not, they will be asked to re-enter their credentials. My goal is to have the username displayed on the ...