javascript - Extra top padding in a container when using HTML5 doctype -
there top padding in following div
:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>extra top padding</title> <style> div { background: tan; } div * { vertical-align: middle; } span { font-size: 16px; } </style> </head> <body> <div id="container"> <img src="https://ssl.gstatic.com/images/icons/gplus-16.png"><span>some text</span> </div> <p id="indicator"></p> <script> document.getelementbyid('indicator').textcontent = document.getelementbyid('container').clientheight; </script> </body> </html>
it shows correctly if use html 4.01 transitional doctype:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
bug in html5?
tl;dr
it's not bug. follows consequence of using standards mode or standards mode document. , it's not padding, it's line-height space.
personally, wouldn't satisfied above explanation. explain requires substantial detail. here goes:
calculations times new roman typeface. other typefaces may give different values though principles remain same.
the height difference same or without img element let's discard that.
when use html5 doctype (or html 4.01 strict doctype) standards mode. when use html 4.01 transitional doctype standards mode. difference between 2 modes inline boxes affect line height. in particular, strut has no effect in standards mode.
so in almost standards mode, inline box text. total height of div line height of text. 16px * 1.25 (the div's line-height setting) = 20px
.
standards mode more complicated. have both text , strut. strut aligned baseline. default line height of 1.25 gives 2px upper half leading + 13px ascender above baseline , 3px descender + 2px lower half leading. total 20px.
the text vertical-align:middle. css 2.1 spec says
align vertical midpoint of box baseline of parent box plus half x-height of parent.
the baseline of parent box baseline of strut know above 5px above bottom (before adding text).
the x height typeface @ font-size of 16px around 7px. halve , truncate whole number gives 3px. add 5px , alignment line text of 8px above bottom.
the vertical midpoint of box has 10px above alignment line , 10px below alignment line.
the line height must large enough contain inline boxes, means must maximum of boxes above alignment line + maximum of boxes below alignment line. that's max(12px[strut], 10px[text]) above line , max(8px[strut], 10px[text]) below line.
which equals (12px + 10px) or 22px
.
Comments
Post a Comment