Explain Codes LogoExplain Codes Logo

Vertical align in bootstrap table

html
responsive-design
css
best-practices
Alex KataevbyAlex Kataev·Nov 4, 2024
TLDR

For vertical centering in Bootstrap tables, insert the align-middle class to your <td> or <th> tags:

<td class="align-middle">Your content</td>

This align-middle class ensures that content aligns at the vertical center inside the table cell using predefined Bootstrap's CSS. When dealing with images or complex inside-the-cell structures, set the fixed dimensions to keep the alignment stable:

<td class="align-middle"><img src="path-to-image.jpg" alt="Descriptive Text" style="height: 100px; width: auto;"></td>

In case where Bootstrap's utility classes leave out some cases, remember that CSS specificity can be a game changer. Use it to create rules that takes precedent over Bootstrap's defaults:

#myTable td.align-middle { vertical-align: middle !important; }
<h2 id='Understanding Alignment'>Digging deeper into vertical alignment</h2>

The world of vertical alignment can be quite tricky to aim for the bullseye, particularly if you're setting different types of content within tables. If you're climbing a mountain, it's better know the terrain, right? Here we explain how to be the 'mountain goat' of Bootstrap's align-middle class.

Multiple content types

If your td or th is housing both text and image, be mindful of the image size:

  • Stick to fixed dimensions for the images to keep the alignment consistent.
  • For text contained with images, discipline that text with a <span> and apply alignment classes when needed.
  • Bootstrap's align-top or align-bottom can be godsent for vertical alignments other than the middle.

Winning the specificity game

Another round of game where Bootstrap's classes just isn't hitting the mark because of other overriding CSS rules? You've got the power to reach the target:

  • Craft your CSS selector to be more specific than the current rule. (Remember, specificity is power!)
  • Employ the !important declaration wisely to prioritize necessary styling.

Tackling multi-line text

Working with multi-lines of text? Consider these:

  • Line-heights are like the lines of destiny. It has a major role in vertical alignment.
  • Be the artist and use padding to align multi-line text without disturbing those who are single (line).

Confronting edge cases

When alignment isn't your friend

Suspect some CSS conflicts messing up the alignment party? Take matters into your own hands using browser's developer tools.

Responsive designs – friend or foe?

Tables bend (reflow) under the pressure of smaller screens:

  • Here, Bootstrap's a real friend offering screen size-specific utility classes, not just empty comforting words.
  • Different devices, different problems? No problem, Bootstrap says test devices of all screen sizes.

Accessibility – including everyone

  • Not just for alignments, semantic HTML is the hero behind the scenes ensuring accessibility.
  • Keep vertical-align: middle; to content-housing cells, nothing else, or else...