What is the best way to include a header in my fixed navigation bar?

I'm currently facing an issue that I can't seem to resolve, and I suspect it might have something to do with the positioning. Despite my attempts to find a solution online, I still can't determine why this problem persists.

My objective is to place my header (name) in my fixed navigation bar. However, whenever I try to achieve this, the header ends up being positioned behind the grey background color... How can I bring the header to the front of the background? I want the header to remain fixed within the navigation bar. Thank you in advance for any assistance!

html {
    margin: 0;
    background: #ffffff;
}

body {
    margin: 0;
    background: #ffffff;
}

/*---font---*/
@font-face {
    font-family: open-sans;
    src: url('open-sans.regular.ttf');
}

@font-face {
    font-family: Prata-Regular;
    src: url('Prata-Regular.ttf');
}

@font-face {
    font-family: Frontage-Outline;
    src: url('Frontage-Outline.otf');
}

/*---nav bar---*/
h1 {
    display: inline-block;
    float: left;
    position: fixed;
    z-index: -1;
}

ul {
    list-style-type: none;
    background-color: #F5F5F5;
    margin: 0;
    padding: 0;
    overflow: hidden;
    position: fixed;
    top: 0;
    width: 100%;
    display: inline-block;
}

li {
    float: right;
}

li a {
    display: block;
    color: #000;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-family: open-sans;
}

li a:hover:not(.active) {
    color: #555555;
}

.active {
    color: #000;
}
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
    <head>
        <link rel="stylesheet" href="main.css"> 
    </head>
    
    <body>  
        
<!--nav bar-->
    <div class="navbar">
        <div class="bar">
            <h1>cindy le</h1>
            <ul>
                <li><a href="#about">about</a></li>
                <li><a href="#work">work</a></li>
                <li><a href="#contact">contact</a></li>
            </ul>
        </div>
    </div>
        
    </body>



</html>

Answer №1

Set the z-index of h1 to 1

html {
    margin: 0;
    background: #ffffff;
}

body {
    margin: 0;
    background: #ffffff;
}

/*---font---*/
@font-face {
    font-family: open-sans;
    src: url('open-sans.regular.ttf');
}

@font-face {
    font-family: Prata-Regular;
    src: url('Prata-Regular.ttf');
}

@font-face {
    font-family: Frontage-Outline;
    src: url('Frontage-Outline.otf');
}

/*---nav bar---*/
h1 {
    display: inline-block;
    float: left;
    position: fixed;
    z-index: 1; /* Update to 1 */
}

ul {
    list-style-type: none;
    background-color: #F5F5F5;
    margin: 0;
    padding: 0;
    overflow: hidden;
    position: fixed;
    top: 0;
    width: 100%;
    display: inline-block;
}

li {
    float: right;
}

li a {
    display: block;
    color: #000;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-family: open-sans;
}

li a:hover:not(.active) {
    color: #555555;
}

.active {
    color: #000;
}
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
    <head>
        <link rel="stylesheet" href="main.css"> 
    </head>
    
    <body>  
        
<!--nav bar-->
    <div class="navbar">
        <div class="bar">
            <h1>cindy le</h1>
            <ul>
                <li><a href="#about">about</a></li>
                <li><a href="#work">work</a></li>
                <li><a href="#contact">contact</a></li>
            </ul>
        </div>
    </div>
        
    </body>



</html>

Answer №2

To achieve this layout, it is recommended to first fix the navbar div in place and then float a div to the left containing the h1 tag, and another div to the right containing the ul element. It is not advisable to float the h1 and ul tags separately. Here's an example of how you can structure it:

.navbar{
position: fixed;
width: 100%;
top: 100%;
left: 100%;
background-color: #f5f5f5;
}

.navbar-left{
float left;
text-align: left;
}

.navbar-right{
float: right;
text-align: right;
}

.navbar ul{
display: inline-block;
list-style-type: none;
margin: 0px;
padding: 0px;
}

.navbar ul li{
float: left;
}

.navbar ul li a{
display: block;
padding: 14px 16px;
color: #555555;
font-family: open-sans;
}

.navbar ul li a:hover{
color: #000;
}

.clear{
clear: both;
}  

<div class="navbar">
    <div class="navbar-left">
        <h1>header text</h1>
   </div>
   <div class="navbar-left">
        <ul>
            <li><a href="#about">about</a></li>
            <li><a href="#work">work</a></li> 
            <li><a href="#contact">contact</a></li>
        </ul>
    </div>
    <div class="clear"></div>
</div>

Apologies for any formatting errors as I am posting from my mobile device.

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

The chosen options are not appearing. Could this be a problem related to AngularJS?

I am working on setting up a dropdown menu in HTML that includes only the AngularJS tag. This dropdown menu will be used to populate another AngularJS dropdown menu. Below is the code for the first dropdown menu: <select class="form-control" ng-model=" ...

Having difficulty handling text overflow in Angular4 and HTML

