Tips for improving text alignment within a circular shape on an HTML webpage using CSS

Struggling to position text inside a circular shape on my website, I've attempted the following:

https://i.sstatic.net/fKOks.jpg

From initial inspection, it seems like there could be a better solution. Do you have any suggestions for ensuring that "LM" or "TM" is perfectly centered within the circle?

Check out the fiddle here: Sample Demo

html:

.performance-box-container {
  margin: 5px;
  padding: 5px;
}

.performance-box {
  width: 120px;
  height: 60px;
  margin: 0px 5px 5px 0px;
  padding: 5px 5px 10px 5px;
  border: 2px solid #868686;
  background-color: rgba(202, 202, 202, 0.5);
  display: inline-block;
}

.performance-box-name-in-circle {
  height: 40px;
  width: 40px;
  border: 2px solid #000;
  background-color: #fff;
  border-radius: 50%;
  font-weight: bold;
  font-size: 12px;
  display: inline-block;
}

.performance-box-name-in-circle span {
  display: table-cell;
  vertical-align: middle;
  height: 40px;
  width: 40px;
  text-align: center;
  padding: 0;
  margin: 0;
}

.performance-box-stats {
  margin-left: 5px;
  font-size: 10px;
  font-weight: bold;
  display: inline-block;
}

.performance-box-stats p {
  line-height: 1.5;
  margin: 0;
}

.performance-box-stats span.positive {
  color: #388C00;
}

.performance-box-stats span.unchanged {
  color: #000;
}

.performance-box-stats span.negative {
  color: #f00;
}
<div class="performance-box-container">

  <div class="performance-box">
    <div class="performance-box-name-in-circle">
      <span>TM</span>
    </div>
    <div class="performance-box-stats">
      <p>DTD: <span class="positive">+3.5%</span></p>
      <p>MTD: <span class="unchanged">+0.0%</span></p>
      <p>YTD: <span class="negative">-4.5%</span></p>
    </div>
  </div>

  <div class="performance-box">
    <div class="performance-box-name-in-circle">
      <span>LM</span>
    </div>
    <div class="performance-box-stats">
      <p>DTD: <span class="positive">+3.5%</span></p>
      <p>MTD: <span class="unchanged">+0.0%</span></p>
      <p>YTD: <span class="negative">-4.5%</span></p>
    </div>
  </div>

</div>

Answer №1

To center align items both vertically and horizontally, you can utilize flexbox

display: flex;
justify-content: center;
align-items: center;

.example-box,
.example-box span {
    display: flex;
    justify-content: center;
    align-items: center;
}

.example-box {
    height: 40px;
    width: 40px;
    border: 2px solid #000;
    background-color: #fff;
    border-radius: 50%;
    font-weight: bold;
    font-size: 12px;
}
<div class="example-box">
  <span>AB</span>
</div>

For a demonstration, refer to this Updated JSFiddle

Answer №2

Utilized the power of flexbox to showcase performance-box-name-in-circle and performance-box-stats alongside each other. Additionally, employed the following CSS to position the span in the center of the circle. Trust this explanation is beneficial. Thank you.

margin: 50%;
display: inline-block;
transform: translate(-50%, -50%);

.performance-box-container {
  margin: 5px;
  padding: 5px;
  display: flex;
}

.performance-box {
  width: 120px;
  /* height: 60px; */
  margin: 0px 5px 5px 0px;
  padding: 5px 5px 10px 5px;
  border: 2px solid #868686;
  background-color: rgba(202, 202, 202, 0.5);
  /* display: inline-block; */
  display: flex;
  justify-content: center;
  align-items: center;
}

.performance-box-name-in-circle {
  height: 40px;
  width: 40px;
  border: 2px solid #000;
  background-color: #fff;
  border-radius: 50%;
  font-weight: bold;
  font-size: 12px;
  display: inline-block;
}

.performance-box-name-in-circle span {
  display: table-cell;
  */
  /* vertical-align: middle; */
  /* height: 40px; */
  /* width: 40px; */
  /* text-align: center; */
  padding: 0;
  margin: 0;
  margin: 50%;
  /* margin-top: 80px; */
  display: inline-block;
  transform: translate(-50%, -50%);
}

.performance-box-stats {
  margin-left: 5px;
  font-size: 10px;
  font-weight: bold;
  display: inline-block;
}

.performance-box-stats p {
  line-height: 1.5;
  margin: 0;
}

.performance-box-stats span.positive {
  color: #388C00;
}

.performance-box-stats span.unchanged {
  color: #000;
}

.performance-box-stats span.negative {
  color: #f00;
}
<body>

  <div class="performance-box-container">

    <div class="performance-box">
      <div class="performance-box-name-in-circle">
        <span>TM</span>
      </div>
      <div class="performance-box-stats">
        <p>DTD: <span class="positive">+3.5%</span></p>
        <p>MTD: <span class="unchanged">+0.0%</span></p>
        <p>YTD: <span class="negative">-4.5%</span></p>
      </div>
    </div>

    <div class="performance-box">
      <div class="performance-box-name-in-circle">
        <span>LM</span>
      </div>
      <div class="performance-box-stats">
        <p>DTD: <span class="positive">+3.5%</span></p>
        <p>MTD: <span class="unchanged">+0.0%</span></p>
        <p>YTD: <span class="negative">-4.5%</span></p>
      </div>
    </div>

  </div>

