Search code examples
htmlcsstwitter-bootstrapbackground-imagebackground-color

Navigation bar background image remains overwritten by background color


Trying to create a simple tabbed site using Bootstrap, and I would like the navigation bar at the top to have a background image. However, the background image never appears. This is unlikely to be a path issue, since (a) the IDE (IntelliJ IDEA) finds the relative path properly in the project, and (b) image URL using http://... doesn't work either. It seems the background-image property never gets recognized.

Here is what I have so far:

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

I understand that (and please correct me if I am mistaken) main.css should override specifications in bootstrap.css. But, if I remove the background: rgba(52, 73, 94, 0.7); line from main.css, the result is just the default specified in boostrap.css (which is Bootstrap v3.3.4, in case that is relevant here).


Solution

  • The issue may have been caused by

    1. The image being very big. You can use the background-size property to ensure it will fit into the space.

    or

    1. The formatting of your comments was wrong. In CSS, comments look like this: /* a comment */

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