Centering the element on the navbar and ensuring its fixed position

Attempting to create a navbar with elements aligned to the left, right, and center. However, struggling with centering the elements on the screen. Can anyone assist in identifying my mistake?

Additionally, seeking a way to keep the navbar fixed so it does not disappear when scrolling. Any suggestions would be appreciated!

Thank you in advance!

View My Code DEMO

HTML

<nav class="navbar">
        <ul class="nav">
          <li class="nav-item">
            <a class="btn"><img style="width:45px; height:45px" src="https://cdn3.iconfinder.com/data/icons/2018-social-media-logotypes/1000/2018_social_media_popular_app_logo_reddit-128.png"></a>
          </li>   
        </ul>
        <ul class="nav justify-content-center divBtn">
          <li class="nav-item">
            <a class="btn"><img style="width:45px; height:45px" src="https://cdn3.iconfinder.com/data/icons/2018-social-media-logotypes/1000/2018_social_media_popular_app_logo_reddit-128.png"></a>
          </li>   
        </ul>
        <ul class="nav justify-content-end">
          <li class="nav-item">
             <button>My buttons</button>
              <button>My buttons</button>
          </li>
             <li class="nav-item">
              <button>My buttons</button>
          </li>
             <li class="nav-item">
            <a class="btn"><img style="width:45px; height:45px" src="https://cdn3.iconfinder.com/data/icons/2018-social-media-logotypes/1000/2018_social_media_popular_app_logo_reddit-128.png"></a>
          </li>
             <li class="nav-item">
            <a class="btn"> <img style="width:45px; height:45px" src="https://cdn3.iconfinder.com/data/icons/2018-social-media-logotypes/1000/2018_social_media_popular_app_logo_reddit-128.png">
            </a>
          </li>
             <li class="nav-item">
            <a class="btn"><img style="width:45px; height:45px" src="https://cdn3.iconfinder.com/data/icons/2018-social-media-logotypes/1000/2018_social_media_popular_app_logo_reddit-128.png">
            </a>
          </li>
        </ul>
      </nav>

Answer №1

It's possible that I may not fully grasp your question, but if I do, then I believe I have the solution for you. You can achieve the desired effect by applying specific margins to the left and right "group" of the navbar. First, make sure to set the width of a div, each with a width of 20% (totaling 60%). The left div should have a margin-right of 20%, while the right div should have a margin-left of 20%. To center everything perfectly, set the text-align of the div to center. Feel free to test my code by clicking on the "run code snippet" button below:

.navBar {
    display: flex;
    position: fixed;
    top: 0;
    left: 0;
    background: #ccc;
    width: 100%;
    padding: 5px 0;
}

.navBar div {
    width: 20%;
    text-align: center;
}

.navBar div a  {
    margin: 0 4px;
    padding: 0 4px;;
    background: rgb(0, 0, 0);
    color: #fff;
    text-decoration: none;
}



.left {
    margin-right: 20%;
}

