Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

height style property doesn't work in div elements

Tags:

html

css

I'm setting a height of 20px on a <div>, though when it renders in the browser, its only 14px high.

Any ideas?

<div style="display:inline; height:20px width: 70px">My Text Here</div>
like image 978
Priest of Psi Avatar asked Feb 11 '11 09:02

Priest of Psi


People also ask

Why is height property not working CSS?

If the height of the containing block is not specified explicitly and this element is not absolutely positioned, the value computes to 'auto'. The height depends on the values of other properties. Note that min-height and max-height are not acceptable. It must be the height property.

Can div have height?

This seems like the ideal candidate for transition from a table-based layout to a CSS layout. It makes sense: both DIVs and tables can be nested, have HEIGHT and WIDTH attributes set, contain borders, etc.

Why does my div have no height?

The reason why the height or the containers is 0 is because you are floating all the content inside them and floated elements don't expand the height (or width) of their parents.


4 Answers

You cannot set height and width for elements with display:inline;. Use display:inline-block; instead.

From the CSS2 spec:

10.6.1 Inline, non-replaced elements

The height property does not apply. The height of the content area should be based on the font, but this specification does not specify how. A UA may, e.g., use the em-box or the maximum ascender and descender of the font. (The latter would ensure that glyphs with parts above or below the em-box still fall within the content area, but leads to differently sized boxes for different fonts; the former would ensure authors can control background styling relative to the 'line-height', but leads to glyphs painting outside their content area.)

EDIT — You're also missing a ; terminator for the height property:

<div style="display:inline; height:20px width: 70px">My Text Here</div> <!--                                  ^^ here                       --> 

Working example: http://jsfiddle.net/FpqtJ/

like image 172
Andy E Avatar answered Sep 25 '22 02:09

Andy E


This worked for me:

min-height: 14px; height: 14px; 
like image 31
Sol Avatar answered Sep 26 '22 02:09

Sol


Set positioning to absolute. That will solve the problem immediately, but might cause some problems in layout later. You can always figure out a way around them ;)

Example:

position:absolute;
like image 36
Purnesh Tripathi Avatar answered Sep 25 '22 02:09

Purnesh Tripathi


Also, make sure you add ";" to each style. Your excluding them from width and height and while it might not be causing your specific problem, it's important to close it.

<div style="height:20px; width: 70px;">My Text Here</div>
like image 45
We're All Scholars Avatar answered Sep 22 '22 02:09

We're All Scholars