如何将左边的导航栏中除了一个项目以外的所有项目对齐,从左边距开始并使用固定的最小和最大宽度组合,比如300px和400px,但最后一项是否发送到右边距?
我需要结果如下:
目前,我有四个项目,HTML和CSS如下:
HTML
<div id="nav">
<ul>
<li><a href="/">One</a></li>
<li><a href="/">Two</a></li>
<li><a href="/">Three</a></li>
<li><a href="/">Four</a></li>
</ul>
</div>
CSS
#nav span {
display: inline-block;
position: relative;
}
#nav ul {
text-align: justify;
max-width: 400px;
}
#nav ul:after {
margin-left: 100%;
content: "";
}
#nav ul li {
display: inline;
}
请注意,我使用this question的接受答案中给出的技术证明了400px最大宽度内的元素。目前400px中有四个元素,但我需要将第四个元素输出并将其发送到右侧。
答案 0 :(得分:1)
您可以在display: flex
上使用ul
,并将margin-left: auto
和text-align: right;
放在最后li
元素上。我对li元素进行了大小限制,以满足您的尺寸要求,最小100px(300的三分之一)和最大133px(约400px的三分之一):
#nav ul {
width: 100%;
padding: 0;
background: #ccc;
display: flex;
}
#nav ul li {
display: inline;
box-sizing: border-box;
min-width: 100px;
max-width: 133px;
padding: 5px 10px; /* only to make it look better */
}
#nav ul li:last-child {
margin-left: auto;
text-align: right;
}
&#13;
<div id="nav">
<ul>
<li><a href="/">One</a></li>
<li><a href="/">Two</a></li>
<li><a href="/">Three</a></li>
<li><a href="/">Four</a></li>
</ul>
</div>
&#13;
使用浮点数的替代解决方案(在评论后要求非CSS3解决方案):
#nav ul {
width: 100%;
padding: 0;
background-color: #ccc;
overflow: hidden;
}
#nav ul li {
float: left;
list-style: none;
box-sizing: border-box;
min-width: 100px;
max-width: 133px;
padding: 5px 10px;
/* only to make it look better */
}
#nav ul li:last-child {
float: right;
text-align: right;
}
&#13;
<div id="nav">
<ul>
<li><a href="/">One</a>
</li>
<li><a href="/">Two</a>
</li>
<li><a href="/">Three</a>
</li>
<li><a href="/">Four</a>
</li>
</ul>
</div>
&#13;
答案 1 :(得分:0)
希望它可以帮到你
<html lang="en">
<head>
<title>Bootstrap Case</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Test</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="#">One</a></li>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Two<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Two 1-1</a></li>
<li><a href="#">Two 1-2</a></li>
<li><a href="#">Two 1-3</a></li>
</ul>
</li>
<li><a href="#">Three</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Four</a></li>
</ul>
</div>
</nav>
</body>
</html>
&#13;
注意:点击整页查看
答案 2 :(得分:0)
#nav {
background: #F9F9F9;
}
#nav ul {
list-style: none;
margin: 0;
padding: 0;
display: table;
table-layout: fixed;
width: 100%;
}
#nav li {
display: table-cell;
width: 60px;
text-align: center;
border: 1px solid #CCC;
}
#nav li:last-child {
width: auto;
text-align: right;
}
#nav a {
display: inline-block;
padding: 7px;
}
&#13;
<div id="nav">
<ul>
<li><a href="/">One</a></li>
<li><a href="/">Two</a></li>
<li><a href="/">Three</a></li>
<li><a href="/">Four</a></li>
</ul>
</div>
&#13;
答案 3 :(得分:0)
<强> Codepen 强>
你的意思是这样吗?
尝试使用此代码,只需保持标记相同。
#nav ul {
text-align: justify;
max-width: 400px;
float:left;
display:block;
position:relative;
}
#nav ul li
{
display:inline-block;
float:left;
}
#nav ul li a
{
text-decoration:none;
color:#242424;
}
#nav ul li:last-of-type {
position:absolute;
right:-1200px;
}
#nav ul {
text-align: justify;
max-width: 400px;
float:left;
display:block;
position:relative;
}
#nav ul li
{
display:inline-block;
float:left;
}
#nav ul li a
{
text-decoration:none;
color:#242424;
}
#nav ul li:last-of-type {
position:absolute;
right:-1200px;
}