Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a CSS button: positioning the text

Tags:

html

css

position

I'm trying to make a rudimentary css button.

I'm encountering errors while trying to position the text vertically and horizontally centered within the button. I have this following html.

<!DOCTYPE html>
<html>
  <head>
    <style type="text/css">
    #button
    {
      background:lightblue;
      border-radius: 30px;
      width: 15em;
      height: 3em;
      position:relative;
    }
    h1 {
      vertical-align: center;
      text-align: center;
    }
    </style>
    <title>Making a button...</title>
  </head>
  <body>
    <div id="button"><h1>Click Me</h1></div>
  </body>
</html>

jsfiddle for this question: http://jsfiddle.net/CRpvr/

I was told there was a workaround using line-height: 100%, but if I need more than one line, this doesnt work very well. What is the most idiomatic and proper way to accomplish this?

like image 291
Senjai Avatar asked Oct 25 '25 06:10

Senjai


1 Answers

Simply change "height" within your button ID to "line-height". I checked this and it worked.

like image 176
Chris Avatar answered Oct 27 '25 22:10

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!