Placement of navigation bar on top of picture

Creating a gaming website and encountering challenges with text alignment. The goal is to have the navbar text positioned on top of a customized navbar background.

Current appearance navbar example

<!DOCTYPE html>
 <html>
   <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">


  <style>
  body  {
     background-color: black;
     height: 100%;
     margin:0;
     padding:0;
   }    

 .topnav{
     position:absolute;
     text-align: center;
     margin:auto;
     width: 50%;    
  }

 .topnav a {
     line-height: 200px;
     padding: 50px;
     display: ;
     color: #f2f2f2;
     text-align: center;
     text-decoration: none;
     font-size: 17px;
  }

  .navbackground {
     position:absolute;
     top: -50px;
     left:0;
     right:0;
     bottom: 700px;
     margin:auto;
     width: 50%;    
  }

  .banner {
     position:relivent;
     top: 10px;
     left:0;
     right:0;
     bottom:0;
     margin:auto;
     width: 100%;
    }
 </style>
 </head>

 <body>


 <div class="topnav">
   <a class="active" href="#home">Home</a>
   <a href="#news">News</a>
   <a href="#contact">Contact</a>
   <a href="#about">About</a> 
 </div>

  <img src="nav.png" class="navbackground ">
  <img src="wallpaper.JPE" class="banner" >


  </body>
  </html>

Considering using nav.png as a background image in the topnav class but struggling to scale it correctly. Seeking advice or suggestions for improvement from experienced CSS/HTML users!

ps.. Newbie to CSS, HTML here, so appreciate your guidance :)

Answer №1

While the z-index property does work to control how elements stack on top of each other, as a newcomer to HTML, it's important to understand that properly ordering your elements is often the best approach.

Your current setup looks like this:

topnav
  link1
  link2
  link3
topnav background
webpage background

Elements are rendered based on the order they're written in. So, if you want the background to appear first, it should be written first:

webpage background -- will be rendered first
topnav
  link1
  link2
  link3
topnav background

Additionally, for the topnav background to display correctly, it should precede the topnav. If you want the topnav to be relative to the background, you'll need to nest them:

webpage background
topnav background -- background serves as the parent of topnav
  topnav
    link1
    link2
    link3

This way, the rendering order becomes:

  • Render webpage background
  • Render topnav background
  • Render topnav (just a placeholder)
  • Render links

In this scenario, the topnav seems unnecessary. You can apply the background directly to the topnav and eliminate the topnav background element:

webpage background
topnav -- here we use the background CSS property
  link1
  link2
  link3

To achieve the same outcome without altering your HTML using z-index, it's advisable to prioritize clear and semantic HTML structure.

Answer №2

In order to prioritize the navigation, it is necessary to configure the z-index (with a higher value). For further information, please refer to this article on w3schools.

Answer №3

navbar{
   z-index: 1000;
}

To ensure proper stacking, don't forget to include a z-index on the parent element of the navigation bar.

Answer №4

In my opinion, setting the image as a background would be the optimal choice. This allows you to scale the background image using the CSS property background-size, which can be set to either contain or cover depending on how you want it to behave when the aspect ratios of the div and png don't match. Rather than scaling the background directly, I recommend scaling the image in an image editor to fit the size of your navigation div or adjusting the dimensions of the div to match the png.

