Tips for aligning multiple lines on the left and right in HTML

Is there a way to align text to the left and right on multiple lines, like this example:

┌─────────────────────────────────────┐
│ Tums                                 29 │
│ Oxydativedecarboxilization          ATP │
│ appdata      C:\Users\Ian\AppData\Local │
└─────────────────────────────────────┘ 

For instance:

Please Note: There has been a similar question asked before about single-line alignment

Here are some examples I've come across from SO and other sources regarding attempts at left and right alignment:

<!DOCTYPE html>
<HTML>
<HEAD>
<HEAD>
    <TITLE>Home Page</TITLE>
    <STYLE type="text/css">
        .left {
            float: left;
        }
        .right {
            float: right;
        }
    </STYLE>
</HEAD>
<BODY>
    <DIV>
        <P><SPAN class='left'>Tums</SPAN><SPAN class='right'>29</SPAN>
        <P><SPAN class='left'>Oxydativedecarboxilization</SPAN><SPAN class='right'>42</SPAN>
        <P><SPAN class='left'>appdata</SPAN><SPAN class='right'>C:\Users\Ian\AppData\Local</SPAN>
    </DIV>

    <UL>
        <LI class='line1'><P class='left'>Tums<P class='right'>29
        <LI class='line2'><P class='left'>Oxydativedecarboxilization<P class='right'>42
        <LI class='line3'><P class='left'>appdata<P class='right'>C:\Users\Ian\AppData\Local
    </UL>

    <DIV>
        <P class='left'>Tums<P class='right'>29
        <P class='left'>Oxydativedecarboxilization<P class='right'>42
        <P class='left'>appdata<P class='right'>C:\Users\Ian\AppData\Local
    </DIV>
</BODY>
</HTML>

However, these render incorrectly as shown below:

Intended rendering should be:

Further Reading

  • HTML/CSS - Right and left align on the same line?
  • Cross-browser CSS for left align and right align on the same line

Answer №1

To fix the layout, you just need to clear your floats. Check out this example for reference.

<div>
<p><span class='left'>Tums</span><span class='right'>29</span></p>
<p><span class='left'>Oxydativedecarboxilization</span><span class='right'>42</span></p>
<p><span class='left'>appdata</span><span class='right'>C:\Users\Ian\AppData\Local</span></p>

.left { float: left; }
.right { float: right; }
p { overflow: hidden; }

Answer №2

.container { clear:both; }

<SECTION>
    <P class="container"><SPAN class='left'>Advil</SPAN><SPAN class='right'>15</SPAN>
    <P class="container"><SPAN class='left'>Hydrolysisreaction</SPAN><SPAN class='right'>27</SPAN>
    <P class="container"><SPAN class='left'>userdata</SPAN><SPAN class='right'>C:\Users\Emily\AppData\Local</SPAN>
</SECTION>

Answer №3

Discover: http://jsfiddle.net/thirtydot/HkybR/2/

XHTML:

<div class="data">
    <div>
        <span class="left">Tums</span><span class="right">29</span>
    </div>
    <div>
        <span class="left">Oxydativedecarboxilization</span><span class="right">ATP</span>
    </div>
    <div>
        <span class="left">appdata</span><span class="right">C:\Users\Ian\AppData\Local</span>
    </div>
</div>​

Perhaps a more meaningful structure could be implemented.

CSS:

.info {
    margin: 16px;
    padding: 8px;
    background: #fff;
    float: left;
    font-size: 21px;
    clear: both;
}
.info > div {
    overflow: hidden;
    text-align: right;
}
.info .left {
    float: left;
    padding-right: 10px;
}​

Answer №4

<UL style="list-style-type: none;">
    <LI class='line1'>Tums<div class='right'>29</div></LI>
    <LI class='line2'>Oxydativedecarboxilization<div class='right'>42</div></LI>
    <LI class='line3'>appdata<div class='right'>C:\Users\Ian\AppData\Local</div></LI>
</UL>

Renders as...

You might also want to consider adjusting the margins of the UL element.

UPDATE: It appears that utilizing the clear method offers a more refined solution!

Answer №5

Wow, that test case was quite complex. Below is a simplified and functional multi-line version:

<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Home Page</TITLE>
<STYLE type="text/css">
.left {
float: left;
}
.right {
float: right;
}
.clear {
clear:both;
}
</STYLE>
</HEAD>
<BODY>
<DIV>
<P class="left clear">Left</P>
<P class="right">Right</P>
<P class="left clear">Left</P
<P class="right">Right</P>
<P class="left clear">Left</P>
<P class="right">RightRightRightRightRightRight RightRight RightRight RightRight</P>
</DIV>
</BODY>
</HTML>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

No response from jQuery's $.getJSON() function

I'm currently experimenting with jQuery by using a script that I wrote. As a beginner in learning jQuery, I am trying to read data from a .json file and display it in a div. function jQuerytest() { $.getJSON( "books/testbook/pageIndex.json", func ...

CSS fails to display image within PHP script

