2

I have button with text on top

Here is code

.settings-button-new {
    width: 150px;
    height: 150px;
    text-decoration: none;
    line-height: 14px;
    text-align: center;
    position: relative;
    margin-left: 20px;
    margin-bottom: 20px;
    border: solid 4px #d3dbde;
    -webkit-border-radius: 13px;
    -moz-border-radius: 13px;
    border-radius: 13px;
    font: 15px Arial, Helvetica, sans-serif;
    color: #1c559b;
    background-color: #ffffff;
    background-image: -moz-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -webkit-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -o-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -ms-linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#fcfcfc',GradientType=0 );
    background-image: linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
    -webkit-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
    -moz-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
    box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
}

.settings-button-new span {
    display: block;
    position: absolute;
    top:0;
    left:0;
}
 <button class="settings-button-new"><span>Home</span></button>

It on the top, but not centered as you can see.

I try to use text-align: center; but seems it not works.

How can i center it in button?

Thank's for help

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Sukhomlin Eugene
  • 183
  • 4
  • 19

6 Answers6

2

Just add below css code for center, if don't need to vertically center then the top:0 or as your need; and transform: translate(-50%, 0);.

.settings-button-new span {
      display: block;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

.settings-button-new {
  width: 150px;
  height: 150px;
  text-decoration: none;
  line-height: 14px;
  position: relative;
  margin-left: 20px;
  margin-bottom: 20px;
  border: solid 4px #d3dbde;
  -webkit-border-radius: 13px;
  -moz-border-radius: 13px;
  border-radius: 13px;
  font: 15px Arial, Helvetica, sans-serif;
  color: #1c559b;
  background-color: #ffffff;
  background-image: -moz-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -webkit-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -o-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -ms-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#fcfcfc', GradientType=0);
  background-image: linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  -webkit-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
  -moz-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
  box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
}

.settings-button-new span {
  display: block;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<button class="settings-button-new"><span>Home</span></button>

Only center.

.settings-button-new span {
          display: block;
          position: absolute;
          top: 5px;
          left: 50%;
          transform: translate(-50%, 0);
        }

.settings-button-new {
  width: 150px;
  height: 150px;
  text-decoration: none;
  line-height: 14px;
  position: relative;
  margin-left: 20px;
  margin-bottom: 20px;
  border: solid 4px #d3dbde;
  -webkit-border-radius: 13px;
  -moz-border-radius: 13px;
  border-radius: 13px;
  font: 15px Arial, Helvetica, sans-serif;
  color: #1c559b;
  background-color: #ffffff;
  background-image: -moz-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -webkit-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -o-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -ms-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#fcfcfc', GradientType=0);
  background-image: linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  -webkit-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
  -moz-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
  box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
}

.settings-button-new span {
  display: block;
  position: absolute;
  top: 5px;
  left: 50%;
  transform: translate(-50%, 0);
}
<button class="settings-button-new"><span>Home</span></button>
LKG
  • 4,152
  • 1
  • 11
  • 21
1

Maybe this code can help you

.settings-button-new {
    width: 150px;
    height: 150px;
    text-decoration: none;
    line-height: 14px;
    text-align: center;
    position: relative;
    margin-left: 20px;
    margin-bottom: 20px;
    border: solid 4px #d3dbde;
    -webkit-border-radius: 13px;
    -moz-border-radius: 13px;
    border-radius: 13px;
    font: 15px Arial, Helvetica, sans-serif;
    color: #1c559b;
    background-color: #ffffff;
    background-image: -moz-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -webkit-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -o-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -ms-linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#fcfcfc',GradientType=0 );
    background-image: linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
    -webkit-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
    -moz-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
    box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
}

.settings-button-new span {
    display: block;
    position: absolute;
    top:0;
    left: 50%;
    transform: translateX(-50%);
}
 <button class="settings-button-new"><span>Home</span></button>
Yusril Herlian Syah
  • 497
  • 1
  • 6
  • 12
1

You can remove vertical aligning text using without absolute positioning: you can use vertical-align and pseudoelement for this. Source answer for this technique.

Also

  • You don't need absolute position in this case (and text-align: center).
  • I would recommend you to delete vendor prefixed properties because they are for very old browsers
  • linear-gradient will work for background, not background-image property

Demo:

/* styles to align button's text at the top */
.settings-button-new:after {
  content: "";
  display: inline-block;
  vertical-align: top;
  height: inherit;
}

/* styles to align button's text at the top */
.settings-button-new span {
  vertical-align: top;
  display: inline-block;
}

/* Cleaned up button styles */
.settings-button-new {
  width: 150px;
  height: 150px;
  text-decoration: none;
  margin-left: 20px;
  margin-bottom: 20px;
  border: solid 4px #d3dbde;
  border-radius: 13px;
  font: 15px Arial, Helvetica, sans-serif;
  color: #1c559b;
  background: #fff linear-gradient(top, #fff 0%, #fcfcfc 100%);
  box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #fff;
}
<button class="settings-button-new">
  <span>Home</span>
</button>
Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
0
.settings-button-new span {
margin: 0px;
text-align: center;

}

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
0

can you change span to div , text-align:center will then work fine

Fahad Azhar
  • 188
  • 1
  • 6
0

remove the poistion .and simply add margin:auto

.settings-button-new {
    width: 150px;
    height: 150px;
    text-decoration: none;
    line-height: 14px;
    text-align: center;
    position: relative;
    margin-left: 20px;
    margin-bottom: 20px;
    border: solid 4px #d3dbde;
    -webkit-border-radius: 13px;
    -moz-border-radius: 13px;
    border-radius: 13px;
    font: 15px Arial, Helvetica, sans-serif;
    color: #1c559b;
    background-color: #ffffff;
    background-image: -moz-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -webkit-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -o-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -ms-linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#fcfcfc',GradientType=0 );
    background-image: linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
    -webkit-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
    -moz-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
    box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
}

.settings-button-new span {
    margin:auto;
}
<button class="settings-button-new"><span>Home</span></button>
prasanth
  • 22,145
  • 4
  • 29
  • 53