How can I change the styles of the navigation bar when it comes to small and medium devices?

advertisements

I want to change the background-color of my navbar when it comes to mobile and tablet devices. I tried this code but it does not work:

/* 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);
}

I just want to know, why this one does not work and how can I change the settings of my navbar on sm and md devices...

Note: My navbar element has the id of 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> &nbsp; ورورد به اکانت کاربری</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="#">درباره ما &nbsp; <span class="glyphicon glyphicon-info-sign"></span></a></li>
                    <li class="BKoodakBold rightlinks"><a href="#">میراث گویانت &nbsp; <span class="glyphicon glyphicon-gift"></span></a></li>
                    <li class="BKoodakBold rightlinks"><a href="#">صفحه اصلی &nbsp; <span class="glyphicon glyphicon-globe"></span></a></li>
                </ul>
            </div>
        </div>
    </nav>


The solution is very simple. Your CSS is correct - it's just a little upside-down. Just exchange your CSS blocks so you specify the media query AFTER styling your element:

#nav_wrapper{
    background-color:#377D7A;
    opacity:0.5;
    filter:alpha(opacity=70);
}

@media (max-width: 767px) {
    #nav_wrapper{
        background-color:#fff;
    }
}

Don't bother using JS for this. The CSS is working and means less overhead.