2

I'm trying to show a percentage within a circle that is perfectly aligned in the center of the element. Cool, this works.

But, I want the numeric value aligned center while the "%" floats next to and doesn't affect the placement.

See codepen: https://codepen.io/brycesnyder/pen/MEwddv

div {
  font-family: sans-serif;
  height: 200px;
  width: 200px;
  background: #ae63e4;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
  line-height: 1;
  color: white;
  text-align: center;
}

div.percent:after {
  content: '%';
  font-size: 30px;
  margin-bottom: 15px;
}

div.a-percent {
  position: relative;
  background: #0ebeff;
}

div.a-percent:after {
  content: '%';
  position: absolute;
  font-size: 30px;
  margin-top: 5px;
}
<div class="percent">0</div>
<div class="percent">10</div>
<div class="percent">100</div>

<br>
<br>

<div class="a-percent">0</div>
<div class="a-percent">10</div>
<div class="a-percent">100</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
SNYDERHAUS
  • 421
  • 2
  • 8
  • 26

3 Answers3

1

Why not add an identical percent symbol on the left side of the number?

Then hide this duplicate with visibility: hidden.

This creates equal balance in the container and the number can be perfectly centered.

div {
  font-family: sans-serif;
  height: 200px;
  width: 200px;
  background: #ae63e4;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
  line-height: 1;
  color: white;
  text-align: center;
}
div::after {
  content: '%';
  font-size: 30px;
  margin-bottom: 15px;
}
div::before {
  content: '%';
  font-size: 30px;
  margin-bottom: 15px;
  visibility: hidden;
}
div.a-percent {
  background: #0ebeff;
}
<div class="percent">0</div>
<div class="percent">10</div>
<div class="percent">100</div>

<br>
<br>

<div class="a-percent">0</div>
<div class="a-percent">10</div>
<div class="a-percent">100</div>

More details and other options here: Center and right align flexbox elements

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
0

It seems I have managed to figure out how to keep the pacement by simply adding position absolute to the pseudo element. Previously, I had thought that this would cause the element to 0,0 it's placement.

div {
  height: 200px; width: 200px;
  background: maroon;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
  &.percent {
    &:after {
      content: '%';
      position: absolute;
  }
}
SNYDERHAUS
  • 421
  • 2
  • 8
  • 26
0

try this :

div {
    font-family: sans-serif;
    height: 200px; width: 200px;
    background: #ae63e4;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 60px;
    line-height: 1;
    color: white;
    text-align: center;
    &.percent {
        &:after {
            content: '%';
            font-size: 30px;
        }
    }
}
Zina Taklit
  • 89
  • 2
  • 11