我正在尝试为div添加底部边框以用于导航栏。我想要达到的效果:
目前,我有以下代码:
$("a").click(function() {
$("a").removeClass("current");
$(this).addClass("current");
});
.container {
}
.container .item {
float: left;
list-style-type: none;
margin: 0 1px;
}
.container .item a {
color: black;
text-decoration: none;
background-color: green;
width: 50px;
font-size: 13px;
text-align: center;
font-weight: bold;
display: table-cell;
vertical-align: middle;
height: 40px;
}
.container .item a.current {
border-bottom: 2px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="item">
<a class="current" href="#">Page 1</a>
</div>
<div class="item">
<a href="#">Page 2</a>
</div>
<div class="item">
<a href="#">Page 3</a>
</div>
<div class="item">
<a href="#">Page 4</a>
</div>
<div class="item">
<a href="#">Page 5</a>
</div>
<div class="item">
<a href="#">Page 6</a>
</div>
</div>
我找不到在div的内容和底部边框之间添加空格的方法,而不是与div背景颜色相同。
答案 0 :(得分:10)
目前看来你无法做到这一点。您不能在元素和它自己的边框之间添加间隙。但是,您可以将边框添加到其父元素(在本例中为div.item
元素),然后将padding-bottom
添加到同一元素以将其与a
元素分开:
$("a").click(function() {
$(".current").removeClass("current");
$(this).parent().addClass("current");
});
.container {
}
.container .item {
float: left;
list-style-type: none;
margin: 0 1px;
}
.container .item a {
color: black;
text-decoration: none;
background-color: green;
width: 50px;
font-size: 13px;
text-align: center;
font-weight: bold;
display: table-cell;
vertical-align: middle;
height: 40px;
}
.container .item.current {
border-bottom: 2px solid red;
padding-bottom: 4px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="item current">
<a href="#">Page 1</a>
</div>
<div class="item">
<a href="#">Page 2</a>
</div>
<div class="item">
<a href="#">Page 3</a>
</div>
<div class="item">
<a href="#">Page 4</a>
</div>
<div class="item">
<a href="#">Page 5</a>
</div>
<div class="item">
<a href="#">Page 6</a>
</div>
</div>
请注意,我还修改了您的JavaScript,将此.current
类添加到li
元素,而不是点击的a
元素。
答案 1 :(得分:4)
新css:
.container {
}
.container .item {
float: left;
list-style-type: none;
margin: 0 1px;
border-bottom: 8px solid red;
}
.container .item a {
color: black;
text-decoration: none;
background-color: green;
width: 50px;
font-size: 13px;
text-align: center;
font-weight: bold;
display: table-cell;
vertical-align: middle;
height: 40px;
border-bottom: 4px solid white;
}
.container .item a.current {
}
答案 2 :(得分:4)
使用:after
伪元素的另一个版本。与其他答案不同,这会将白色边框放在元素中,而不是将绿色推到外面。
我添加/更改了有趣的部分:
.container .item a {
...
position: relative;
}
.container .item a.current:after {
content:'';
position: absolute;
left: 0;
bottom: 2px;
height: 2px;
width: 100%;
background-color: #FFF;
}
这是一个演示:
$("a").click(function() {
$("a").removeClass("current");
$(this).addClass("current");
});
.container {
}
.container .item {
float: left;
list-style-type: none;
margin: 0 1px;
}
.container .item a {
color: black;
text-decoration: none;
background-color: green;
width: 50px;
font-size: 13px;
text-align: center;
font-weight: bold;
display: table-cell;
vertical-align: middle;
height: 40px;
position: relative;
}
.container .item a.current {
border-bottom: 2px solid red;
}
.container .item a.current:after {
content:'';
position: absolute;
left: 0;
bottom: 2px;
height: 2px;
width: 100%;
background-color: #FFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="item">
<a class="current" href="#">Page 1</a>
</div>
<div class="item">
<a href="#">Page 2</a>
</div>
<div class="item">
<a href="#">Page 3</a>
</div>
</div>
答案 3 :(得分:1)
不确定这是否是你想要的。试试这个。我在课堂上添加了一个div。这也可以使用方法后的css来完成。
$("a").click(function() {
$("a").removeClass("current");
$(this).addClass("current");
});
&#13;
.container {
}
.container .item {
float: left;
list-style-type: none;
margin: 0 1px;
}
.container .item a {
color: black;
text-decoration: none;
background-color: green;
width: 50px;
font-size: 13px;
text-align: center;
font-weight: bold;
display: table-cell;
vertical-align: middle;
height: 40px;
}
.box {
margin-top:2px;
height: 2px;
background-color:red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="item">
<a class="current" href="#">Page 1</a>
<div class="box"></div>
</div>
<div class="item">
<a href="#">Page 2</a>
</div>
<div class="item">
<a href="#">Page 3</a>
</div>
<div class="item">
<a href="#">Page 4</a>
</div>
<div class="item">
<a href="#">Page 5</a>
</div>
<div class="item">
<a href="#">Page 6</a>
</div>
</div>
&#13;