导航栏背景图像仍然被背景颜色覆盖

时间:2018-06-02 20:04:25

标签: html css twitter-bootstrap background-image background-color

尝试使用Bootstrap创建一个简单的选项卡式网站,我希望顶部的导航栏有一个背景图像。但是,背景图像永远不会出现。这不太可能是路径问题,因为(a)IDE(IntelliJ IDEA)在项目中正确找到相对路径,(b)使用http://...的图像URL也不起作用。似乎背景图像属性永远不会得到识别。

这是我到目前为止所做的:

.navbar > .container .navbar-brand, .navbar > .container-fluid .navbar-brand {
  margin-left: -8px;
  height: 46px;  <!-- navbar-height -->
}

.navbar-custom {
  background: rgba(52, 73, 94, 0.7);  <!-- navbar-bgcolor -->
  background-image: url("https://jessicasse.files.wordpress.com/2012/10/distant-lights-1-1152x8641.jpg");
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 3;
  font-family: 'Lato', 'Open Sans', 'Helvetica Neue', Helvetica, Calibri, Arial, sans-serif;  <!-- heading font -->
}

.navbar-custom .navbar-brand {
  font-size: inherit;
  font-weight: lighter;
  font-kerning: auto;
  font-family: 'Lato', 'Open Sans', 'Helvetica Neue', Helvetica, Calibri, Arial, sans-serif;  <!-- heading font -->
}

.navbar-custom .nav li a {
  font-size: 14px;
  font-weight: lighter;
  letter-spacing: 1px;
}

.navbar-default .navbar-nav > .current > a,
.navbar-default .navbar-nav > .current > a:hover,
.navbar-default .navbar-nav > .current > a:focus {
  font-weight: bolder;
}

.navbar-default .navbar-nav > li > a {
  color: ghostwhite;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="My Home Page">
  
  <title>My Home Page</title>
  
  <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
  <link rel="stylesheet" type="text/css" href="assets/css/bootstrap-theme.css">
  <link rel="stylesheet" type="text/css" href="assets/css/font-awesome.css">
  <link rel="stylesheet" type="text/css" href="assets/css/main.css">
  
  <!-- Custom Fonts -->
  <link rel="stylesheet" type="text/css" href="assets/fonts/Lato:300,400,700,300italic,400italic.css">
  <link rel="stylesheet" type="text/css" href="assets/fonts/Lora:400,700,400italic,700italic.css">

</head>

<body>
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header page-scroll">
      <div>
        <a style="color: ghostwhite; font-size: xx-large; font-weight: lighter" href="">This Page</a>
        <br>
        <p>
          <span style="color: ghostwhite; font-size: medium; font-weight: lighter">Job Title</span><br>
          <span style="color: ghostwhite; font-size: medium; font-weight: lighter">Company</span>
        </p>
      </div>
    </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 navbar-right">
        <li class="current"><a href="#">Home</a></li>
        <li><a href="research.html">Research</a></li>
        <li><a href="teaching.html">Teaching</a></li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container -->
</nav>

</body>
</html>

我理解(如果我弄错了,请纠正我)main.css应该覆盖bootstrap.css中的规范。但是,如果我从background: rgba(52, 73, 94, 0.7);中删除main.css行,则结果只是boostrap.css中指定的默认值(如果此处相关,则为Bootstrap v3.3.4)。

1 个答案:

答案 0 :(得分:1)

问题可能是由

引起的
  1. 图像非常大。您可以使用background-size属性来确保它适合空间。
    1. 您的评论格式错误。在CSS中,注释如下所示:/ *注释* /
    2. .navbar > .container .navbar-brand, .navbar > .container-fluid .navbar-brand {
        margin-left: -8px;
        height: 46px;  
      }
      
      .navbar-custom {
        background: rgba(52, 73, 94, 0.7);  
        background-image: url("https://jessicasse.files.wordpress.com/2012/10/distant-lights-1-1152x8641.jpg");
        background-size: cover;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        z-index: 3;
        font-family: 'Lato', 'Open Sans', 'Helvetica Neue', Helvetica, Calibri, Arial, sans-serif; 
      }
      
      .navbar-custom .navbar-brand {
        font-size: inherit;
        font-weight: lighter;
        font-kerning: auto;
        font-family: 'Lato', 'Open Sans', 'Helvetica Neue', Helvetica, Calibri, Arial, sans-serif;  <!-- heading font -->
      }
      
      .navbar-custom .nav li a {
        font-size: 14px;
        font-weight: lighter;
        letter-spacing: 1px;
      }
      
      .navbar-default .navbar-nav > .current > a,
      .navbar-default .navbar-nav > .current > a:hover,
      .navbar-default .navbar-nav > .current > a:focus {
        font-weight: bolder;
      }
      
      .navbar-default .navbar-nav > li > a {
        color: ghostwhite;
      }
      <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="My Home Page">
        
        <title>My Home Page</title>
        
        <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="assets/css/bootstrap-theme.css">
        <link rel="stylesheet" type="text/css" href="assets/css/font-awesome.css">
        <link rel="stylesheet" type="text/css" href="assets/css/main.css">
        
        <!-- Custom Fonts -->
        <link rel="stylesheet" type="text/css" href="assets/fonts/Lato:300,400,700,300italic,400italic.css">
        <link rel="stylesheet" type="text/css" href="assets/fonts/Lora:400,700,400italic,700italic.css">
      
      </head>
      
      <body>
      <nav class="navbar navbar-default navbar-custom navbar-fixed-top">
        <div class="container-fluid">
          <!-- Brand and toggle get grouped for better mobile display -->
          <div class="navbar-header page-scroll">
            <div>
              <a style="color: ghostwhite; font-size: xx-large; font-weight: lighter" href="">This Page</a>
              <br>
              <p>
                <span style="color: ghostwhite; font-size: medium; font-weight: lighter">Job Title</span><br>
                <span style="color: ghostwhite; font-size: medium; font-weight: lighter">Company</span>
              </p>
            </div>
          </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 navbar-right">
              <li class="current"><a href="#">Home</a></li>
              <li><a href="research.html">Research</a></li>
              <li><a href="teaching.html">Teaching</a></li>
            </ul>
          </div>
          <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
      </nav>
      
      </body>
      </html>