为了导航栏的目的,我试图为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.item元素),然后将padding-bottom添加到同一元素,以将其与元素分开:
$("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>