如何让导航项目的背景完全被颜色填充,而不仅仅是其中的一部分?
这是我尝试使用Bootstrap药丸,但它不能完成这项工作。另外,我尝试了自己的CSS,但它和药丸一样。
总而言之,如何仅使用导航项CSS的背景颜色,使我的导航栏(图像中的 )看起来像this one。
更新:添加了HTML和CSS代码
我的代码: HTML
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#"><i class="fa fa-paw" aria-hidden="true"></i> Purrfection Shop</a>
<!--<i class="fa fa-home" aria-hidden="true"></i>
<i class="fa fa-sign-in" aria-hidden="true"></i>
<i class="fa fa-sign-out" aria-hidden="true"></i>-->
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto nav-pills">
<li class="nav-item">
<a class="nav-link" href="#jumbotron">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#services">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#about-container">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#footer">Contact</a>
</li>
</ul>
<ul class="navbar-nav ml-auto nav-pills">
<li class="nav-item">
<a class="nav-link" href="#!"><i class="fa fa-user-plus" aria-hidden="true"></i> Sign Up</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#!"><i class="fa fa-user" aria-hidden="true"></i> Login</a>
</li>
</ul>
</div>
</nav>
CSS:
.navbar {
border-bottom: 1px solid #F5F5F5;
background-color: white;
text-transform: uppercase;
letter-spacing: 2px;
}
答案 0 :(得分:11)
关键是从导航栏中删除顶部和底部填充,并将其添加到药丸中,从药丸中移除圆角,然后将自定义cssin包装在媒体查询中以使其响应。
这是JSfiddle。
这是代码:
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<style type="text/css">
@media only screen and (min-width: 991px) {
.navbar {
padding-top: 0;
padding-bottom: 0;
}
.nav-pills .nav-link {
padding-top: 15px;
padding-bottom: 15px;
border-radius: 0;
}
}
</style>
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">
</span>
</button>
<a class="navbar-brand" href="#">
<i class="fa fa-paw" aria-hidden="true">
</i> Purrfection Shop</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto nav-pills">
<li class="nav-item">
<a class="nav-link active" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
答案 1 :(得分:2)
查看此代码:[JS FIddle Here] 的 HTML 强>
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">
</span>
</button>
<a class="navbar-brand" href="#">
<i class="fa fa-paw" aria-hidden="true">
</i> Purrfection Shop</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto nav-pills">
<li class="nav-item">
<a class="nav-link active" href="#jumbotron">Home
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#services">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#about-container">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#footer">Contact</a>
</li>
</ul>
<ul class="navbar-nav ml-auto nav-pills">
<li class="nav-item">
<a class="nav-link" href="#!"><i class="fa fa-user-plus" aria-hidden="true"></i> Sign Up</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#!"><i class="fa fa-user" aria-hidden="true"></i> Login</a>
</li>
</ul>
</div>
</nav>
<强> CSS 强>:
@media only screen and (min-width: 991px) {
.navbar {
padding-top: 0;
padding-bottom: 0;
}
.active {
background: blue;
}
.nav-item {}
.nav-link {
padding-top: 15px;
padding-bottom: 15px;
}
}
.active {
background: blue;
}
答案 2 :(得分:-1)
请在下面查看fiddle.use展开结果视图
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<style>
.navbar-light .navbar-nav .active>.nav-link, .navbar-light .navbar-nav .nav-link.active, .navbar-light .navbar-nav .nav-link.open, .navbar-light .navbar-nav .open>.nav-link{background-color: #0275D8;color: #fff;}
.navbar{padding: 0 1rem;}
.navbar-toggleable-md .navbar-nav .nav-link{padding: 10px 12px;}
</style>
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
</nav>
&#13;