如何更改下拉菜单文本的文本位置?导航栏下拉菜单背景颜色不透明?

时间:2017-03-14 22:23:28

标签: html css

Q1 如何在我的导航栏下拉菜单文本中心,当我尝试使用text-align:center;时,它会影响我不想要的主导航栏文本。

Q2 当用户将鼠标悬停在下拉菜单上的页面名称时,如何更改背景颜色的不透明度?

当用户将鼠标悬停在下拉菜单中的某个页面上时,白色背景会影响下拉菜单中的白色文本。另外,我想知道如何为这部分创建它自己的CSS,因为我认为如果你点击导航栏上的下拉菜单改变不透明度,主导航栏会受到影响当导航栏上文本的背景颜色变为白色时。

以下是我的代码。

HTML

    <!DOCTYPE html>
    <html>

    <head>
        <html lang="en">
        <meta charset="UTF-8">
        <title>Liam Docherty | London Web Developer &amp; GFX designer</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <link rel="stylesheet" href="css/style.css">
    </head>

     <body>
            <nav 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>
                        <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="#section3">Contact</a>
                                        </li>
                                        <li><a href="#">Something else here</a>
                                        </li>
                                        <li><a href="#">Separated link</a>
                                        </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><a href="#">Separated link</a>
                                        </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>


        <section class="section1">
            <div class="hero"></div>




            <a href= ".section2"><i class="fa fa-angle-down" style="font-size:100px;"></i></a>

        </section>
        <section class="section2">



            <a href = ".section2"> <i class="fa fa-angle-down" style="font-size:100px;"></i></a>

        </section>

        <section class="section3" id="section3">

            <a href = "#bs-example-navbar-collapse-1"> <i class="fa fa-angle-up" style="font-size:100px;"></i></a>

        </section>
        <script   src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="   crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    </body>

    <footer>
        <div class="page">
            <h1 class="footer">Copyright © 2017 Liam Docherty's Site. All rights reserved.</h1>
        </div>
     </footer>
    </html>

CSS

<style>
    body {
      margin: 0;
      padding: 0;
    }
    .navbar.navbar-default {
      background-color: #4D5061;
      height: 10vh;
      z-index: 100;
    }
    .navbar.navbar-default ul {
      list-style-type: none;
      text-align: right;
    }
    .navbar.navbar-default ul li {
      display: inline-block;
    }

    .dropdown .dropdown-menu {
      background-color: #4D5061;
    }
    .dropdown .dropdown-menu a {
      color: white;
    }
    .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: #FFFFFF;
      border-radius: 9px;
      transition: all .2s;
    }
    .navbar.navbar-default ul li a:hover {
      color: white;
    }
    .nav.navbar-nav,
    .nav.navbar-nav>li {
      float: none;
    }
    .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;
      height: 95vh;
    }
    .section1 {
      height: 100vh;
      text-align: center;
      color: white;
      background-image: url("https://static.pexels.com/photos/48727/pexels-photo-48727.jpeg");
      background-attachment: fixed;
    }
    .section2 {
      //height: 95vh;
      background-color: #A59E8C;
      text-align: center;
      color: white;
    }
    .section3 {
      //height: 95vh;
      background-color: #A59E8C;
      text-align: center;
      color: white;
    }
    .fa-angle-down {
      color: #4D5061;
      position: absolute;
      bottom: 0px;
    }
    .fa-angle-up {
      color: #4D5061;
      position: absolute;
      bottom: 0px;
    }
    .footer {
      height: 5vh;
      background-color: #4D5061;
      text-align: center;
      padding:0;
      right:0;
      bottom:0;
      left:0:
    }
    h1{
      font-size: 14pt;
      margin:0;
      color: white;
      font-family: 'Roboto', sans-serif;
    }
    .navbar.navbar-default ul.dropdown-menu li,
    .navbar.navbar-default ul.dropdown-menu li a {
      position: relative;
      display: block;
    }
    </style>

3 个答案:

答案 0 :(得分:0)

要设置下拉列表的样式而不设置主导航样式,请将其下拉列表自己的类名称。然后就可以了

 .navbar .navbar-default .customClassName{
     ...styles for just the drop down
}

答案 1 :(得分:0)

由于您的填充,您的放置菜单项文本似乎偏离中心。你的填充(3.5vh 8px 4px)给出了不同的顶部和底部填充。 Vh取屏幕尺寸的百分比,而4px是静态量。如果你在顶部和底部使用相同的填充,它应该工作(填充:4px 8px)像Hadas所说,考虑使用类来避免破坏你网站的其他部分。

答案 2 :(得分:0)

 .nav ul.dropdown-menu li a:hover {

    background-color: rgba(238, 238, 238, 0.75);

 }

要仅为背景颜色设置不透明度,您必须根据RGBA在悬停时编写背景颜色属性。使用http://hex2rgba.devoth.com/之类的工具将当前十六进制转换为具有不透明度级别的RGBA,并将background-color属性添加到元素的:悬停状态。

直接设置不透明度属性也会改变文本和内容的不透明度。