What is the best way to align the navigation bar in the center

Hi there! Currently, I am in the process of creating a navigation bar for a website but I am facing an issue where the logo and navigation elements are not covering the entire width of the browser. My goal is to have the logo aligned to the far left and the list elements aligned to the far right.

Here is the HTML code snippet:

        <link rel="stylesheet" type="text/css" href="resources/css/styles.css">
        <link rel="stylesheet" type="text/css" href="vendors/css/normalize.css">
         <link rel="stylesheet" type="text/css" href="vendors/css/grid.css">
         <link rel="stylesheet" type="text/css" href="vendors/css/ionicons-5.4.0.designerpack%20copy">
        <link href='http://fonts.googleapis.com/css?family=Lato:100,300,400,300italic' rel='stylesheet' type='text/css'>
        <title>moBurgerz</title>

    </head>

    <body>
        
        <header>
            <nav>
            <div class="row"> 
            <img src="Resources/img/moBurgerzLogo.png" class="logo">
                
                    <ul class="main-nav">
                        <li><a href="#">About</a></li>
                        <li><a href="#">Locations</a></li>
                        <li><a href="#">Menu</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
            </div>
            </nav>
            
            <div class="hero-text">
                <h1>BEST BURGERS IN <br> DA City.</h1>
                <a class="btn btn-full"href="#">I'm hungry</a>
                <a class="btn btn-empty"href="#">Show me more</a>
            </div>
        </header>
        
        <section class="story">
            <div class="row">
                <h2>Our Story</h2>
                <p>moBurgerz was established in the summer of 2017 in the DMV. Since than we have grown tremendously with the help of our customers. Every burger is made with love. Come join us and see what the hype is about.</p>
            </div>
        
        
        </section>
    
    
    </body>

</html>

css:
/*---------------------*/
/*BASIC SETUP*/
/*---------------------*/
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    
}

html,body {
    font-family: 'Lato','sans-serif','arial';
    color: #fff;
    font-size: 15px;
    font-weight: 300;
    text-transform: uppercase;
    text-rendering: optimizeLegibility;
    
}
section {
    padding: 80px 0;
}

.row {
    max-width: 1140px;
    margin: 0 auto;
}

h2 {
    text-align: center;
    margin: 0;
    padding-top:  20px;
    font-size: 25px;
}


/*------------------------*/
/*Navigation*/
/*------------------------*/
header {
    background-image: linear-gradient(rgba(0,0,0,0.4),rgba(0,0,0,0.4)),url("img/truck.jpg");
    background-size: cover;
    background-position: center;
    height: 100vh;
    
}
.logo {
    height: 100px;
    float: left;
    width: auto;
    margin-top: 10px;
}
.hero-text {
    position: absolute;
    width: 1140px;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

.main-nav {
    list-style: none;
    float: right;
    margin-top: 55px;
}
.main-nav li {
    color: #fff;
    display: inline-block;
    margin-left: 40px; 
}

.main-nav li a:hover,
.main-nav li a:link, {
    color: #fff;
    text-decoration: none;
    text-transform: uppercase;
    
}
.main-nav li a:visited,
.main-nav li a:active, {
    color: #fff;
    text-decoration: none;
    text-transform: uppercase;
}

.story {
    color: #5d5a5a;
}

Answer №1

Thank you for updating the code. To center the navbar, you need to turn your .row div into a flex container and align all items in the center after removing any arbitrary margins on the image and navbar elements. This will ensure that the contents are displayed in a single row with proper alignment. You can always add back the margins if needed, and the current layout can help you determine the spacing easily.

If this solution addresses your issue and answers your query, great! If not, feel free to reach out. Happy to assist. Cheers!

css:
/*---------------------*/
/*BASIC SETUP*/
/*---------------------*/
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;

}

html,body {
    font-family: 'Lato','sans-serif','arial';
    color: #fff;
    font-size: 15px;
    font-weight: 300;
    text-transform: uppercase;
    text-rendering: optimizeLegibility;

}
section {
    padding: 80px 0;
}

.row {
    max-width: 1140px;
    margin: 0 auto;
}

h2 {
    text-align: center;
    margin: 0;
    padding-top:  20px;
    font-size: 25px;
}


