如何更改导航栏文本和中心导航栏文本的文本颜色?

时间:2017-03-04 22:05:55

标签: html css

我想知道如何更改导航栏文字的颜色,当用户将鼠标悬停在它上面时?我知道我必须使用CSS。但是,我不确定我必须从html中选择哪个类。这是我的codepen

此外,我如何居中我的导航条文本,它在中心? 我已经编写了CSS来完成所有请求的事情。但是,识别课程的问题是我的问题。因此,有人可以帮我识别需要在CSS中的哪个类名称。

此外,我已在下面列出了导航栏的HTML代码。

<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代码。

<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>

4 个答案:

答案 0 :(得分:2)

要更改链接悬停颜色,此特异性将使用现有标记覆盖bootstrap的CSS。

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

使导航文本居中。您已text-align: center;上的.navbar.navbar-default uldisplay: inline-block;上的.navbar.navbar-default ul li已经ul,所以您需要做的就是不要浮动li.nav.navbar-nav, .nav.navbar-nav>li { float: none; }

UIApplication.shared.keyWindow?.addSubview(MyUILabel)

答案 1 :(得分:0)

您必须更改.navbar.navbar-default ul li a作为普通字体颜色。

.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;
}

更改悬停颜色(也是焦点):
.navbar-default .navbar-nav>li>a:focus, .navbar-default .navbar-nav>li>a:hover

请务必使用color: #333 !important;,因为您会覆盖正常值。

要将导航栏居中,请将其添加到您的css:

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

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

来源:Center content in responsive bootstrap navbar

答案 2 :(得分:0)

1)如果您只是检查它,您可以看到

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

位于“bootstrap.min.css”中,因此在悬停时更改文本颜色的更好方法是添加自己的样式,如下所示

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

2)关于第二个问题,我更喜欢使用“display:flex”。我添加下面的样式编辑。我希望你能理解。

.navbar.navbar-default>.container-fluid{
    display: flex;
    justify-content: center;
}
.navbar-header{
    margin-right:auto
}
.collapse .navbar-collapse{
    margin-right: auto;
}

答案 3 :(得分:0)

更改导航栏链接的颜色:

.nav.navbar-nav, .nav.navbar-nav>li {
  float: none;
}

将导航栏居中(用Michael Coker's answer替换我的答案,因为他的解决方案要好得多):

try {
    # Load WinSCP .NET assembly
    Add-Type -Path 'WinSCPnet.dll'

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = '10.61.10.225'
        UserName = 'goofy'
        Password = 'changeme'
        SshHostKeyFingerprint = 'ssh-rsa 2048 d4:1c:1a:4c:c3:60:d5:05:12:02:d2:d8:d6:ae:6c:5d'
    }

    $session = New-Object WinSCP.Session

    try {
        # Connect
        $session.Open($sessionOptions)

        # Upload files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

        $transferResult = $session.PutFiles($outfile, "/home/public/somedir/somesubdir/$basename", $false, $transferOptions)

        # Throw on any error
        $transferResult.Check()

        # Print results
        foreach ($transfer in $transferResult.Transfers) {
            Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
        }
    } finally {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
} catch [Exception] {
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}