我想在移动设备和平板电脑设备上更改导航栏的background-color
。我尝试了这段代码,但它不起作用:
/* Custom Styles On Mobile And Tablet Devices */
@media (max-width: 767px) {
#nav_wrapper{
background-color:#fff;
}
}
/* Normal Styles On Desktop */
#nav_wrapper{
background-color:#377D7A;
opacity:0.5;
filter:alpha(opacity=70);
}
我只是想知道,为什么这个不起作用,如何在 sm 和 md 设备上更改导航栏的设置......
注意:我的 navbar 元素的ID为nav_wrapper
:
<nav id="nav_wrapper" class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="BKoodakBold leftlinks"><a href="#"><span class="glyphicon glyphicon-user leftlinks"></span> ورورد به اکانت کاربری</a></li>
</ul>
<ul class="nav navbar-nav navbar-center">
<a class="navbar-brand" href="#"><img src="assets/img/logo.png" width="50" height="30"></a>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="BKoodakBold rightlinks"><a href="#">درباره ما <span class="glyphicon glyphicon-info-sign"></span></a></li>
<li class="BKoodakBold rightlinks"><a href="#">میراث گویانت <span class="glyphicon glyphicon-gift"></span></a></li>
<li class="BKoodakBold rightlinks"><a href="#">صفحه اصلی <span class="glyphicon glyphicon-globe"></span></a></li>
</ul>
</div>
</div>
</nav>
答案 0 :(得分:2)
解决方案非常简单。你的CSS是正确的 - 它只是有点颠倒。只需交换您的CSS块,以便在设置元素样式后指定媒体查询:
#nav_wrapper{
background-color:#377D7A;
opacity:0.5;
filter:alpha(opacity=70);
}
@media (max-width: 767px) {
#nav_wrapper{
background-color:#fff;
}
}
不要为此使用JS而烦恼。 CSS正在运行,意味着开销更少。