Reputation: 126327
Given the following HTML,
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
Give me the best CSS (without changing the HTML) to position the elements like so:
Home About Blog Contact
The first list item should be left-aligned. The rest should be right-aligned.
Don't just position everything absolutely. For example, if I want to change "Blog" to "My Blog," then I shouldn't have to change the CSS for things to stay looking good.
Upvotes: 0
Views: 1869
Reputation: 13978
You can achieve like below.
HTML
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
CSS
ul
{
float:right;
width:100%;
display:table;
}
ul li
{
list-style-type:none;
padding:0px 10px;
background-color:#ff00ff;
text-align:right;
display:table-cell;
width:1px;
}
ul li:first-child
{
display:table-cell;
float:left;
width:auto;
}
Upvotes: 1
Reputation: 126327
This works but doesn't ignore the whitespace between the inline-block list items.
ul {
margin: 0 auto;
padding: 0;
text-align: right;
width: 1024px;
}
ul > li {
display: inline-block;
}
ul > li:first-child {
float: left;
}
ul > li > a {
display: block;
padding: 14px 21px;
}
Upvotes: 0