2

I have a border-radius circling a letter grade but not all of the letters appear centered within their border radius. For example the letter D looks the worst:

enter image description here

Does anyone know of a way to make sure the text inside of the border radius always appears centered? (This app uses react if it matters.)

Here is the css for it:

const buttonStyle = {
  width: 40,
  height: 40,
  borderRadius: 100,
  borderWidth: 2,
  borderColor: stylesColors.grey,
  padding: 0,
  marginRight: 15,
  fontSize: 27,
  textAlign: 'center',
  verticalAlign: 'middle',
  display: 'table-cell'
};

And the html though I doubt it matters much:

grades.map((grade, index) => {
  return (
    <Track key={index}>
      <Button
        className={(grade.letter === value.grade) ? 'green-button' : null}
        style={buttonStyle}
        id={`reviews--rating-${ratingTypeName}--${grade.letter}`}
        key={index}
        data-track-autopopulated={false}
        data-track-rating-type={`${ratingTypeName.toLowerCase()}Grade`}
        data-track-selected-grade={grade.letter}
        data-track-draft-review-id={draftReviewId}
        data-track-service-provider-id={serviceProviderId}
        onClick={() => this.handleSelect(grade.letter)}>
        {grade.letter}
      </Button>
    </Track>
  );
})
FairyQueen
  • 2,283
  • 7
  • 37
  • 57
  • 1
    I wonder if this is related to the font? When I convert your code to html/css (https://jsfiddle.net/rcn6oue0/1/) and use the default system font on macOS, most of the characters look centred except for D which is slightly off - though less so than your screen shot. – Turnip May 03 '19 at 13:43
  • Possible duplicate of [Vertically and horizontally centering text in circle in CSS (like iphone notification badge)](https://stackoverflow.com/questions/4801181/vertically-and-horizontally-centering-text-in-circle-in-css-like-iphone-notific) – Mosè Raguzzini May 03 '19 at 14:31
  • Aren't your attributes there incorrect syntax for react? I'm probably wrong, but I've never seen hyphens used to set props before. – gilbert-v May 03 '19 at 15:07

2 Answers2

3

There are some issues in your css/scaffolding as isolating the snipped results in correctly centered items:

 .centerRound{
  width: 40px;
  height: 40px;
  border-radius: 100%;
  border: 2px solid grey;
  padding: 0px;
  margin-right: 15;
  font-size: 27;
  text-align: center;
  vertical-align: middle;
  display: table-cell;
  font-size:35px;
  }
  
<div class="centerRound">A</div>
<div class="centerRound">B</div>
<div class="centerRound">C</div>
<div class="centerRound">D</div>
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
2

is this what you want?

div {
  height: 30px;
  width: 30px;
  border: 1px solid black;
  border-radius: 50%;
  display: inline-block;
}

span {
  display: inline-block;
  margin-top: 50%;
  margin-left: 50%;
  transform: translate(-50%, -50%);
}
<div>
  <span>
  A</span>
</div>

<div>
  <span>
  B</span>
</div>

<div>
  <span>
  C</span>
</div>

<div>
  <span>
  D</span>
</div>
Turnip
  • 35,836
  • 15
  • 89
  • 111
Xenio Gracias
  • 2,728
  • 1
  • 9
  • 16