What's the reason for my badge being an oval shape?

I am attempting to design a badge that hovers above my cart icon and shows the total number of items currently in the cart.

However, I have encountered an issue where the badge appears as an oval shape rather than a perfect circle. Can anyone assist with figuring out what mistake I may have made?

const cartStyle = {
  width: "48px",
  height: "48px",
  verticalAlign: "middle",
  float: "right",
  marginBottom: "0",
  backgroundImage: `url(${ShoppingCart})`
};

const badgeStyle = {
   content: "0",
   background: "white",
   display: "inline",
   position: "relative",
   borderRadius: "50%",
   width: "36px",
   height: "36px",
   padding: "8px",
   left: "15px",
   border: "2px solid #666",
   color: "#666",
   textAlign: "center"
}

class Cart extends Component {
    render() {
        return(
            <div className="cart" style={cartStyle}>
                <div style={badgeStyle} > { this.props.cartProducts.length } </div>
            </div>
        );
    }
}

https://codepen.io/stoerebink/pen/QvjYpo

Answer №1

To properly size your element, it is necessary to utilize inline-block display. Additionally, you can center the text using the line-height property:

 .bla {
  background: white; 
                   display: inline-block;
  position: relative; 
  border-radius: 50%; 
  width: 36px; 
  height: 36px; 
                   line-height:36px;
  padding: 8px; 
  left: 15px; 
  border: 2px solid black; 
  color: black; 
  text-align: center;
}
<div class="bla">0</div>

Answer №2

Using display: inline will make the element inline and its width will be based on its content. To set a specific width, switch to inline-block instead.

Answer №3

By setting the display property to inline, the div does not have the necessary properties to control its width and height. To create a circular shape, change the display property to inline-block. For vertical text alignment, set the line-height equal to the height, such as 36px in your case.

Answer №4

The reason for this is the content itself. When the font-size is adjusted, the badge appears in an oval shape. By setting the font-size to 0em, you can achieve a perfectly round circle.

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

Arrange the <form:radiobuttons> in a vertical position

I'm currently utilizing Spring MVC's <form:radiobuttons> to showcase radio buttons: <form:radiobuttons path="selection" items="${arraySelection}" /> The issue I am facing is that it is displaying the radio buttons in a horizontal la ...

I am currently working on a project that involves utilizing React and Node.js. However, I have been encountering difficulties in passing data to the database due to my implementation of MySQL

index.js import './App.css'; import {useState} from 'react'; import Axios from 'axios' function App() { const [password,setPassword] = useState(""); const [title,setTitle] = useState(""); const addPassw ...

Tips for Fixing npm Error Code ENOENT and syscall lstat

I am trying to set up the create-react-app typescript template and encountering this error: npm ERR! code ENOENT npm ERR! syscall lstat npm ERR! path C:\Users\m.namazi\AppData\Roaming\npm npm ERR! errno -4058 npm ERR! enoent ENOENT ...

Struggling to locate a route for the React styled components image

I'm having trouble locating the correct path for the image in my React styled components. I believe the path is correct, but could the issue be related to styled-components? Check it out here import styled from "styled-components"; export defaul ...

"Prisma vs. Supabase: A Comparison of Image Uploading

I am encountering an issue with adding image URLs to the Prisma database. I have successfully uploaded multiple images from an input file to Supabase storage, but when I try to add their URLs to the database, I receive an error regarding property compatibi ...

Could anyone assist me in activating code folding with react-codemirror2?

As I work on my web application, I've integrated react-codemirror2 to showcase JSON data. However, as the JSON files grow in size, navigating through the data becomes cumbersome. I am looking to introduce code folding to improve user experience, but I ...

Properly display a markup string within React components

I am currently facing a minor issue with my Hygraph CMS setup that is causing a slight roadblock. I have integrated React/Next JS for the front end, and typically, cases involving dangerouslySetInnerHTML or import parse from 'html-react-parser' s ...

Guide to setting up eslintrc and integrating React with airbnb and typescript without using the create react app command

My current project does not utilize create-react-app. I am in the process of updating my Eslint configuration. How can I properly add React to the eslintrc.js file? In the extend property, I currently have the following: extends: [ 'airbnb', ...

The cube mesh is failing to render inside the designated div

I created a Torus and Cube Mesh in my scene, but I want the cube to be positioned at the bottom right corner of the screen. Here's what I tried: <div style="position: absolute; bottom: 0px; right:0px; width: 100px; text-align: center; "> & ...

Tips for ensuring the pop-up submenu remains visible even when the cursor is outside the parent container and submenu during hover

Is there a way to keep the pop submenu visible even when the mouse hovers outside of its parent container or the submenu? Currently, if the mouse doesn't move straight down from the parent container (B) .account-settings-container to the top arrow of ...

Adaptive text sizing within Microsoft Outlook

I am facing a challenge in making the font size of my email signature responsive. Although VW seems to be the solution, it doesn't seem to work in Outlook. I have attempted using CSS, but it appears that inline CSS might be necessary. However, I am un ...

Moxios consistently providing empty responses in tandem with thunk and axios interactions

Having trouble with mocking my axios function, it keeps returning empty data: LoginAction: export function loginUser(email, password) { return function(dispatch) { return axios .post(`${API_URL}/users/authenticate`, { email, password }, { wit ...

Anticipated the presence of a corresponding <div> within the server's HTML nested in another <div>

I've encountered a troubling error on my website while using Next.js. The error is causing layout issues and upon investigation, it seems that the device detection hook is at fault. Here's the hook in question: const isBrowser = typeof window !== ...

Create a duplicate array with all distinct elements - JavaScript

Help needed: I am facing an issue with an array of objects. My goal is to create a unique copy of this array to prevent any changes in the original one when modifications are made to the new array. I have experimented with several methods, including: let ...

Guidelines for attaching a div to the bottom of a page?

I have a navigation menu inside div.wrapper, and I am trying to make the div.wrapper stick to the footer. <div class="wrapper"> <div id="menu"> <ul> <li>1.</li> <li>2.</li> ...

React and Redux error: Attempting to access properties of an undefined variable ('type')

While developing a redux reducer that manages user login and logout, I encountered an issue with the logout functionality. The login process is functioning properly, but upon attempting to logout, I received the following error: loginReducer.js:60 Uncaught ...

Implementing a Responsive Form on an Image Using Bootstrap

I am facing a challenge where I need to position a form on top of a Responsive Image. My goal is to ensure that the form is responsive as well and adjusts its size according to the image. As of now, the image is responsive but I am struggling to make the ...

Tips for keeping the options menu visible even when the video is paused

As I was creating my own customized Video player, I encountered an issue where I wanted the options bar/menu to disappear when the user became inactive. While I have managed to achieve this functionality, I still require the option bar to remain visible ...

Emails cannot display CSS styles

I'm experiencing a problem with my email messages not reading the CSS styles, regardless of whether I use inline CSS or not. I have tried everything but still can't figure out the issue. Can anyone assist me? $firstname = $row['firstname&ap ...

Is the dragging behavior of a rotated image different than that of the original image when using CSS rotation?

While working on a CSS grid to showcase images rotated at 60 degrees for a diagonal view, I encountered an issue. I wanted users to have the ability to drag and drop images within the grid, but when they drag an image, it moves as if it weren't rotate ...