Minimum height for div or span with empty element

后端 未结 2 1554
一生所求
一生所求 2021-02-05 16:41

When I enclose within span or div a string that happens to be an empty string or includes only white spaces, that part does not have any height, and wh

相关标签:
2条回答
  • 2021-02-05 17:10

    Your span element needs to become a block element if you want to set its height. So set the style display: block or display: inline-block as appropriate.

    span.item {
      display: inline-block;
    }
    

    To set the height of an empty span, I've found it best to simply inject a   rather than set a min-height. (UPDATE: per @sawa, rather than using a non-breaking space character, perhaps a more suitable character would take up no space, i.e. the unicode ZERO WIDTH SPACE character, \200b.)

    span.item:empty::before {
      content: "\200b"; /* unicode zero width space character */
    }
    

    This will work for whatever the font size may be, and it avoids problems with the baseline not lining up with adjacent text. Look at the line that says "Huh?" below:

    how baseline fails

    http://plnkr.co/edit/GGd7mz?p=preview

    (See similar question: https://stackoverflow.com/a/29354766/516910)

    0 讨论(0)
  • 2021-02-05 17:20

    Your code isn't right: you don’t need enclosing quotes ("") around values in CSS.

    Either use:

    min-height: 1em; /* I am not sure if one can use em with height properties */
    

    Or use:

    min-height: 12px;
    
    0 讨论(0)
提交回复
热议问题