Home > Back-end >  Center content being pushed aside by :before
Center content being pushed aside by :before

Time:10-13

Run the snippet below... the two digits in Orange are not centered in their fields. They get offset when I add the :before content. How can I elegantly get the digit to center again (preferably without using magic numbers).

.container {
  background: #ededed;
  height: 4rem;
  display: flex;
  align-items: center;
  justify-content: center;
 }
 
.number {
  display: flex;
  flex-direction: row;
}

.digit:nth-last-of-type(3n):before {
  content: ",";
  right: 0.75em;
  position: relative;
  color: black;
  font-weight: bold;
 }
 
 .digit:nth-last-of-type(3n) {
  background: orange;
  }
 
 .digit {
  width: 1em;
  text-align: center;
  padding: 0.25em;
  margin: 0.5em;
  border-radius: 0.1em;
  background: white;
  color: black;
 }
<div class="container">
  <div class="number">
    <span class="digit">1</span>
    <span class="digit">9</span>
    <span class="digit">0</span>
    <span class="digit">1</span>
    <span class="digit">0</span>
    <span class="digit">0</span>
    <span class="digit">0</span>    
  </div>
</div>

CodePudding user response:

You can make :before absolute and digit relative to achieve this.

.container {
  background: #ededed;
  height: 4rem;
  display: flex;
  align-items: center;
  justify-content: center;
 }
 
.number {
  display: flex;
  flex-direction: row;
}

.digit:nth-last-of-type(3n):before {
  content: ",";
  left: -10px;
  position: absolute;
  color: black;
  font-weight: bold;
 }
 
 .digit:nth-last-of-type(3n) {
  background: orange;
  }
 
 .digit {
  width: 1em;
  text-align: center;
  padding: 0.25em;
  margin: 0.5em;
  border-radius: 0.1em;
  background: white;
  color: black;
position:relative;
 }
<div class="container">
  <div class="number">
    <span class="digit">1</span>
    <span class="digit">9</span>
    <span class="digit">0</span>
    <span class="digit">1</span>
    <span class="digit">0</span>
    <span class="digit">0</span>
    <span class="digit">0</span>    
  </div>
</div>

  • Related