You could also consider making the background image the background-image of the body or a surrounding div for a cohesive design. Take a look at this example: https://jsfiddle.net/hg5jxn3s/7/

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
  body  {
     background-color: black;
     height: 100%;
     margin:0;
     padding:0;
     background: url(wallpaper.JPE) no-repeat top left;
     background-size: cover;
   }    

 .topnav{
   background:url(https://i.sstatic.net/h54t6.png) no-repeat top left;
   background-size: contain;
     position:relative;
     text-align: center;
     margin:0px auto;
     width: 900px;    
     height: 150px;
     padding:70px 0px;
  }

 .topnav a {
     line-height: 1em;
     padding: 0px 50px;
     color: #f2f2f2;
     text-align: center;
     text-decoration: none;
     font-size: 17px;
  }
 </style>
 </head>

 <body>


 <div class="topnav">
   <a class="active" href="#home">Home</a>
   <a href="#news">News</a>
   <a href="#contact">Contact</a>
   <a href="#about">About</a> 
 </div>


  </body>
  </html>

I've adjusted the width of the topnav to a fixed width instead of flexible (50%) because the navigation items in your setup do not scale. This prevents them from breaking into a second row at certain sizes, which may not work well with the background image. You can preview the changes by updating the CSS for topnav to:

.topnav{
    background:url(https://i.sstatic.net/h54t6.png) no-repeat top left;
    background-size: 100% 100%;
    position:relative;
    text-align: center;
    margin:0px auto;
    width: 50%;    
    height: auto;
    padding:70px 0px;
}

Check out the updated version here: https://jsfiddle.net/hg5jxn3s/10/

Answer №5

Are you interested in achieving a similar effect? ;)

To implement this in your code, assign the .topnav{ ... } class a position of relative and set the background image as demonstrated in this fiddle. Next, create a new HTML div with a class="nav" and apply the CSS style

.topnav .nav{ position: absolute; left: 10%; width: 80%; }
. Also, give the .topnav a a style of
line-height: 60px; width: 24%; display: inline-block;

Those are the key changes I implemented.

body {
  background-color: black;
  height: 100%;
  margin: 0;
  padding: 0;
}

.topnav {
  display: block;
  margin: 20px auto;
  width: 80%;
  height: 60px;
  background-image:url(https://i.sstatic.net/h54t6.png);
  background-repeat: no-repeat;
  background-position: center;
  background-size:100%;
  position: relative;
}

.topnav .nav {
  position: absolute;
  left: 10%;
  width: 80%;
}

.topnav .nav a {
  line-height: 60px;
  display: inline-block;
  color: #f2f2f2;
  text-align: center;
  text-decoration: none;
  font-size: 17px;
  width: 24%;
}

.navbackground {
  position: absolute;
  top: -50px;
  left: 0;
  right: 0;
  bottom: 700px;
  margin: auto;
  width: 50%;
}

.banner {
  position: fixed;
  top: 20px;
  left: 0;
  right: 0;
  margin: auto;
  width: 100%;
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <body>
  <img src="https://cdna.artstation.com/p/assets/images/images/005/718/562/large/josh-bruce-headerfinal.jpg?1493246411" class="banner">
    <div class="topnav">
      <div class="nav">
        <a class="active" href="#home">Home</a>
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <a href="#about">About</a>
      </div>
    </div>
  </body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customizing Bootstrap CSS

In my recent code, I included a bootstrap css reference in this manner: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5T ...

What is the best way to display an alert when the button is clicked repeatedly?

Is it possible to keep displaying the alert every time we click the button, rather than just once after clicking it? I currently have an alert set to trigger when a button is clicked, but it disappears after 3 seconds. How can I make it show up again with ...

Display a smaller image preview in the product photo gallery

Hey there! I'm currently working on a website and looking to showcase my product in a similar way as shown here: I would like my main image to be displayed with all the other variants of the product image underneath, along with the same visual effect ...

Cannot utilize Map within each loop

Below is the less code snippet: @threshold:{ a:50; b:200; }; @themes:{ a:red; b:blue; }; .mymixin(@name,@color,@thrshld){ //do-something } each(@themes,{ .mymixin(@key,@value,@threshold[@key]); }); Upon executing the code, an error message ...

Is the table row changing in size when a filter script class is applied?

When trying to filter table rows by category and adding the class filterTr to the element <tr>, I noticed that the width of the <tr> element changes, causing it to no longer align with the <th> element above. I am unsure why this resizin ...

Automatically populate a jQuery dropdown box with MySQL data

I have a scenario where I need to create 2 dropdown boxes. The first dropdown box will display all the tables from a database, and when a table is selected, the second dropdown box should be populated with fields from that specific table. Dropdown Box 1: ...

Issue with Div element not appearing on Azure AD B2C page customization

Utilizing PopperJS, I attempted to incorporate a popover box that appears when the user focuses on the password field within an Azure AD B2C page customization. Despite noticing the presence of the box element, it fails to display as intended. Any assistan ...

What methods can I use to emphasize three distinct dates?

I'm facing difficulty highlighting three specific dates: "10-11-2020", "22-11-2020", and "07-11-2020" using React.js and Datepicker. Unfortunately, my attempts have not been successful so far. Here is the code snippet I am working with: import React, ...

Having trouble retrieving the default selected value using the index in Angular Material's mat-button-toggle functionality

I'm encountering an issue with setting the default value for a toggle button group. The code is simple and the toggle buttons are correctly fetching values from the index, but I can't seem to get one of them to be default selected. I tried settin ...

What could be causing my UI Bootstrap datepicker-popup to suddenly stop functioning?

After updating to UI Bootstrap 0.11.0, I encountered an issue where my datepickers were no longer appearing correctly. To demonstrate this problem, I have created a plunker which can be viewed here. Essentially, the code snippet causing the problem is as f ...

Am I on the right track in my understanding of how document and viewport relate to mouse position in JavaScript?

After reviewing responses from a previous inquiry, it is evident that both pertain to the x and y coordinates of mouse positions. In relation to the document and In relation to the viewport. I have delved into an article on QuirksMode, yet I feel ther ...

There seems to be a JSON syntax error on the website for tracking Covid-

Hi everyone, I'm currently working on a Covid-19 tracker website and facing an issue that says: 'SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data' ; Being new to web development, I am unsure about what&apo ...

When using angular's ngHide directive on a button, it will still occupy space in the

At the bottom of this HTML file, there are 3 buttons displayed. The first image illustrates the layout of the 3 buttons. The second image demonstrates what happens when the middle button in the footer is commented out. The third image shows the situat ...

Troubleshooting padding problem in mobile gallery view with Bootstrap

Having a problem with the gallery layout in Bootstrap. It looks great on desktop view, but on mobile, there's no space between images on the same row. On mobile, the images stack vertically without any spacing between them within a row. However, ther ...

How to prevent jQuery from continually recalculating width on window resize?

Here is the code I've been working on: http://jsfiddle.net/7cXZj/ var callback = function () { $('.progress-bar').width($('.progress-bar').parent().width() - 190); $(".mainpart-background").animate({ width: "80%" }, 80 ...

Understanding the relationship between CSS height and line-height can help developers create more

Is there a way to use CSS to set the height of a box based on its own line-height or its parent's line-height? This feature would make it much simpler to create text areas that are a specific multiple of lines, for example, displaying exactly three l ...

Clicking on a link initiates the dropdown menu for selecting an option

This project is specifically designed for mobile use, so there's no need to worry about how it will appear on desktop screens. In this project, I have an "a href" with an icon next to it that simulates a button. When a user clicks on it, a dropdown me ...

What would you suggest as a more effective method for preventing content overlap during multi uploads?

Recently, I was working on a Wordpress site and used the Ninja Forms plugin to create a form. One of the challenges I encountered was styling the file upload button. After some research, I came across a tutorial that provided a great solution that worked a ...

Tips for invoking a JavaScript class method from an HTML document

I've created a Typescript class that dynamically inserts an HTML element to a page after it's loaded. Here is the code snippet: const calcElement: string = ` <div class="container"> <span class="cc-title">Field</span> ...

A division of text is colliding with a division that holds a list with no particular

Hello everyone, I am new to HTML/CSS and I am facing an issue that I believe can be easily resolved. Currently, my code has four sections, with the last three displaying as expected in a consecutive manner. However, the second section is overlapping on t ...