Having an issue with displaying CSS through a PHP file. Everything seems to be working fine until I try to display an image from the database. The source code does accept the value from the DB, but the image doesn't show up. Interestingly, the image d ...

Avoid unnecessary extra margin on CSS div elements

Currently working with Bootstrap4. In a row with 2 col-md-6 (totaling 12 columns for the row). However, experiencing an unwanted margin on the left column as shown in the image below. How can I remove it? https://i.sstatic.net/u5oix.png html: <hea ...

What is the best way to unselect a checkbox that is already checked on an HTML page using AngularJS?

I've created checkboxes in my HTML code like this: <div ng-repeat="fruit in fruits"> <input type="checkbox" name="{{fruit.name}} ng-model="fruit.value" ng-checked="isChecked(fruit)" ng-change="changed(fruit)" ng-required="true" ...

Organize items within a compartment using Bootstrap3

I have decided to update my website to Bootstrap3 and encountered a common issue that many others seem to be facing as well. I am trying to evenly distribute 4 elements, each approximately 220px wide, inside a 990px container. In the old version of Bootstr ...

Implementing color transitions in javascript

Everyone talks about transitioning things in CSS, but have you ever thought about transitioning background colors in JavaScript? I'm currently working on a function that changes the background color of a div with the id of ex1 to green. However, I&apo ...

Should the username be specified but the password left blank, then an error message will be shown

<div class="form"> <h1>Sign In</h1> <form action="" method="post" name="login"> <input type="text" name="username" placeholder="Enter username" required /> <input type="password" name="password" placeholder="Enter password" ...

Using the function to show content by calling its assigned ID

<script type="text/javascript"> function selectRow(id){ $.get("http://inactive/test.php?id=" + id, function(data,status){ return data; }); } </script> <script type="text/javascript"> $("tr").click(function() { window.l ...

Add design to the footer during a specific event using Javascript

Here is the CSS code I am working with: .footer { font-family: "lato", sans-serif; padding: 20px; line-height: 1.2; text-align: right; background: #eee; color: #0D47A1; font-size: 13px; height: 80px; position: fixed; right: 0px; botto ...

Guide on how to load just the content section upon clicking the submit button

Here is an example of my code: <html> <head> <title>Test Loading</title> </head> <body> <div id="header"> This is header </div> <div id="navigation" ...

What could be the reason my header is not displaying in specific browsers?

For some reason, I am encountering an issue with displaying my banner on all browsers except Safari. When I use the inspect element feature, it shows that the banner is there, but it's not visible on the screen. Here is the HTML and CSS code for the h ...

WebSlides experiencing toolbar misalignment issues while embedding Bokeh visualizations

My goal is to integrate Bokeh plots into portable html slideshows using WebSlides (unlike Reveal.js, which doesn't meet my requirements). Below is a visual representation of the issue I am encountering, with the toolbar appearing misaligned: https:/ ...

What is the process for configuring my CSS and JS files to send HTTP response headers?

During our security evaluation of a web application built in Laravel 4, we discovered that the Anti-MIME-Sniffing header X-Content-Type-Options was not properly configured to 'nosniff'. The following PHP code snippet sets the necessary HTTP heade ...

Enhanced compatibility with Touch.radiusX feature on smartphone and tablet devices

Curious if anyone knows about the level of support for this property. I am currently using the PR0C0D1N6 app on an iPhone 4 and based on the specifications, when radiusX is not supported it should default to 1. However, in my case, radiusX is showing as un ...

Adjusting the height of a textarea within a table

My textarea's height is supposed to be 500%, but it's not changing. I suspect that being inside a table might have something to do with the issue, but I'm unsure of what needs to be adjusted to set the height correctly. Surprisingly, the wid ...

Troubleshooting the inability to set percentage values in Bootstrap styling

My goal is to create a bar with thin bars, but my use of Bootstrap limits my control over sizes. <template> <div class="container-fluid mx-auto"> <div class="row"> <div class="square rounded-pill&qu ...

Updating HTML label values using jQuery in a loop based on their index position

I am having an issue with my ajax call where I want to loop through each label in a class and set their value to the response returned from the server. However, the current code sets all labels to the same value instead of setting individual values based o ...

Issue with reactivity not functioning as expected within VueJS loop without clear explanation

Struggling with implementing reactivity in vue.js within a loop? The loop renders fine, but firing an event updates the content without visibly rendering the data on the page. The latest version of vue.js is being used with bootstrap and jquery. Despite a ...

Automating the process of posting a file to a form with javascript

I have a piece of client-side JavaScript that creates a jpeg file through HTML5 canvas manipulation when the user clicks an "OK" button. My goal is to automatically insert this jpeg output into the "Upload Front Side" field in a form, simulating a user up ...

Leverage Html5 to open and retrieve data from an Excel document

Is there a method to access excel file data using HTML5 while uploading the file on the client side? I have come across the use of reader in JavaScript, but unsure how it can be helpful in this scenario. Is there a way to read excel file data seamlessly ...