1

CodeSandbox:
https://codesandbox.io/s/eloquent-haibt-1bnib?file=/src/main.js

enter image description here

I want to center the - text in the button, but I cannot find a way to do it.

html

  <button class="round-button align-middle mr-1">
    <span>-</span>
  </button>

css

.round-button {
  min-width: 20px;
  max-height: 20px;
  text-decoration: none;
  display: inline-block;
  outline: none;
  cursor: pointer;
  border-style: none;
  color: white;
  background-color: #3498db;
  border-radius: 100%;
  overflow: none;
  text-align: center;
  padding: 0;
}
.round-button:before {
  content: "";
  display: inline-block;
  vertical-align: middle;
}
CCCC
  • 5,665
  • 4
  • 41
  • 88

5 Answers5

2

html

<button class="round-button align-middle mr-1">-</button>

css

.round-button {
  min-width: 20px;
  height: 20px;
  border-style: none;
  color: white;
  background-color: #3498db;
  border-radius: 50%;
  text-align: center;
  padding: 0;
  line-height: 20px; // to center text vertically
}

You just need to add the same line-height as your button's height and don't need an extra span element to add text. I've also removed unnecessary styles.

Rahul K
  • 888
  • 5
  • 16
1

Try setting line-height: 20px to that. If it still looks off, you might be using a custom font with non-standard line height. In this case play with the line-height property until it looks okay.

Atanas Atanasov
  • 506
  • 7
  • 11
0

Add the following style properties to .round-button:

.round-button {
  display: flex;
  align-items: center;
  justify-content: center;
}

And, remove style for .round-button:before.

Praveen
  • 187
  • 6
0

Try this.

.round-button {
  background-color: #3498db;
  border-style: none;
  border-radius: 100%;
  color: #fff;
  cursor: pointer;
  
  aspect-ratio: 1 / 1;
  width: 48px;
  
  display: flex;
  align-items: center;
  justify-content: center;
}
  <button class="round-button">
    <span>-</span>
  </button>
naveen
  • 53,448
  • 46
  • 161
  • 251
-1

Try changing <span>-</span> to <span style="position:relative; left:0px; top:-3px">-</span>. If it doesn't look right you can play around with it.

Ellen
  • 1
  • 1