28

I have got tricky problem here. I'd like to vertical align my text inside a button

<button id="rock" onClick="choose(1)">Rock</button>

And here is my CSS

button {
    font-size: 22px;
    border: 2px solid #87231C;
    border-radius: 100px;
    width: 100px;
    height: 100px;
    color: #FF5A51;
    text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
button:active {
    font-size: 22px;
    border: 2px solid red;
    border-radius: 100px;
    width: 100px;
    height: 100px;
}

You can check it out here http://jsfiddle.net/kA8pp/ . I want to have the text on the bottom. Thank you very much!

EDIT: I can't explain it well so here is the picture of it :)

enter image description here

Black
  • 18,150
  • 39
  • 158
  • 271
Kyrbi
  • 2,212
  • 7
  • 25
  • 42
  • 1
    what do you mean by on the bottom, as in under the picture or just at the bottom of the picture? – npage Mar 18 '13 at 21:40

5 Answers5

28

You can use line-height to achieve your goal.

button {
    font-size: 22px;
    border: 2px solid #87231C;
    border-radius: 100px;
    width: 100px;
    height: 100px;
    color: #FF5A51;
    text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
    line-height: 150px;
    overflow: visible;
}

http://jsfiddle.net/kA8pp/2/

Valentin Flachsel
  • 10,795
  • 10
  • 44
  • 67
npage
  • 1,258
  • 10
  • 14
  • I will use padding-top:65px; as someone said it here. But thank you very much also. :) – Kyrbi Mar 18 '13 at 21:47
  • +1, Was about to write the same answer. @MíraKapičiak, Padding is a wrong way to solve this. This is the correct way. – Starx Mar 18 '13 at 21:48
  • cool. using line-height I can put text at bottom but how about vertically aligning it to the top? – MilkyTech May 08 '14 at 16:13
  • 3
    I'm here because I'm trying *not* to use line-height, which renders differently across browsers on many fonts. Turns out its not so easy.. – Yuji 'Tomita' Tomita Jun 03 '14 at 22:40
22

You can use flexbox (check browser support, depending on your needs).

button {
  display: inline-flex;
  align-items: flex-end; 
}
Pensierinmusica
  • 6,404
  • 9
  • 40
  • 58
10

Try padding-top:65px; in button class

button {
    font-size: 22px;
    border: 2px solid #87231C;
    border-radius: 100px;
    width: 100px;
    height: 100px;
    color: #FF5A51;
    text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
    padding-top:65px;
}

JS Fiddle Demo

Sachin
  • 40,216
  • 7
  • 90
  • 102
2

Buttons Style Differently: A Variation on Styling

The <button> element behaves a bit differently from some other elements, say a <div>. If you set display: table-cell in a button, it does not affect the layout, so vertical-align will allow you to control the text position.

On the other hand, one could do the following:

<div class="button" id="rock" onClick="choose(1)">Rock</div>

and use the following CSS:

.button {
    font-size: 22px;
    border: 2px solid #87231C;
    border-radius: 100px;
    width: 100px;
    height: 90px;
    color: #FF5A51;
    text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
    display: table-cell;
    vertical-align: bottom;
    text-align: center;
    padding-bottom: 10px;
}

In this case, you have some control by adjusting padding and height.

If you are binding a JavaScript action to the element, it does not really matter all that much what the tag is, div or button.

This approach may some advantages in particular situations.

If you apply the above CSS to a button tag, it will not work. Good to know.

http://jsfiddle.net/audetwebdesign/QSap8/

Marc Audet
  • 46,011
  • 11
  • 63
  • 83
-1

This should work for you!

<pre>
.button{
    font-family: Arial, Helvetica, sans-serif;
    font-weight: normal;
    color: #444;
    width: 32px;
    padding: 2px;
    margin-left: 5px;
    background-color: #fdfdfd;
    border: 1px solid #cdcdcd;
    cursor: pointer;
}
</pre>
Ankur Gupta
  • 41
  • 10