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?
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.
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.
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.
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.
You are correct about <span>
directly under a <ul>
being invalid. Here's a few solutions:
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>
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With