How to vertically align 2 different sizes of text?

后端 未结 8 590
终归单人心
终归单人心 2020-12-02 13:48

I know to vertically align text to the middle of a block, you set the line-height to the same height of the block.

However, if I have a sentence with a word in the m

相关标签:
8条回答
  • 2020-12-02 14:26

    You technically can't, however, if you have fixed text sizes you could use positioning (relative) to move the larger text down and set the line-height to the smaller text (I'm presuming this larger text is marked up as such so you can use that as a CSS selector)

    0 讨论(0)
  • 2020-12-02 14:26

    You can use percentage sizes to reapply the parent's line-height

    .big {
      font-size: 200%;
      line-height: 25%;
      display: inline-block;
      vertical-align: middle;
    }
    Utque aegrum corpus <span class="big">etiam</span> levibus solet offensis 

    0 讨论(0)
  • 2020-12-02 14:34

    The functionality you are seeing is correct because the default for "vertical-align" is baseline. It appears that you want vertical-align:top. There are other options. See here at W3Schools.

    Edit W3Schools has not cleaned up their act and still, appear, to be a shoddy (at best) source of information. I now use sitepoint. Scroll to the bottom of the sitepoint front page to access their reference sections.

    0 讨论(0)
  • 2020-12-02 14:35

    Easy way - use flex:

    <div>
            abcde
            &nbsp;&nbsp;
            <span>efghai</span>
    </div>
    
    <style>
        div {
            padding: 20px;
            background-color: orange;
            display: flex;
            align-items: center; }
    
        span {
            font-size: 1.5em; }
    </style>
    
    0 讨论(0)
  • 2020-12-02 14:38

    An option is to use a table there the different sized texts are in their own cells and use align:middle on each cell.

    Its not pretty and does not work for all occasions, but it is simple and works with any text size.

    0 讨论(0)
  • 2020-12-02 14:39

    This works

    header > span {
        margin: 0px 12px 0px 12px;
        vertical-align:middle;
    }
    .responsive-title{
        font-size: 12vmin;
        line-height: 1em;
    }
    .responsive-subtitle{
        font-size: 6vmin;
        line-height: 2em;
    }
    <header>
      <span class="responsive-title">Foo</span>
      <span class="responsive-subtitle">Bar</span>
    </header>

    0 讨论(0)
提交回复
热议问题