我有两个问题:
我可以设置右边的高度吗?
我可以隐藏最后一个边界吗?
body {
margin: 0;
padding: 0;
}
#banner {
height: 30px;
width: 750px;
margin: 0 auto;
background-color: aquamarine;
}
#banner ul {
list-style: none;
height: 30px;
padding: 0;
}
#banner ul li {
float: left;
margin: auto 0;
height: 30px;
line-height: 30px;
width: 100px;
text-align: center;
border-right: 1px solid #aba;
}

<div id="banner">
<ul>
<li>Home</li>
<li>link</li>
<li>product</li>
<li>phone</li>
<li>cat</li>
<li>about</li>
</ul>
</div>
&#13;
答案 0 :(得分:2)
这是你的边界:
li { border-right: 1px solid #aba; }
但是你不希望它出现在最后一个项目上。所以试试这个:
li + li {
border-left: 1px solid #aba;
}
新规则将左侧边框应用于紧跟在另一个li
之后的li
个元素。这将排除第一个li
上的左边框和上一个li
上的右边框。
您希望边框小于全高。您可以使用绝对定位的伪元素实现效果:
li {
position: relative; /* establish the containing block for abspos children */
}
li+li::before {
content: "";
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
height: 50%;
width: 2px;
background-color: red;
}
body {
margin: 0;
padding: 0;
}
#banner {
height: 30px;
width: 750px;
margin: 0 auto;
background-color: aquamarine;
}
#banner ul {
list-style: none;
height: 30px;
padding: 0;
}
#banner ul li {
float: left;
margin: auto 0;
height: 30px;
line-height: 30px;
width: 100px;
text-align: center;
/* border-right: 1px solid #aba; */
position: relative;
}
li+li::before {
content: "";
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
height: 50%;
width: 2px;
background-color: red;
}
&#13;
<div id="banner">
<ul>
<li>Home</li>
<li>link</li>
<li>product</li>
<li>phone</li>
<li>cat</li>
<li>about</li>
</ul>
</div>
&#13;
答案 1 :(得分:1)
试试这个:
li:not(:last-child):after {
content: "";
display: inline-block;
margin-left: 40px;
border-right: 3px solid orange;
height: 20px;
top: 5px;
position: absolute;
}
完整代码:
body {
margin: 0;
padding:0;
}
#banner {
height:30px;
width: 750px;
margin: 0 auto;
background-color: aquamarine;
vertical-align: middle;
}
#banner ul{
list-style: none;
height:30px;
padding: 0;
position: relative;
}
#banner ul li {
float: left;
margin: auto 0;
height: 30px;
line-height: 30px;
width: 100px;
text-align:center;
}
li:not(:last-child):after {
content: "";
display: inline-block;
margin-left: 40px;
border-right: 3px solid orange;
height: 20px;
top: 5px;
position: absolute;
}
&#13;
<div id="banner">
<ul >
<li>Home</li>
<li>link</li>
<li>product</li>
<li>phone</li>
<li>cat</li>
<li>about</li>
</ul>
</div>
&#13;
答案 2 :(得分:1)
ul {
display: flex;
}
li {
position: relative;
display: flex;
justify-content: center;
flex: 1;
padding: 10px 0;
background-color: gray;
list-style-type: none;
}
li::after {
content: "";
position: absolute;
top: 5px; /* control the 'height' with top/bottom */
bottom: 5px;
right: 0;
width: 5px; /*same as what your border was */
background-color: black; /* instead of border-color */
}
li:last-child::after { /*hides last 'border' */
display: none;
}
&#13;
<div>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</div>
&#13;
<强>拨弄强>
答案 3 :(得分:0)
要移除上一个li
的边框,请使用伪类:last-child
我在display:flex
的顶部和底部添加边距以使其居中后使用了align-item:center
和li
body {
margin: 0;
padding:0;
}
#banner {
height:30px;
width: 750px;
margin: 0 auto;
background-color: aquamarine;
}
#banner ul{
list-style: none;
height:30px;
padding: 0;
display: flex;
align-items: center;
}
#banner ul li {
float: left;
width: 100px;
text-align:center;
border-right:1px solid #aba;
margin-top:10px;
margin-bottom: 10px;
}
#banner ul li:last-child {
border-right: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<link rel="stylesheet" href="styles/index.css" type="text/css">
</head>
<body>
<div id="banner">
<ul >
<li>Home</li>
<li>link</li>
<li>product</li>
<li>phone</li>
<li>cat</li>
<li>about</li>
</ul>
</div>
</body>
</html>
答案 4 :(得分:-2)