Minimum height for div or span with empty element

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 when that span or div is further embedded into something like a table, that cell does not have enough height, and looks wrong. How can I ensure that a span or div takes up at least the height of when the string has other characters? This is something like doing \strut in TeX. I can either insert something into the string, or adjust the css.

I tried, putting the following into the relavant css class, but the problem is that I have to manually adjust the string height (I am not sure if it is "1em". Probably not). What is the right way to do this?

答:
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.

你可能感兴趣的:(Minimum height for div or span with empty element)