10

I want to create a button circle link with text inside it but I have problem centering the text inside the circle button. The line height is too large. Any suggestion to this problem?

Here's the code: https://jsfiddle.net/hma443rL/

.btn-donate {
  background: #97c83e;
  text-align: center;
  width: 149px;
  height: 148px;
  border-radius: 100%;
  display: inline-block;
  font-size: 35px;
  line-height: 2.3;
  vertical-align:middle;
  color: white;
  font-weight: bold;
  text-decoration: none
}
<a href="" class="btn btn-donate">
  Donate <span>Us</span>
</a>

I'm trying to create a button like this

enter image description here

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
antonscie
  • 149
  • 1
  • 11

4 Answers4

15

Flexbox can do that:

.btn-donate {
  background: #97c83e;
  text-align: center;
  width: 149px;
  height: 149px;
  border-radius: 100%;
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-size: 35px;
  color: white;
  font-weight: bold;
  text-decoration: none
}
<a href="" class="btn btn-donate">Donate <span>Here</span></a>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
2

Use inline-blocks to line them up vertically instead of using line-height like here.

I have moved the full text inside the span in the markup

snippet below:

.btn-donate {
  background: #97c83e;
  text-align: center;
  width: 149px;
  height: 148px;
  border-radius: 100%;
  display: inline-block;
  font-size: 35px;
  vertical-align: middle;
  color: white;
  font-weight: bold;
  text-decoration: none
}
a.btn.btn-donate span {
  display: inline-block;
  vertical-align: middle;
}
a.btn.btn-donate:after {
  content: '';
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}
<a href="" class="btn btn-donate"><span>Donate Us</span></a>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
1

If you add another element to your markup you can centre using a combination of relative positions and transform

.btn-donate {
  background: #97c83e;
  text-align: center;
  width: 149px;
  height: 148px;
  border-radius: 100%;
  display: inline-block;
  font-size: 35px;
  vertical-align: middle;
  color: white;
  font-weight: bold;
  text-decoration: none
}
.wrapper {
  display: block;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<a href="" class="btn btn-donate">
  <span class="wrapper">Donate <span>Us</span></span>
</a>
Turnip
  • 35,836
  • 15
  • 89
  • 111
0

I'm usually partial to using table display properties, but I believe it would suit your requirements here just fine. It requires very minimal adjustments to style and markup.

.btn-donate span {
    vertical-align: middle;
    display: table-cell;
}

.btn-donate {
    background: #97c83e;
    text-align: center;
    width: 149px;
    height: 148px;
    border-radius: 100%;
    display: table;
    font-size: 35px;
    vertical-align: middle;
    color: white;
    font-weight: bold;
    text-decoration: none;
}
<a href=""class="btn btn-donate"><span>Donate Us</span></a>
UncaughtTypeError
  • 8,226
  • 4
  • 23
  • 38