How to vertically align text with icon font?

前端 未结 10 1328
醉话见心
醉话见心 2020-11-29 01:08

I have a very basic HTML which mix plain text and icon fonts. The problem is that icons are not exactly rendered at the same height than the text:

相关标签:
10条回答
  • 2020-11-29 01:24

    You can use this property : vertical-align:middle;

    .selector-class { 
        float:left;
        vertical-align:middle; 
    } 
    
    0 讨论(0)
  • 2020-11-29 01:25

    Set line-height to the vertical size of the picture, then do vertical-align:middle like Josh said.

    so if the picture is 20px, you would have

    {
    line-height:20px;
    font-size:14px;
    vertical-align:middle;
    }
    
    0 讨论(0)
  • 2020-11-29 01:33

    To expand on Marian Udrea's answer: In my scenario, I was trying to align the text with a material icon. There's something weird about material icons that prevented it from being aligned. None of the answers were working, until I added the vertical-align to the icon element, instead of the parent element.

    So, if the icon is 24px in height:

    .parent {
        line-height: 24px; // Same as icon height
    
        i.material-icons {  // Only if you're using material icons
          display: inline-flex;
          vertical-align: top;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 01:35

    Add this to your CSS:

    .menu i.large.icon,
    .menu i.large.basic.icon {
        vertical-align:baseline;
    }
    

    DEMO

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

    vertical-align can take a unit value so you can resort to that when needed:

    {
      display:inline-block;
      vertical-align: 5px;
    }
    
    {
      display:inline-block;
      vertical-align: -5px;
    }
    
    0 讨论(0)
  • 2020-11-29 01:43

    There are already a few answers here but I found flexbox to be the cleanest and least "hacky" solution:

    parent-element {
      display: flex;
      align-items: center;
    }
    

    To support Safari < 8, Firefox < 21 and Internet Explorer < 10 (Use this polyfill to support IE8+9) you'll need vendor prefixes:

    parent-element {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-align: center;
          -ms-flex-align: center;
              align-items: center;
    }
    
    0 讨论(0)
提交回复
热议问题