height vs line-height styling

后端 未结 6 1533
北恋
北恋 2020-12-13 13:14

What is the difference between using these two when dealing with text that will never be more than a single line? They both can produce similar results on the screen from w

相关标签:
6条回答
  • 2020-12-13 13:51

    height is the vertical measurement of the container.

    line-height is the distance from the top of the first line of text to the top of the second.

    If used with only one line of text I'd expect them to produce similar results in most cases.

    I use height when I want to explicitly set the container size and line-height for typographic layout, where it might be relevant if the user resizes the text.

    0 讨论(0)
  • 2020-12-13 13:53

    Assuming the text is smaller than the container:

    Setting the line-height on the container specifies the minimum height of line-boxes inside it. For 1 line of text, this results in the text vertically centered inside the container.

    If you set height on the container then the container will grow vertically, but the text inside it will start on the first (top) line inside it.

    0 讨论(0)
  • 2020-12-13 13:59

    For practical purposes in the case you cite (never wrapping to more than one line) line-height will vertically center the text. Be careful about that assumption though as the user ultimately controls the font-size.

    0 讨论(0)
  • 2020-12-13 14:07

    height = line-height + padding-top + padding-bottom

    0 讨论(0)
  • 2020-12-13 14:08

    Easily understand by seeing this example.

    .height
    {
        height: 20px;
    }
    
    .lheight
    {
        line-height: 15px;
    }
    
    .lheightbigger
    {
        line-height: 35px;
    }
    <h2>Height</h2>
    <div class="height">
      This is demo text. This is demo text.
      This is demo text. This is demo text. 
      This is demo text. This is demo text.
    </div><br>
    <h2>Line Height with less space</h2>
    <div class="lheight">
      This is demo text showing line height example. This is demo text showing line height example.
      This is demo text showing line height example. This is demo text showing line height example.
      This is demo text showing line height example.This is demo text showing line height example.
    </div>
    <h2>Normal Text</h2>
    <div>
      This is normal text.
      This is normal text.
      This is normal text.
    </div>
    <h2>Line Height with more space</h2>
    <div class="lheightbigger">
      This is normal text. This is normal text.This is normal text.
      This is normal text. This is normal text.This is normal text.
      This is normal text. This is normal text.This is normal text.
    </div>

    0 讨论(0)
  • 2020-12-13 14:11

    If you wrap the text in a div, give the div a height, and the text grows to be 2 lines (perhaps because it is being viewed on a small screen like a phone) then the text will overlap with the elements below it. On the other hand, if you give the div a line-height and the text grows to 2 lines, the div will expand (assuming you don't also give the div a height).

    Here is a fiddle that demonstrates this.

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