</body>

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

Encountering a 404 error while loading CSS in a Django application

I'm currently in the process of developing a chatbot application. I'm encountering issues with loading the CSS for the frontend using django's static data load feature. Here are the file paths: CSS path: C:\Documents\Django_Works ...

Tips for updating the background color of your navigation bar:

I've been using bootstrap and I want to customize the active page background color to green, but for some reason it doesn't seem to be working. .navbar-inverse { color: #fff; background-color: #4CAF50; } <nav class="navbar navbar-invers ...

The div is identified by an ID and a class, but the class is not being utilized by the div

Can someone please explain why the font size, background color, and font weight are not applying to the content in the "welcome" class in this simple HTML/CSS code below? The DIV has both an ID and a class, yet the styles are not being reflected. Any insi ...

Displaying a pop-up window upon clicking on an item in the list view

After creating a list where clicking an item triggers a popup, issues arose when testing on small mobile screens. Many users found themselves accidentally tapping buttons in the popup while trying to view it. What is the most effective way to temporarily ...

The use of JQuery insertAfter function in Chrome and Safari results in the content being inserted as a string representation

Currently, I am utilizing jQuery to dynamically insert rows into a table. The row itself acts as a text template, with certain elements being replaced before being converted into a jQuery DOM node. This process works smoothly in Firefox and IE: jQuery(jQu ...

Dynamic HTML binding with v-model in Vue JS allows for seamless two-way

I recently incorporated Vue Js into my main MVC application. I want to be able to bind a Vue Js v-model to dynamically loaded HTML from a partial view. For example, in my partial view, I have the following: <input type="text" id="firstNam ...

Evolution of the material through a fresh new slide

Can someone assist me with an animation issue? I have a slideshow consisting of 4 images that are supposed to transition automatically after a set time interval. Upon initially loading the webpage, the animation works perfectly as intended. However, subs ...

Creating a rectangle link that extends beyond its containing div

On our website, there is a button that we are trying to turn into a link without the tape on top of it. The issue we're facing is that creating the link also affects the empty space in the top left corner of the tape graphic. Our Attempts We attemp ...

Numerous requests for .js files originating from a local server

I am currently working on an HTML project where I need to link multiple .js files from my local computer. Below is the structure of my folders: Documents/ ├── js/ | └── .js files └── index.html Within my HTML file, I have included the ...

Operating my application on a 2G network

Overview of my mobile application: I have created a simple HTML/CSS/JavaScript-jQuery app using PHONEGAP. The app utilizes a WCFService on a server for database communication, which is accessed through AJAX calls. After building and testing the applicati ...

The Bootstrap Tooltip seems to be glued in place

Utilizing jQuery, I am dynamically generating a div that includes add and close buttons. Bootstrap tooltips are applied to these buttons for additional functionality. However, a problem arises where the tooltip for the first add button remains visible even ...

Customizing CSS for displayed MDBootrap components

I am currently using MDBoostrap, a styling tool similar to Bootstrap, in my React application. I have a select element that is coded like so: <div> <select id="Regions" style={errorSelect} value={this.state.userRegionId} onChange={this.handle ...

Removing values when disabling a button using JavaScript

Within my code, I have two tables - one for non-clickable teams and another for clickable matches. Check out the image of the tables View the code on my GitHub repository let teams = [ {name: 'Roma', points: 0, wins: 0, draws: 0, loses: ...

Unable to scroll further, however, an iframe is overflowing

After adding an iframe and various tags on the page to enable scrolling, I was hopeful that it would work. Unfortunately, it did not. You can view the HTML and CSS of the page here: What steps should I take next? ...

What is the method for including a TabIndex property in a JSON file?

https://i.sstatic.net/ISi72.png I discussed the integration of HTML fields within a JSON file and highlighted how to utilize the TabIndex property effectively in JSON files. ...

The correlation between dynamic pricing and surge pricing in relation to PX

I'm exploring the translation of measurements from dp and sp to pixels within Google's new material design for a website I'm working on. Usually, my web designs have used pixel-based measurements for both grid and font sizing over the four y ...

How to eliminate vertical spacing between rows in Bootstrap?

I am looking to eliminate the unsightly vertical spacing between the bootstrap rows displayed below: https://i.sstatic.net/dgR2z.png The code appears as follows: <div class="outerbox container-fluid vw-100"> <div class="row vw-100 justify-co ...

Enhance Your Website with jQuery Loading Using Bootstrap's Less Features

Hey everyone! So I had a question a few days back that I couldn't find the answer to: why was the content appearing weird and not formatted correctly when I used jQuery's .load() function to load another .HTML page from my directory? The strange ...

Can a URL be substituted for a symlink in a Perl script?

After spending two days re-templating a perl script, I've managed to make it look very nice. However, I have encountered an issue with the calendar feature, which is written in perl. My main concern is preventing users from discovering the actual fold ...

scrollbar is shy and not visible

I don't use CSS often, but I attempted to create a scrollable area with the following code. However, it seems to only hide the text that doesn't fit without providing a scrolling option. This behavior is consistent across Chrome, IE, and Firefox, ...