html - Vertical alignment in table-cell together with floating div -


i have following (sample) html code:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html>   <head>     <title>th-alignment</title>     <style type="text/css">         th {             border: 1px solid #333;             vertical-align: middle;         }         div.sb {             float: right;             width: 4px;             height: 26px;             background-color: #999;         }     </style>   </head>   <body>     <table cellspacing="0" cellpadding="0">         <colgroup>             <col width="120" />             <col width="120" />         </colgroup>         <thead>             <tr>                 <th>                     <div class="sb"></div>                     <div>heading 1</div>                 </th>                 <th>                     <div>heading 2</div>                 </th>             </tr>         </thead>     </table>   </body> </html> 

the div in second column vertically centered, div in first column not (always positioned @ top of table-cell). why floating div inside column 1 influence vertical alignment of other div within table cell?

thanks in advance.

if expand height th, notice elements still vertically aligned middle.

see fiddle example!

the issue here line-height.

since you've got more 1 element on first th, , 1 of elements has height defined, elements aligned middle of th between them, aligned height of current line-height, not defined , being calculated either font-size or parent element definition.

since floated element has height of 26px, grows down, element text no line-height defined it's height equal font-size provided text inside, never getting middle of floated sibling.

to fix this, need use:

line-height: 26px; 

to set line-height equal height of floated element, aligning text middle of floated element , solving visual aspect of vertical alignment.


see this fiddle example working solution!


note: 1 element not affecting other suspect, don't have enough declarations have coherent alignment among them!

by default text , images aligned baseline, if no line-height given, baseline font-size.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -