How to create: navigation bar on an image

Learn how to add a navigation menu on an image using CSS.

Try It Yourself

How to create a navigation bar on an image

Step 1 - Add HTML:

Create navigation bar:

<div class="bg-img">
  <div class="container">
    <div class="topnav">
      <a href="#home">Home</a>
      <a href="#news">News</a>
      <a href="#contact">Contact</a>
      <a href="#about">About</a>
    </div>
  </div>
</div>

Step 2 - Add CSS:

Set navigation bar style:

.bg-img {
  /* Image used */
  background-image: url("img_nature.jpg");
  min-height: 380px;
  /* Center the image and scale appropriately */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  /* Need to position the navigation bar */
  position: relative;
{}
/* Place the navigation bar container inside the image */
.container {
  position: absolute;
  margin: 20px;
  width: auto;
{}
/* Navigation bar */
.topnav {
  overflow: hidden;
  background-color: #333;
{}
/* Navigation bar link */
.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
{}
.topnav a:hover {
  background-color: #ddd;
  color: black;
{}

Try It Yourself