I want to vertical-align text in select box

前端 未结 16 1183
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 00:25

I want to vertically align the text in select box. I tried using

select{
   verticle-align:middle;
}

however it does not work in any brows

相关标签:
16条回答
  • 2020-11-29 01:14

    My solution is to add padding-top for select targeted to firefox only like this

    // firefox vertical aligment of text
    @-moz-document url-prefix() {
        select {
            padding-top: 13px;
       }
    }
    
    0 讨论(0)
  • 2020-11-29 01:15

    I used to use height and line-height with the same values, but the proved to be inconsistent across the interwebs. My current approach is to mix that with padding like so.

    select {
      font-size:14px;
      height:18px;
      line-height:18px;
      padding:5px 0;
      width:200px;
      text-align:center;
    }
    

    You could also use padding for the horizontal value instead of the width + text-align

    0 讨论(0)
  • 2020-11-29 01:16

    I've tried as following and it worked for me:

    select {
         line-height:normal;
         padding:3px;
    } 
    
    0 讨论(0)
  • 2020-11-29 01:17

    Your best option will probably be to adjust the top padding & compare across browsers. It's not perfect, but I think it's as close as you can get.

    padding-top:4px;
    

    The amount of padding you need will depend on the font size.

    Tip: If your font is set in px, set padding in px as well. If your font is set in em, set the padding in em too.

    EDIT

    These are the options I think you have, since vertical-align isn't consistant across the browsers.

    A. Remove height attribute and let the browser handle it. This usually works the best for me.

     <style>
        select{
            border: 1px solid #ABADB3;
            margin: 0;
            padding: auto 0;
            font-size:14px;
        }
        </style>
        <select>
            <option>option 1</option>
            <option>number 2</option>
        </select>
    

    B. Add top-padding to push down the text. (Pushed down the arrow in some browsers)

    <style>
    select{
        height: 28px !important;
        border: 1px solid #ABADB3;
        margin: 0;
        padding: 4px 0 0 0;
        font-size:14px;
    }
    </style>
    <select>
        <option>option 1</option>
        <option>number 2</option>
    </select>
    

    C. You can make the font larger to try and match the select height a little nicer.

    <style>
    select{
        height: 28px !important;
        border: 1px solid #ABADB3;
        margin: 0;
        padding: 0 0 0 0;
        font-size:17px;
    }
    </style>
    <select>
        <option>option 1</option>
        <option>number 2</option>
    </select>
    
    0 讨论(0)
  • 2020-11-29 01:17

    I ran into this problem exactly. My select options were vertically centered in webkit, but ff defaulted them to the top. After looking around I didn't really want to create a work around that was messy and ultimately didn't solve my problem.

    Solution: Javascript.

    if ($.browser.mozilla) {
                $('.styledSelect select').css( "padding-top","8px" );
            }
    

    This solves your problem very precisely. The only downside here is that I'm using jQuery, and if you aren't using jQuery on your project already, you may not want to include a js library for a one-off.

    Note: I don't recommend styling anything with js unless absolutely necessary. CSS should always be the primary solution for styling–think of jQuery (in this particular example) as the axe labeled "Break in case of Emergency".

    *UPDATE* This is an old post and since it appears people are still referencing this solution I should state (as mentioned in the comments) that since 1.9 this feature has been removed from jQuery. Please see the Modernizr project to perform feature detection in lieu of browser sniffing.

    0 讨论(0)
  • 2020-11-29 01:19

    So far this is working fine for me:

    line-height: 100%;
    
    0 讨论(0)
提交回复
热议问题