I'm facing an issue with displaying a very large text in a table. Despite trying various attributes such as - { text-overflow: clip; } { text-overflow: ellipsis; } { text-overflow: ellipsis-word; } { text-overflow: "---"; } { text-overflow: ellip ...

What is the best way to limit the number of options displayed in the vue-multiselect

I'm looking to truncate multiple values on the vue-multiselect component. I attempted to override various classes without success, as shown in this example: .multiselect__content-wrapper { overflow-x: hidden; text-overflow: ellipsis; } ...

JavaScript: Remove just the specific user input without affecting the rest of the HTML block

I'm facing a dilemma with deleting a specific user input without removing the entire HTML block. I know that the delete button triggers on the HTML ID 'noteDelete', which is the parent of each user input. However, I'm unsure about how t ...

Guide to: Implementing Radio Buttons to Adjust Image Opacity

How can I dynamically change the opacity of an image using JavaScript when a radio button is clicked? I attempted to achieve this using a forEach function, but the current code causes all images to instantly switch to opacity 1; Additionally, after refre ...

Steps for creating a fade effect between two different elements when the mouse is interacting with one of them

I want to create a unique effect with an image in the background and a button above it. When the mouse hovers over the button, I want its opacity to decrease while the image's opacity increases simultaneously. I have experience applying fade effects ...

How to access a PHP include using data retrieved from a MySQL database?

I am currently in the process of developing a website that will be powered by a MySQL database. To simplify the structure of the URLs, I have decided to rewrite them to point to a single file. The file's layout is as follows: <html> <head&g ...

Tips for successfully passing an image using props in Vuejs without experiencing the issue of disappearing content when there is no image present

Is there a way to make a component that can function with or without an image? Currently, when the component doesn't have an image, other contents and styles disappear. How can I resolve this issue? App.Vue: <template> <div id="app&qu ...

In CAKEPHP, emphasize a row in a Product table to indicate a comparison condition, without the need for hovering

I've been experimenting with different approaches to make this comparison work. Despite trying various methods, only the "gumboots" string check seems to do the trick. This string represents a product name in the table and proves that PHP is not neede ...

Tips for centering a horizontal unordered list within a division element

I can't seem to figure out why the text-align center isn't working. I've followed other tutorials and applied the textalign: center to the div, then added display: inline to the ul. Below is a sample of my HTML code from the browser as I am ...

Challenges with selecting elements using jQuery

Hey there! I've got a bit of a coding puzzle. I can successfully select a td and change the image inside it, but I also need to display the id of the selected td. The catch is that my function first has to select the image to change it. Is there a way ...

Reveal/Conceal footer upon vertical scrolling

I am attempting to achieve the following goals: Display the div element when the scrolling position is greater than 20 Apply a fadeOut effect after a certain delay Prevent the fadeOut effect when hovering over the sticky footer This is my implementation ...

Tapping elsewhere on the screen will not result in the select box closing

I'm currently troubleshooting an issue with the site filter feature. When the btn-select is open and I click outside the panel, the select remains open. I am looking to create an event that will detect when the dropdown is closed so that I can close t ...

Align the dimensions of the table to match with the background image in a proportional

Seeking a contemporary method to match a table with a background image, ensuring all content scales proportionally. Mobile visibility is not a concern for this specific project. Using Wordpress with a starter bootstrap theme. Check out the code on jsfidd ...

Having trouble with a static CSS file not loading correctly in the Django admin interface

Currently, I am attempting to replace the base.css file with my own custom CSS file in order to personalize the appearance of Django Admin. {% extends "admin/base.html" %} {% load static %} {% block title %}Custom Django Admin{% endblock %} {% block bran ...

Designing functions with HTML

I have been working on creating a Coffeescript function that incorporates common HTML for an object that is frequently used on my page. To make the content dynamic, I am passing a variable to the function which will be replaced each time it is called. Unfo ...

Delay the rendering of the MUI DataGrid column until after the DataGrid is visible on the screen

Would it be feasible to asynchronously load a column of data in the MUI DataGrid after the table is displayed on the screen? Retrieving this additional data from the database is time-consuming, so I aim to have it appear once the table has fully loaded. T ...

Organizing posts in a sub directory within the _posts folder and ordering them by time using Jekyll

Currently, I am utilizing the mmistakes/minimal-mistakes theme built with jekyll to create my github pages. The goal is to display all of my posts located in the _posts/android directory within the _pages/android.html file. Specifically, I want to include ...

Ways to transfer JavaScript arguments to CSS style

Currently, I am developing a web service using Angular and Spring Boot. In this project, I have implemented cdkDrag functionality to allow users to place images in specific locations within the workspace. My goal is to maintain the placement of these image ...

what is the best way to position a glyphicon next to a form?

Forgive me for asking such a basic question, but I struggle with aligning and positioning elements. Here is my issue: https://i.sstatic.net/Uy6XB.jpg https://i.sstatic.net/TTs6V.jpg All I want is for the glyphicon to be displayed next to the textfield, ...