Right align and left align text in same HTML table cell
Categories:
Mastering Table Cell Alignment: Right and Left Text in a Single HTML Cell

Learn various CSS techniques to achieve both right and left alignment within the same HTML table cell, enhancing data presentation and readability.
HTML tables are fundamental for displaying tabular data. While aligning content within a cell (like text-align: left;
or text-align: right;
) is straightforward, the challenge arises when you need to display different pieces of text within the same cell, with some aligned to the left and others to the right. This article explores several robust CSS methods to achieve this dual alignment, providing flexibility and control over your table layouts.
The Challenge of Dual Alignment in a Single Cell
By default, a <td>
element can only have one text-align
property applied to its direct content. If you apply text-align: left;
to a <td>
, all its direct text content will align left. The same applies to right
or center
. To achieve dual alignment, we need to introduce additional elements within the <td>
that can be individually styled and positioned. This typically involves using <span>
or <div>
elements and leveraging CSS properties like float
, flexbox
, or grid
.

Conceptual approach to dual alignment within a single table cell.
Method 1: Using Floats for Simple Dual Alignment
The float
property is a classic CSS technique for positioning elements side-by-side. By floating one element left and another right within the same <td>
, you can achieve the desired dual alignment. It's important to clear the floats if there's subsequent content or if the <td>
needs to correctly wrap its floated children.
<table>
<tr>
<th>Product</th>
<th>Details</th>
</tr>
<tr>
<td>Item A</td>
<td>
<span style="float: left;">Quantity: 5</span>
<span style="float: right;">Price: $12.99</span>
<div style="clear: both;"></div> <!-- Clear floats -->
</td>
</tr>
<tr>
<td>Item B</td>
<td>
<span style="float: left;">Quantity: 10</span>
<span style="float: right;">Price: $5.50</span>
<div style="clear: both;"></div>
</td>
</tr>
</table>
HTML structure using float
for dual alignment.
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left; /* Default alignment for the cell */
}
td > span {
display: inline-block; /* Ensure spans behave like blocks for float */
}
.clear-floats {
clear: both;
height: 0;
overflow: hidden;
}
Basic CSS for the float method. Note the clear: both;
to prevent layout issues.
<td>
in this case) needs to expand to contain its floated children or if there's content following the floats that shouldn't be affected by them. A common technique is to add an empty div
with clear: both;
or apply overflow: hidden;
to the parent.Method 2: Leveraging Flexbox for Modern Alignment
Flexbox is a powerful and more modern layout module that offers a more robust and predictable way to align items. By setting the <td>
to display: flex;
and using justify-content: space-between;
, you can easily push two child elements to opposite ends of the cell.
<table>
<tr>
<th>Product</th>
<th>Details</th>
</tr>
<tr>
<td>Item A</td>
<td class="flex-cell">
<span>Quantity: 5</span>
<span>Price: $12.99</span>
</td>
</tr>
<tr>
<td>Item B</td>
<td class="flex-cell">
<span>Quantity: 10</span>
<span>Price: $5.50</span>
</td>
</tr>
</table>
HTML structure for Flexbox method.
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
}
.flex-cell {
display: flex;
justify-content: space-between; /* Pushes items to opposite ends */
align-items: center; /* Vertically centers items */
}
CSS for Flexbox dual alignment. justify-content: space-between;
is key here.
Method 3: Using CSS Grid for Structured Layouts
CSS Grid offers another powerful way to create two-column layouts within a <td>
. While it might be overkill for a simple left/right alignment, it provides excellent control for more complex internal cell structures. You can define a grid with two columns and place your content accordingly.
<table>
<tr>
<th>Product</th>
<th>Details</th>
</tr>
<tr>
<td>Item A</td>
<td class="grid-cell">
<span>Quantity: 5</span>
<span>Price: $12.99</span>
</td>
</tr>
<tr>
<td>Item B</td>
<td class="grid-cell">
<span>Quantity: 10</span>
<span>Price: $5.50</span>
</td>
</tr>
</table>
HTML structure for CSS Grid method.
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
}
.grid-cell {
display: grid;
grid-template-columns: 1fr 1fr; /* Two equal columns */
justify-items: start; /* Default for first item */
align-items: center; /* Vertically centers items */
}
.grid-cell span:last-child {
justify-self: end; /* Align the second item to the end of its grid area */
}
CSS for Grid-based dual alignment. grid-template-columns
and justify-self
are key.
Choosing the Right Method
The best method depends on your specific needs and browser support requirements:
- Floats: Good for legacy browser support or very simple cases, but can be tricky with clearing and responsiveness.
- Flexbox: The recommended modern approach for one-dimensional alignment (like left/right in a row). It's clean, powerful, and widely supported.
- CSS Grid: Excellent for complex two-dimensional layouts within a cell, but might be overkill for simple dual alignment.