Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style two groups of <li> in a single line?

I want to have two groups of list items in one line, so that one group starts from left to right and another from right to left; like so:

[li1 li2 li3]........................................[li6 li5 li4]

I have tried this:

li{
  display:inline;
 }
#left{
  float:left;
 }
#right{
  float:right;
  direction:rtl;
 }
<ul>
  <span id="left">
    <li> item1</li>
    <li> item2</li>
    <li> item3</li>
    </span>
  <span id="right">
    <li> item4</li>
    <li> item5</li>
    <li> item6</li>
    </span>
</ul>

The output works, but the code is not valid, since it uses a <span> directly under a <ul>.

What's the valid code for this?

like image 794
Majid Avatar asked May 09 '16 16:05

Majid


People also ask

How do I display Li in the same line?

How align Li tag in HTML? Write style="text-align:center;" to parent div of ul. Write style="display:inline-table;" to ul. Write style="display:inline;" to li.

How do I put two divs on the same line?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How do I make a list on the same line?

The quickest way to display a list on a single line is to give the <li> elements a display property value of inline or inline-block . Doing so places all the <li> elements within a single line, with a single space between each list item.

How do I keep an element on the same line in HTML?

To get all elements to appear on one line the easiest way is to: Set white-space property to nowrap on a parent element; Have display: inline-block set on all child elements.


2 Answers

You are correct about <span> directly under a <ul> being invalid. Here's a few solutions:

Give float to the <li> element directly.

li{
  display:inline;
  float: left;
 }
.right{
  float:right;
  direction:rtl;
 }
<ul>
    <li> item1</li>
    <li> item2</li>
    <li> item3</li>
    <li class="right"> item4</li>
    <li class="right"> item5</li>
    <li class="right"> item6</li>
</ul>

Use nth-of-type or nth-child.

I think this is a nice and clean solution. Though you'd have to change the css if you add/remove items in the list:

li {
  display: inline;
  float: left;
}
li:nth-of-type(n+4) {
  float: right;
}
<ul>
  <li>item1</li>
  <li>item2</li>
  <li>item3</li>
  <li>item4</li>
  <li>item5</li>
  <li>item6</li>
</ul>

Also, it looks like you can omit the direction: rtl from your code.

like image 107
Chris Avatar answered Sep 21 '22 15:09

Chris


Try something like this;

<ul>
    <li class="left"> item1</li>
    <li class="left"> item2</li>
    <li class="left"> item3</li>
    <li class="right"> item4</li>
    <li class="right"> item5</li>
    <li class="right"> item6</li>
</ul>



ul li {
  margin-left:10px;
  margin-right:20px;
}
.left {
  float:left;
}
.right {
  float:right;
  direct:rtl;
}

You are just giving all items with a left class the float left rule, and all items with the right class a float right rule (and then changing the direction like you were originally). This way you don't have to add any extra markup or wrap the li's in a li or anything.

like image 31
James Tudsbury Avatar answered Sep 21 '22 15:09

James Tudsbury