Explain Codes LogoExplain Codes Logo

How may I align text to the left and text to the right in the same line?

html
responsive-design
css-grid
flexbox
Alex KataevbyAlex Kataev·Aug 22, 2024
TLDR

You can use CSS floats to align text to the left and the right on the same line. The following line of code demonstrates this:

<div> <span style="float: left;">Left Text</span> <!-- I belong to the left side, just like your heart --> <span style="float: right;">Right Text</span> <!-- I'd rather stay on your right, like a trusted pen --> </div>

Remember to contain your floats and clear them properly to avoid layout disruptions.

Digging deeper: Alternatives and best practices

In addition to CSS floats, other modern techniques can also achieve the same result beautifully.

Inline-block: Avoiding the float

You can replace floats with display: inline-block;, which helps maintain elements inline without the quirks associated with float.

<div> <span style="display: inline-block; width: 50%;">Left Text</span> <!-- With 50% width, I herald you from the left --> <span style="display: inline-block; text-align: right; width: 50%;">Right Text</span> <!-- Carefully aligned to the right, with 50% of the spotlight --> </div>

Flexbox: Simplifying alignment

If you want more control over arrangement of items especially with responsive behavior in mind, you can utilize flexbox.

<div style="display: flex; justify-content: space-between;"> <div>Left Text</div> <!-- I'm a flex item, you can put me on a diet, I can shrink or grow --> <div>Right Text</div> <!-- I'm flexible too but I'll always stick to the right! --> </div>

CSS Grid: Precision and flexibility combined

If you want more fine-grained control with the placement of items, you can use CSS Grid.

<div style="display: grid; grid-template-columns: auto auto;"> <div>Left Text</div> <!-- Grid life, breathing from the left lung --> <div style="justify-self: end;">Right Text</div> <!-- With grid, gravity pulls me to the right --> </div>

External Styling: Better practice

In order to maintain code readability, it's always recommended to keep the styles separated from the HTML structure using CSS classes.

.left { float: left; } /* Lefty loosey */ .right { float: right; } /* Righty tighty */
<div> <span class="left">Left Text</span> <span class="right">Right Text</span> </div>

Considerations

While implementing these practices, make sure to watch out for potential pitfalls. Be aware of the clearfix hack when using floats and be cautious about cross-browser compatibility as older browsers may not support CSS flexbox or grid entirely.

Extra tips, tricks and best practices

When it comes to aligning text to opposite ends on the same line, sometime you may face challenges:

Overlapping text

To handle text that could exceed their container width, consider using text-overflow property.

Responsive design

Use media queries to make your alignment behaviour adaptable to various screen sizes.

Accessible text

Make sure your alignment compliments the readability and accessibility of your text. Ultimately, remember that content is king.

Live implementation

Visualize all these techniques working live through a jsfiddle link.