After researching the topic, I found valuable insights on this website:
- Historically, HTML layout lacked specification for vertical behavior. It primarily focused on width scaling, with height adjusting accordingly. Vertical sizing and layout were extensions of horizontal rules.
vertical-align
can exhibit diverse behaviors based on its context in coding.
Understanding vertical-align in table cells
In table cells, vertical-align
generally aligns content as expected, resembling the outdated valign
. In modern browsers, these code snippets produce similar outcomes:
<td valign="middle"> <!-- avoid using valign --> </td>
<td style="vertical-align:middle"> ... </td>
<div style="display:table-cell; vertical-align:middle"> ... </div>
Exploring vertical-align with inline elements
Applying vertical-align
to inline elements introduces a different dynamic. It imitates the deprecated align
attribute used on <img>
elements. Modern browsers treat these code snippets similarly:
<img align="middle" ...>
<img style="vertical-align:middle" ...>
<span style="display:inline-block; vertical-align:middle"> foo<br>bar </span>