1

I was wondering how I can change colour of the nav bar text as it appears and when the user hovers over it? I'm aware I have to use CSS. However, I'm not sure of what class I have to pick from the html. Here is my codepen.

In addition, how can I centre my nav bar text therefore, it's in the centre? I have written the CSS to do all of the requested things. However, the problem identifying the class is my issue. As a result of this, can someone help me with identifying what class name needs to go where in my CSS please.

Furthermore, I have listed my HTML code of the navigation bar below.

<body>
        <nav class="navbar navbar-default">
            <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>
                    <a class="navbar-brand" href="#"><img src="http://placehold.it/60x60" alt="Your Brand Name"></a>
                </div>

                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                            <li><a href="#">Home</a>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">About Me <span class="caret"></span></a>
                                <ul class="dropdown-menu">
                                    <li><a href="#">Action</a>
                                    </li>
                                    <li><a href="#">Another action</a>
                                    </li>
                                    <li><a href="#">Something else here</a>
                                    </li>
                                    <li role="separator" class="divider"></li>
                                    <li><a href="#">Separated link</a>
                                    </li>
                                    <li role="separator" class="divider"></li>
                                    <li><a href="#">One more separated link</a>
                                    </li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Units <span class="caret"></span></a>
                                <ul class="dropdown-menu">
                                    <li><a href="#">Action</a>
                                    </li>
                                    <li><a href="#">Another action</a>
                                    </li>
                                    <li><a href="#">Something else here</a>
                                    </li>
                                    <li role="separator" class="divider"></li>
                                    <li><a href="#">Separated link</a>
                                    </li>
                                    <li role="separator" class="divider"></li>
                                    <li><a href="#">One more separated link</a>
                                    </li>
                                </ul>
                            </li>
                            <li><a href="#">Clients</a>
                            </li>
                            <li><a href="#contact-me">Contact Me</a>
                            </li>
                        </ul>
                      </div><!-- /.navbar-collapse -->
                    </div><!-- /.container-fluid -->
        </nav>

CSS code below.

<style>
    body {
        margin: 0;
        padding: 0;
    }
    .navbar.navbar-default {
        background-color: #4D5061;
        height: 10vh;
    }
    .navbar.navbar-default ul {
        list-style-type: none;
        text-align: center;
    }
    .navbar.navbar-default ul li {
        display: inline-block;
    }
    .navbar.navbar-default ul li a {
        display: inline-block;
        padding: 3.5vh 8px 4px;
        color: white;
        text-decoration: none;
        font-size: 14pt;
        font-family: 'Roboto', sans-serif;
    }
    .navbar.navbar-default ul li:after {
        content: '';
        position: absolute;
        right: 50%;
        bottom: 0;
        left: 50%;
        height: 3px;
        background-color:red;
        border-radius: 9px;
        transition:all .2s;
    }
    .navbar.navbar-default ul li:hover:after {
        right: 0;
        left: 0;
    }
    a:hover {
        color:red;
        text-decoration:none;
    }
    #logo {
        padding-top: 2vh;
        padding-left: 20px;
        float: left;
    }
    section {
        position: relative;
    }
    .section1 {
        height: 90vh;
        background-color: #FFFFFF;
        text-align: center;
    }
    .section2 {
        height: 95vh;
        background-color: #A59E8C;
        text-align: center;
        color: white;
    }
    .contact_section {
        height: 93vh;
        background-color: grey;
    }
    .logo {
        color: #000000;
        font-size: 200px;
    }
    .fa-angle-down {
        color: #4D5061;
        position: absolute;
        bottom: 0px;
    }
    footer {
        height: 10vh;
    }
</style>
Johannes
  • 64,305
  • 18
  • 73
  • 130

4 Answers4

2

To change the link hover color, this specificity will overwrite bootstrap's CSS with your existing markup.

.navbar-default.navbar .navbar-nav>li>a:hover {
  color: pink;
}

To center the nav text. You already have text-align: center; on .navbar.navbar-default ul and display: inline-block; on .navbar.navbar-default ul li so all you need to do is not float the ul and the li's

.nav.navbar-nav, .nav.navbar-nav>li {
  float: none;
}
Michael Coker
  • 52,626
  • 5
  • 64
  • 64
0

You have to change .navbar.navbar-default ul li a for the normal font-color.

.navbar.navbar-default ul li a {
    display: inline-block;
    padding: 3.5vh 8px 4px;
    color: white;                     <-- There you can change the color
    text-decoration: none;
    font-size: 14pt;
    font-family: 'Roboto', sans-serif;
}

To change the hover color (also focus):
.navbar-default .navbar-nav>li>a:focus, .navbar-default .navbar-nav>li>a:hover

Be sure to use color: #333 !important; because you overwrite the normal value.

To center your nav bar add this to your css:

.navbar .navbar-nav {
  display: inline-block;
  float: none;
  vertical-align: top;
}

.navbar .navbar-collapse {
  text-align: center;
}

Source: Center content in responsive bootstrap navbar

Community
  • 1
  • 1
Luca Jung
  • 1,440
  • 11
  • 25
0

1) If you just inspect it you can see

.navbar-default .navbar-nav>li>a:focus, 
.navbar-default .navbar-nav>li>a:hover {
    color: #333;
    background-color: transparent;
}

which is in "bootstrap.min.css" , so better way to change the color of text on hover is adding your own style like below

.navbar.navbar-default ul li a:hover {
    color: white;
}

2) About the second question, I prefer to use "display:flex". I add the style edits below. I hope you will understand.

.navbar.navbar-default>.container-fluid{
    display: flex;
    justify-content: center;
}
.navbar-header{
    margin-right:auto
}
.collapse .navbar-collapse{
    margin-right: auto;
}
Jamil Alisgenderov
  • 1,448
  • 2
  • 12
  • 23
0

To change the color of your navbar' links:

.navbar.navbar-default ul li a:hover {
  color: green;
}

To center your navbar (replaced my answer by Michael Coker's answer, as his solution is far better):

.nav.navbar-nav, .nav.navbar-nav>li {
  float: none;
}
Community
  • 1
  • 1
Dinei
  • 4,494
  • 4
  • 36
  • 60