Alignment of text in a stationary element's location

I'm working on implementing a feature for an online shop where new items are added to the cart, and I want to display the number of new items added. However, I am facing a challenge in centering the text that displays the number, especially when the number can range from single digits to up to four digits.

https://i.sstatic.net/vFnAn.png

Currently, this is how it appears, but my goal is to have the text centered regardless of the number. I attempted to use fixed positioning on a div element and then center the text inside it, but this approach did not yield the desired result.

<div>
   <p id="shoppingCartDot">&#9679;</p>
   <span id="shoppingCartNumberDiv">
      <p id="shoppingCartNumber"></p>
   </span>
</div>

The above code snippet represents my HTML structure.

#shoppingCart{
  position: fixed;
  top: 0px;
  right: 0px;
  padding: 10px;
  padding-bottom: 4px;
  color:#ac8010;
  font-size: 40px;
}

#shoppingCart:hover{
  font-size: 41px;
}

#shoppingCartNumber{
  position: fixed;
  transform: translate(-170%, 0);
  color: #ac8010;
  font-size: 15px;
  -webkit-user-select: none; /* Safari */
  -ms-user-select: none; /* IE 10 and IE 11 */
  user-select: none; /* Standard syntax */
  cursor: pointer;
}

#shoppingCartNumberDiv{
  position: fixed;
  top: 4px;
  right: 10px;
}

#shoppingCartDot{
  position: fixed;
  top: -30px;
  right: 0px;
  color:#02029e;;
  font-size: 60px;
}

The CSS provided corresponds to styling the HTML elements.

If achieving the desired effect through the current method is not feasible, I am open to exploring alternative approaches to accomplish the intended outcome.

Answer №1

If you want to create a badge, simply use a div element and apply the CSS properties like display: flex, justify-content, and align-items to center it. You have the freedom to style it however you like.

#badge {
  height: 30px;
  width: 30px;
  padding: 2px;
  background: blue;
  border-radius: 100%;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div id='badge'>
  +99
</div>

Answer №2

Here is a demo for you to check:

.cart-div {
    position: relative;
    width: fit-content;
    height: fit-content;
}

.shopping-cart {
    font-size: 60px;
}

.number-dot {
    position: absolute;
    top: 0;
    right: 0;
    background-color: blue;
    border-radius: 100%;
    width: 40px;
    height: 40px;
    display: grid;
    place-items: center;
}
<div class="cart-div">
      <div class="shopping-cart">&#128722</div>
      <div class="number-dot">12</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

Creating a blur effect on an image tag when hovering using CSS and HTML

I am currently developing a Portal website for one of my classes, and the template provided by my professor utilizes the <picture> tag to adjust images depending on the browser's size. My goal is to add a blur effect and darken the image while d ...

Retrieve the HTML tag content as a string using jQuery

For my OSINT project, I am developing a tool that needs to extract text from a specific HTML code string. $("p").text(); I'm looking for a way to make my variable work with the above code. Any suggestions? Share your ideas! Solution: $.ajax({ ...

Using a single AJAX function to write on various tags

Information regarding the likes and dislikes count is retrieved from the server using an ajax function with the following queries: $q3="select count(value) as a from rank where personID='$person' and itemID='$itemID' and value>0 ...

A Guide to Connecting a JavaScript File to an HTML Page with Express and Node.js

Struggling with integrating my JavaScript file into my simple NodeJS app. Traditional methods like placing the script in the header doesn't seem to work with Node. I've attempted using sendFile and other approaches, but none have been successful ...

Retain the contents of the shopping cart even when the page is refreshed

For a course project, I am recreating a grocery store website and need assistance on how to retain the shopping cart values even after refreshing the webpage. Please inform me if more information is required... <button type="button" id= ...

What could be causing the issue of my navbar text not changing color using CSS?

Despite several attempts, I am unable to change the color of the hyperlinks in blue or purple using the HTML and CSS below. Can anyone point out what I might be missing? nav ul { /* Navbar unordered */ list-style: none; text-align: center; backgr ...

What is the best way to replicate a div's background color using an image within the body element?

I am looking to achieve a layout similar to the one below: https://i.sstatic.net/1cOYw.png The dark grey color represents the sidebar, but I want to use this color as a repeating vertical image in the body element without covering the footer (light gray) ...

Comparing the efficiency of Jquery draggable with thousands of elements versus updating the elements continuously

Currently, I am developing an application that involves dragging an image using Jquery's draggable utility. The image is accompanied by several overlay divs containing various components positioned based on pixel locations, sometimes reaching into the ...

Creating a three-row CSS layout where the middle row aligns to the right side

I am working on developing a mobile device layout with 3 blocks. The first and third blocks contain text fields, while the second block is filled with a map. However, all of my blocks don't look good when they are too wide. The browser window can have ...

Button with a Jquery icon design

Hello, I am currently working on implementing an icon button that can collapse and expand text. Although I have successfully implemented the logic for collapsing/expanding, I am facing difficulties in creating the actual icon button. The theme I am require ...

Delete the line-height property from the initial line

I have created text that is aligned using the "justify" option, and I would like to add some leading to the text. However, I want the first line of the text to remain at x:0px y:0px. Does anyone know how to remove the line-height from the first line of a ...

Discovering the background color of see-through HTML elements using Selenium

Looking to count the characters with a specific background color. I am currently traversing through all the HTML nodes using this method: Counting inner text letters of HTML element Example HTML Page: <span style="background-color: #ffff00;"> ...

I'm noticing that the top border for my span is smaller than the bottom one. What could

I am struggling to create an arrangement of three triangles in a line, with the first and third pointing up and the middle one pointing down. The upper triangle seems to be too small - any ideas on how to adjust this? Is there another way to achieve this ...

Display HTML content using JavaScript only when a checkbox is selected

Currently, I am updating an HTML form to show additional subsets of questions based on the selection of a "parent" checkbox. The current implementation works well, but I am wondering if there is a more efficient way to achieve this without having to rewrit ...

I am seeking advice on how to incorporate JavaScript to adjust slider values and add numerical values or prices. Any suggestions would

Seeking assistance with a unique project: I am currently developing a calculator that allows users to utilize sliders to select the best option for themselves across various categories. My experience with JavaScript is limited, so I decided to reach out h ...

Sending users to a different page in case the linked document from <a> cannot be located

I am in search of a way to set up an alternative link for a hyperlink in case the primary link does not exist, similar to how 'alt' works for images. I am aware of the existence of 'onerror' but it doesn't work for HTML links. I wo ...

Animating a div in jQuery by creating a duplicate

I've spent hours scouring Google and StackOverflow for a solution to this particular issue I'm facing, but haven't been able to find one. Here's the problem: I'm currently in the process of creating a shopping website. When a user ...

Displaying the servlet response within an iframe

This is the content of Content.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> &l ...

When selecting a new tab in HTML, the current page position remains unchanged. However, I would like the page to automatically scroll to the

In the design of my website, I have incorporated buttons that are linked to various pages using navigation tabs. However, when these buttons are clicked, the view maintains its position on the new page (e.g., halfway scrolled through the page). Instead o ...

Jquery solution for toggling multiple Divs individually

I'm currently working on a small application that has both a sidebar menu and a header menu. My goal is to have all items in these menus toggle the visibility of content in one main window or page. When a button is clicked, I want it to show the corre ...