.right {
    margin-left: 20%;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="main.css">
</head>

<body>
    <div class="navBar">
        <div class="left">
            <a class="btn" href="#">1</a>
            <a class="btn" href="#">2</a>
            <a class="btn" href="#">3</a>
        </div>

        <div class="center">
            <a href="#" class="btn">4</a>
            <a href="#" class="btn">5</a>
            <a href="#" class="btn">6</a>
        </div>

        <div class="right">
            <a href="#" class="btn">7</a>
            <a href="#" class="btn">8</a>
            <a href="#" class="btn">9</a>
        </div>
    </div>
</body>
</html>

To keep the navbar fixed at the top, I utilized "position: fixed," although "position: sticky" is also a valid alternative.

Answer №2

.topfixed{
    position: sticky !important;
    top: 0;
    z-index: 10;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar nabBarContainer topfixed">
        <ul class="nav divBtn">
          <li class="nav-item">
            <a class="btn"><img style="width:45px; height:45px" src="https://cdn3.iconfinder.com/data/icons/2018-social-media-logotypes/1000/2018_social_media_popular_app_logo_reddit-128.png"></a>
          </li>   
        </ul>
        <ul class="nav justify-content-center divBtn">
          <li class="nav-item">
            <a class="btn"><img style="width:45px; height:45px" src="https://cdn3.iconfinder.com/data/icons/2018-social-media-logotypes/1000/2018_social_media_popular_app_logo_reddit-128.png"></a>
          </li>   
        </ul>
        <ul class="nav justify-content-end divBtn">
          
             <li class="nav-item">
            <a class="btn"><img style="width:45px; height:45px" src="https://cdn3.iconfinder.com/data/icons/2018-social-media-logotypes/1000/2018_social_media_popular_app_logo_reddit-128.png">
            </a>
          </li>
        </ul>
      </nav>
      <div style="height:1000px;"></div>

  1. To solve the issue with the header, you can simply apply this CSS class:

HTML

<nav class="navbar nabBarContainer topfixed">

CSS

.topfixed{
        position: sticky;
    top: 0;
    z-index: 10;
}

Uncertain about which buttons you want to center... there are a total of 8 buttons. How would you like them aligned?

Answer №3

//To keep the navigation bar fixed:

position: fixed;

//To center an element:

margin: 0 auto;

//To position an element on the right side:

position: absolute;
right: 0;
top: 0;

//To position an element on the left side:

position: absolute; left: 0; top: 0;

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

Tips for positioning an MUI element inside a container to extend to the edge of the browser window

Currently, I'm working on a project with a Material-UI (MUI) container that contains a Stack element. Within this Stack, there are two Box elements displayed in a row, each set to take up 50% of the width. In my design, I want the second box to break ...

Manipulating a link within a span element with Puppeteer

I'm currently working on a project where I need to scrape a manga website and save all the pages. The code I have is able to navigate through the page and save the images successfully. However, I'm facing an issue when trying to click on a link ...

"Finding the Perfect Alignment: How to Center Legends in Firefox

The issue at hand An issue has been identified where the code functions as intended in Chrome, Opera, and IE but fails to work in Firefox: fieldset>legend { display: table; float: none; margin: 0 auto; } <fieldset> <legend>Legend ...

Creating an event within a class that is generated dynamically when hovering

I'm trying to create a hover effect where an image moves around the page in a pattern (top left corner -> top right corner -> bottom right corner -> bottom left corner -> then back up to the top left corner). To achieve this, I am adding ...

Which HTML elements are compatible with the ng-disabled directive?

After attempting to use ng-disabled on an <a> element, I realized it does not work. It seems to only function properly on the <button> element. Can someone clarify which elements are compatible with ng-disabled? ...

Toggle the div if the if statement is true; otherwise, hide the broken

click: function(event, data) { $('#clicked-state') .text('You clicked: '+data.name); if (data.name == "VA") { $('#va').toggle(); } else { $('#va').style.d ...

Where can I find the specific code needed to create this button style?

There is a button here for adding an event, but unfortunately, the style code is not displayed. Is there a way to obtain the full cascading style sheet for this button? ( ) ...

navigating up and down html elements using css selectors

Within my code, I have this odd repetitive HTML structure (with no classes) and I'm looking to extract all links along with the accompanying text below each link. While it's simple enough to grab all the links using a basic CSS query selector li ...

creating an animated loop within an Angular template

How can we display a dynamic loop in Angular template using an array of object data? [ { "name": "name", "text": "text", "replies": [{ "name": "Reply1", "t ...

What is the solution for positioning an input or select element at the end of a tr element?

Within a table, I have a form where each <td> tag contains either a <select> or an <input>. However, due to the varying lengths of the labels within the <td> tags, the <input> or <select> fields are not aligned in the sa ...

Mastering the fundamentals of integrating/augmenting a fresh template in Umbraco

Let me be completely honest. Umbraco is unlike any other CMS I have worked with before. I am currently struggling to grasp the proper method of creating a Template and integrating it into Umbraco. How exactly can I add a template? I am making changes to t ...

displaying information using specific bootstrap pills

I have a scenario with 3 bootstrap pills where I want to display data in pill 2 and pill 3, but not in pill 1. How can I achieve this without duplicating the data entry? <ul class="nav nav-tabs"> <li><a data-toggle="tab" href="#pill ...

Use percentages for the width in CSS and pixels for the height, ensuring that both dimensions are equal

I'm attempting to create a square that adjusts its size according to the screen size. Currently, I have the width set to 25%. Is there a way to ensure that the height remains the same length in pixels as the width? Appreciate any assistance on this ...

Detecting the existence of a scrollbar within the document object model using JavaScript - a guide

Could someone provide a JavaScript syntax for detecting the presence of a scrollbar in the DOM by measuring the body height and window height? ...

Javascript function that sets the opacity to 0

Hello everyone, I'm new to SO and seeking some guidance on improving my post! I have a function that sets the opacity of an element to 0 in order to clear it. While this works fine, it becomes burdensome when dealing with multiple elements that requi ...

Exploring the inner workings of Viewstate

To improve the performance of my application, I made the decision to disable the Viewstate for my controls. As a result, I noticed the following: <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzU3OTU5NDI1ZGTBCxumGHfzsISSVDAeBM ...

How is it possible that I am able to view the font on this particular website?

Recently, I stumbled upon a fascinating website that allows users to experiment with various CSS3 features. As I examined the website's CSS code to enhance my own skills, I noticed something intriguing about the elegant "CSS3Gen" logo - it utilized a ...

Modify the -webkit-text-stroke- hue using JavaScript

I've been working on a website that features text with "text stroke", and I am attempting to dynamically change the color of this text stroke using JavaScript. My goal is to have four different color options available. However, as it is based on webk ...

Stop browser from downloading attached file

<a href="/scripts/script.pde" target="_blank"></a> My goal is to have the browser show this file as text in a new window. Once clicked, it will be downloaded onto the user's computer. ...

How can I resize several images to fit seamlessly within a div container?

Looking for a smart method to resize multiple images to fit neatly inside a fixed-size div? I've considered using JavaScript to calculate the div's width, dividing it by the number of images, and resizing them equally while maintaining their asp ...