html vertical align the text inside input type button

前端 未结 10 2035
抹茶落季
抹茶落季 2021-02-01 00:40

I\'m trying to create a button with height of \'22px\' , and that the text inside the button will be vertically aligned in the center of the button. I tried everything , and can

相关标签:
10条回答
  • 2021-02-01 01:16

    If your button weren't floated, you could use vertical-align:middle; to center the text inside it. After lots of experimentation, this was the only solution I could find that worked in IE without changing markup. (in Chrome, button text seems to be automatically centered without this hack).

    But you use a float:left which forces the element to be a block element (your display:inline-block is ignored), whcih in turn prevents vertical-align from working because vertical-align doesn't work inside a block element.

    So you have three choices:

    • stop using floats in this form, and use inline and inline-block elements to simulate floating.
    • as @stefan notes above, use another element like an <a> and use javascript to submit the form.
    • accept that in IE your buttons will be off by 1px vertically.
    0 讨论(0)
  • 2021-02-01 01:19

    I had a button where the background-image had a shadow below it so the text alignment was off from the top. Changing the line-height wouldn't help. I added padding-bottom to it and it worked.

    So what you have to do is determine the line-height you want to play with. So, for example, if I have a button who's height is truly 90px but I want the line-height to be 80px I would have something like this:

    input[type=butotn].mybutton{
        background: url(my/image.png) no-repeat center top; /*Image is 90px x 150px*/
        width: 150px;
        height: 80px; /*shadow at the bottom is 10px (90px-10px)*/
        padding-bottom: 10px; /*the padding will make up for the lost height while maintaining the line-height to the proper height */
    
    }
    

    I hope this helps.

    0 讨论(0)
  • 2021-02-01 01:22

    The simplest thing you can do is use reset.css. It normalizes the default stylesheet across browsers, and coincidentally allows button { vertical-align: middle; } to work just fine. Give it a shot - I use it in virtually all of my projects just to kill little bugs like this.

    https://gist.github.com/nathansmith/288292

    0 讨论(0)
  • 2021-02-01 01:29

    I was having a similar issue with my button. I included line-height: 0; and it appears to have worked. Also mentioned by @anddero.

    button[type=submit] {
      background-color: #4056A1;
      border-radius: 12px;
      border: 1px solid #4056A1;
      color: white;
      padding: 16px 32px;
      text-decoration: none;
      margin: 2px 1px;
      cursor: pointer;
      display: inline-block;
      font-size: 16px;
      height: 20px;
      line-height: 0;
    }
    
    0 讨论(0)
  • 2021-02-01 01:30

    Try adding the property line-height: 22px; to the code.

    0 讨论(0)
  • 2021-02-01 01:35

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

    .testbutton {
      display: inline-flex;
      align-items: center; 
    }
    
    0 讨论(0)
提交回复
热议问题