Minimum height for div or span with empty element

16,241

Solution 1

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)

Solution 2

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;
Share:
16,241

Related videos on Youtube

sawa
Author by

sawa

I have published the following two Ruby gems: dom: You can describe HTML/XML DOM structures in Ruby language seamlessly with other parts of Ruby code. Node embedding is described as method chaining, which avoids unnecessary nesting, and confirms to the Rubyistic coding style. manager: Manager generates a user's manual and a developer's chart simultaneously from a single spec file that contains both kinds of information. More precisely, it is a document generator, source code annotation extracter, source code analyzer, class diagram generator, unit test framework, benchmark measurer for alternative implementations of a feature, all in one. Comments and contributions are welcome. I am preparing a web service that is coming out soon.

Updated on June 04, 2022

Comments

  • sawa
    sawa almost 2 years

    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?

    min-height : 1em;

    • wwwroth
      wwwroth over 12 years
      If it's a SPAN, you're going to need to add "display:block;" to it to accept height values. By default, it's displayed inline. I would also recommend if this is inside a table to assign the min-height to the <tr> tag specifically and make the entire row the same height.
  • sawa
    sawa over 12 years
    That was just my typo. Actually, It worked without the quotations, and em is available. I checked it with different values of em, and it worked. The problem is that I need to adjust it manually.
  • Pankaj Upadhyay
    Pankaj Upadhyay over 12 years
    You can set the min property for table colums. Otherwise you have to manage it manually. Besides, it good behavior not to do anything on its own. You might have strange results otherwise :-)
  • sawa
    sawa over 12 years
    Thanks. I would like to accept the comment part of your answer.
  • sawa
    sawa about 9 years
    "\00a0" (&nbsp;) takes up horizontal space. I think "\200b" (&#8203;) is better
  • broc.seib
    broc.seib about 9 years
    @sawa Agreed, "\200b" give the presence of text while being completely invisible.
  • Prasad Silva
    Prasad Silva about 8 years
    Octal escape sequences are deprecated. Use \u200b instead.
  • broc.seib
    broc.seib about 8 years
    @PrasadSilva Hmmm... what's your source saying this is deprecated? The current CSS spec doesn't say anything about a leading "backslash u". That would just give you a literal "u". The spec says "backslash followed by 1-6 hex digits". See this railroad diagram: w3.org/TR/css-syntax-3/#string-token-diagram and w3.org/TR/css-syntax-3/#escape-diagram
  • Prasad Silva
    Prasad Silva about 8 years
    Yea, my mistake. I was looking at this post trying to fix a strict mode error I got in JS while using the zero width space character. ES5 forbids octal syntax in strict mode: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…