Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - display two text blocks on the same line

How can I position two text blocks on the same line, one to the left and the one to the right, without using floats?

Like this:

Bla bla                   5
Whatever                 25
Boo                      12

Each line is a list item - <LI>

like image 319
Alex Avatar asked Jan 11 '11 01:01

Alex


People also ask

How do I put two text on the same line in CSS?

You can use a table :) If they're all inline elements, setting their display:inline; property should work.

How do I put two div boxes in the same line?

To display multiple div tags in the same line, we can use the float property in CSS styles. The float property takes left, right,none(default value) and inherit as values. The value left indicates the div tag will be made to float on the left and right to float the div tag to the right.


5 Answers

You have numbers in your example, which indicates to me that you may be showing tabular data. If that's the case, use a table. Easy!

If that's not the case, use spans with a set width:

.label {
     display: inline-block;
     width: 200px; // or whatever
}

and:

<ul>
    <li>
        <span class="label">Blah blah</span>
        5
    </li>
    <li>
        <span class="label">Stuff</span>
        25
    </li>
</ul>
like image 126
keithjgrant Avatar answered Oct 06 '22 21:10

keithjgrant


You could use tables. Also, you could use absolute positioning and setting the same value for top and bottom to each ul object

like image 39
SuperIRis Avatar answered Oct 06 '22 23:10

SuperIRis


you can see a working example here

ul {
    list-style-type:none;
}
li span {
    display:inline-block;
    width:100px;
}

<ul>
    <li>
        <span>Bla Bla</span>
        <span>5</span>
    </li>
    <li>
        <span>Whatever</span>
        <span>25</span>
    </li>
</ul>
like image 39
Omer Bokhari Avatar answered Oct 06 '22 21:10

Omer Bokhari


I know this is an old one, but it would seem like the most semantically correct option here would be to use DL, DT and DT instead of LI.

<dl>
<dt>Bla bla</dt><dd>5</dd>
<dt>Whatever</dt></dd>25</dd>
<dt>Boo</dt><dl>12</dl>
</dl>

Then you would style it like this:

dl {
    margin: 0;
}

dt {
    clear: left;
    float: left;
    padding: 0 0 2px;
    font-weight: bold;
}

dd {
    float: left;
    margin-left: 10px;
    padding: 0 0 2px;
}

I know you stipulated no floats, but that is the way to get the job done.

like image 23
Kenneth Spencer Avatar answered Oct 06 '22 21:10

Kenneth Spencer


Here is a very simple trick I used. It does use floats, but I think it still looks pretty nice:

HTML:

<h1 class="title1">Chicken Craft</h1>
<h1 class="title2">Minecraft Server</h1>

CSS:

.title1 {
  text-align: left;
  color: #269CEB;
  margin-top: 0px;
  float: left;
  clear: both;
}

.title2 {
  text-align: left;
  color: #FF9D00;
}
like image 20
Vance Avatar answered Oct 06 '22 22:10

Vance