/*------------------------*/
/*Navigation*/
/*------------------------*/
header {
    background-image: linear-gradient(rgba(0,0,0,0.4),rgba(0,0,0,0.4)),url("img/truck.jpg");
    background-size: cover;
    background-position: center;
    height: 100vh;

}
.logo {
    height: 100px;
    float: left;
    width: auto;
}
.hero-text {
    position: absolute;
    width: 1140px;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

.main-nav {
    list-style: none;
    float: right;
}
.main-nav li {
    color: #fff;
    display: inline-block;
    margin-left: 40px; 
}

.main-nav li a:hover,
.main-nav li a:link, {
    color: #fff;
    text-decoration: none;
    text-transform: uppercase;

}
.main-nav li a:visited,
.main-nav li a:active, {
    color: #fff;
    text-decoration: none;
    text-transform: uppercase;
}

.story {
    color: #5d5a5a;
}

.row {
display: flex;
box-sizing: content-box;
align-items: center;
justify-content: space-between;
background-color: green;
padding: 10px;
}
<head>
        <link rel="stylesheet" type="text/css" href="resources/css/styles.css">
        <link rel="stylesheet" type="text/css" href="vendors/css/normalize.css">
         <link rel="stylesheet" type="text/css" href="vendors/css/grid.css">
         <link rel="stylesheet" type="text/css" href="vendors/css/ionicons-5.4.0.designerpack%20copy">
        <link href='http://fonts.googleapis.com/css?family=Lato:100,300,400,300italic' rel='stylesheet' type='text/css'>
        <title>moBurgerz</title>
    
    </head>

    <body>
        
        <header>
            <nav>
            <div class="row"> 
              <img src="https://picsum.photos/200" class="logo">
              <ul class="main-nav">
                  <li><a href="#">About</a></li>
                  <li><a href="#">Locations</a></li>
                  <li><a href="#">Menu</a></li>
                  <li><a href="#">Contact</a></li>
              </ul>
            </div>
            </nav>
            
            <div class="hero-text">
                <h1>BEST BURGERS IN <br> DA City.</h1>
                <a class="btn btn-full"href="#">I'm hungry</a>
                <a class="btn btn-empty"href="#">Show me more</a>
            </div>
        </header>
        
        <section class="story">
            <div class="row">
                <h2>Our Story</h2>
                <p>moBurgerz was established in the summer of 2017 in the DMV. Since than we have grown tremendously with the help of our customers. Every burger is made with love. Come join us and see what the hype is about.</p>
            </div>
        
        
        </section>
    
    
    </body>

Answer №2

Have you considered utilizing flexbox for this?

<div class="nav">
  <div class="logoContainer"></div>
  <div class="itemContainer">
    <div>item1</div>
    <div>item2</div>
    <div>item3</div>
  </div>
</div>

.nav {
    width: "100%";
    height: 50;
    padding-left: 5%;
    padding-left: 5%;

    display: "flex";
    flex-direction: "row";
    justify-content: space-between;
    align-items: center;
    align-content: center;
}

.logoContainer {
    width: 20%;
    height: 100%;
}

.itemContainer {
    width: 80%;
    height: 100%;

    display: "flex";
    flex-direction: "row";
    justify-content: space-between;
    align-items: center;
    align-content: center;
}

Answer №3

To organize your navigation items and logo effectively, place the nav items in one div and the logo in another. Then, nest both of these divs within a parent div and apply the following CSS properties:

  • display: flex;
  • justify-content: space-between;

Make sure to set the styling for the div containing the nav items first. It's recommended to avoid using float in your code for better results.✌

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 ng-binding directive seems to be malfunctioning while ng-model is functioning properly

Currently, I am in the process of learning how to utilize Angular (1.3.10). My objective is to create two input fields that will specify the suit and value for a hand of playing cards. In my attempts to achieve this, I have encountered an issue. When I man ...

Render JSON data retrieved from an AJAX request as a formatted HTML display or table

Code for displaying JSON data $('body').on('click','#check_history',function () { var settings = { "async": true, "crossDomain": true, "url": "https://url.com", ...

The angular-ui modal is always visible from the start

I'm currently referencing this Angular recipes page for guidance on implementing a modal dialog in my user interface. The suggested markup includes adding a button to one of my views. ... html for my view is here ... <button class="btn" ng-click=" ...

Angular fails to function properly post rendering the body using ajax

I recently created a HTML page using AJAX, and here is the code snippet: <div class="contents" id="subContents" ng-app=""> @RenderBody() @RenderSection("scripts", required: false) </div> <script src="http://ajax.googleapis.com/ajax/ ...

Identifying various device platforms through CSS styling

After extensive research and testing, I have explored a variety of resources and solutions for implementing device-specific CSS on my webpage. Here are some of the references I've studied: Detect iPhone/iPad purely by css Detect Xoom browser (Andro ...

Obtaining data from HTML using VBA is not possible with .getElementsByTag() or .getElementByID()

Currently, my project involves extracting data from HTML source code. My focus is on analyzing crash cases from the following website: To achieve this, I aim to collect all relevant data from the HTML by searching for .innertext of specific tags/IDs. Thi ...

Is there a way to show a different color background when hovering over text in a navigation bar using HTML and CSS?

My navigation bar is almost perfect, but there's one small issue: the opacity change when hovering doesn't reach all the way down to the bottom. It's a minor detail, but it bothers me. Here are some screenshots for reference: This: https://i ...

Are there any quick tricks for condensing multiple hover CSS properties into a single line of code?

Imagine wanting to apply the hover CSS property to multiple classes, you would typically have to write it like this: a:hover, p:hover, div:hover{ // CSS properties } It can become repetitive having to add ":hover" to each class. Is there a way to stream ...

Utilizing the NG-CLASS directive within a material table to target a specific 'row' element for styling in CSS

I have a table with various columns, one of which is a status field. I am looking to display colored badges in that column based on the status of each record, which can be "Completed", "Deleted", or "Canceled". I have attempted to use ng-class to dynamical ...

Is there a way to apply toggling and styles to only the card that was clicked in Vue.js?

DisplayBooks.vue consists of a single page responsible for showcasing books fetched from a backend API. Each card on the UI features two buttons - ADD TO BAG and ADDED TO BAG. When a user clicks on the ADD TO BAG button of a specific card, it should toggle ...

JavaScript is unresponsive and fails to display

I attempted to incorporate my initial Javascript snippet into a browser to observe its functionality. However, upon adding these lines directly into the body of my HTML code (even though I am aware that there are more efficient methods), no visible changes ...

Launching shuttles using HTML and JavaScript

I've been working on an HTML code with JavaScript for a platform validation project. Over the last couple of weeks, I've been diligently coding and testing to ensure everything runs smoothly. However, my code suddenly stopped responding, leaving ...

Looking to achieve a scroll effect with mix-blend-mode using JavaScript?

I am seeking a method to adjust the background color of a specific element based on the background itself. While CSS offers the mix-blend-mode property, I am looking to specify the color manually. Ideally, I would like to achieve the same effect using an ...

Ensure that columns are neatly arranged horizontally when they do not all fit in one row

I currently have a row with 6 columns, but when they can't fit next to each other on the screen, 2 of them get pushed below, resulting in a messy layout. I could use a media query to solve this issue, however, I am curious if there is a better way to ...

What is the reason for the Client Height value not affecting other elements?

I'm trying to set the spacing of my sidebar from the top equal to the height of the header. However, when I use clientHeight in JavaScript to get the height and then try to apply it to other elements using marginTop or top values (with position includ ...

Issue with Website Failing to Redirect to Correct Page After Logging In

Hey there, I've been working on a website using Python and Flask, and it's been going well so far. But now I've encountered a problem when trying to implement a login feature for the site. I have set up logic to authenticate users and redi ...

CSS Filters and Utilizing Negative Margins

I've been experimenting with placing a div behind another div in the layout of my website. Instead of using "position:absolute," I decided to try negative margins. Everything seemed to be going well until I added some filters. The div in question has ...

Implementing CSS injection on a Next.js page during server-side rendering - tips and tricks!

I recently started working with next js. In my _app.tsx page, I have the following code: import '../styles/antd.css' import '../styles/globals.css' import type { AppProps } from 'next/app' function MyApp({ Component, pagePr ...

Jumping through pages using a For Loop in Angular 5

Angular 5 - Chrome. Seeking advice on preventing page jumping. I have a table connected to an ngFor loop. When I am at the top of my page and trigger an action that adds an element to the array, if that table with the ngFor loop is off-screen at the time ...

How can I implement horizontal scrolling on a material-ui table containing numerous columns?

As I work with the React Material-UI framework, I am using this table example as a guide. However, I am facing an issue where my table becomes overcrowded when I have numerous columns, causing the content to be cut off. I recently came across the material ...