Tips for ensuring the list stays perfectly centered within the navigation bar when placed among three images

I have my navigation bar set up with the logo on the far left and two flags for English and Italian on the far right. The issue I'm facing is that the UL element, being a block element, pushes the flags onto the next line. However, I want the UL to be centered between the right border of the navbar and the left-aligned logo.

When I tried changing the display property of the UL to inline-block, the flags did come up but then the UL was no longer centered. Does anyone have suggestions on how I can achieve this layout? Perhaps building the navbar without using a list format?

Any tips on making the UL center itself based on the positioning of these three images would be greatly appreciated!

Thank you in advance for your assistance.

Check out the code snippet here

   nav {
     margin-left: auto;
     margin-right: auto;
     margin-top: 0px;
     z-index: 2;
     width: 1200px;
     height: 80px;
     line-height: 40px;
     background: #222;
     color: white;
     padding: 0 1%;
   }
   nav ul {
     margin-left: 1%;
     list-style-type: none;
     text-align: center;
     padding: 5px 0px 5px 0px;
     display: inline;
   }
   nav ul li {
     display: inline-block;
     padding: 5px 10px 5px 10px;
   }
   nav ul li a:link,
   nav ul li a:visited {
     color: #FFF;
     border-bottom: none;
     font-weight: bold;
     display: inline-block;
     width: 110px;
     text-align: center;
     text-decoration: none;
     text-transform: uppercase;
   }
   nav ul li a:hover,
   nav ul li a:active {
     opacity: .6;
   }
   nav ul li.selected {
     background-color: rgba(184, 140, 199, .8);
   }
   #logo {
     margin-top: 5px;
     float: left;
     display: inline-block;
   }
   #en {
     margin-top: 0px;
     display: inline-block;
     float: right;
   }
   #it {
     float: right;
     margin-top: 0px;
     display: inline-block;
   }
<nav>
  <a href="../index.html">
    <img src="../images/sblogo.png" alt="Logo" img id="logo">
  </a>
  <ul>
    <li class="selected"><a href="home.html">Home</a>
    </li>
    <li><a href="news.php">News</a>
    </li>
    <li><a href="artist.html">The Artist</a>
    </li>
    <li><a href="gallery.html">Gallery</a>
    </li>
    <li><a href="contact.html">Contact</a>
    </li>
  </ul>
  <a href="../it/home.html">
    <img src="../images/italian.jpg" alt="Cambio Lingua in Italiano" img id="it">
  </a>
  <a href="../en/home.html">
    <img src="../images/eng.jpg" alt="Change Language to English" img id="en">
  </a>
</nav>

Answer №1

If I have understood correctly, you are looking to place the logo at the beginning of the navigation bar and the language options at the end. The main navigation (ul element) should be centered between the logo and languages with equal spacing on both sides.

Below is my solution to achieve this layout:

.logo {
        align-self: flex-start;
      }
      .nav {
        align-self: center;
      }
      .languages {
        align-self: flex-end;
      }
      nav {
        display: flex;
        justify-content: space-between;
        margin-left: auto;
        margin-right: auto;
        margin-top: 0px;
        z-index: 2;
        width: 1200px;
        height: 80px;
        line-height: 40px;
        background: #222;
        color: white;
        padding: 0 1%;
      }
      nav ul {
        list-style-type: none;
        text-align: center;
        padding: 5px 0px 5px 0px;
        display: inline;
      }
      nav ul li {
        display: inline-block;
        padding: 5px 10px 5px 10px;
      }
      nav ul li a:link,
      nav ul li a:visited {
        color: #FFF;
        border-bottom: none;
        font-weight: bold;
        display: inline-block;
        width: 110px;
        text-align: center;
        text-decoration: none;
        text-transform: uppercase;
      }
      nav ul li a:hover,
      nav ul li a:active {
        opacity: 0.6;
      }
      nav ul li.selected {
        background-color: rgba(184, 140, 199, 0.8);
      }
      #logo {
        margin-top: 5px;
        float: left;
        display: inline-block;
      }
      #en {
        margin-top: 0px;
        display: inline-block;
        float: right;
      }
      #it {
        float: right;
        margin-top: 0px;
        display: inline-block;
      }
<nav>
        <div class="logo">
          <a href="../index.html">
            <img src="../images/sblogo.png" alt= "Logo" img id="logo">
          </a>
        </div>
        <div class="nav">
          <ul>
            <li class="selected"><a href="home.html">Home</a></li>
            <li><a href="news.php">News</a></li>
            <li><a href="artist.html">The Artist</a></li>
            <li><a href="gallery.html">Gallery</a></li>
            <li><a href="contact.html">Contact</a></li>
          </ul>
        </div>
        <div class="languages">
          <a href="../it/home.html">
            <img src="../images/italian.jpg" alt= "Switch Language to Italian" img id="it">
          </a>
          <a href="../en/home.html">
            <img src="../images/eng.jpg" alt= "Switch Language to English" img id="en">
          </a>
        </div>
      </nav>

To improve the layout, ensure you remove margin-left: 1%; from nav ul in your CSS file.

You can view a working demo here: https://jsfiddle.net/sebastianbrosch/p9hgmetf/1/

Answer №2

You can utilize the power of flexbox to achieve your desired layout...

nav {
     margin-left: auto;
     margin-right: auto;
     margin-top: 0px;
     z-index: 2;
     width: 1200px;
     height: 80px;
     line-height: 40px;
     background: #222;
     color: white;
     padding: 0 1%;
     display: flex; /* Use DISPLAY: FLEX; */
    justify-content: center; /* JUSTIFY CONTENT, MAKE HORIZONTALLY CENTERED */
  align-items: center; /* ALIGN ITEMS TO VERTICALLY CENTER*/
   }
   nav ul {
     margin-left: 1%;
     list-style-type: none;
     text-align: center;
     padding: 5px 0px 5px 0px;
   }
   nav ul li {
     flex-grow: 1; /* STRETCH THE ITEMS TO FIT THE SPACE */
     display: inline-block;
     padding: 5px 0;
   }
   nav ul li a:link,
   nav ul li a:visited {
     color: #FFF;
     border-bottom: none;
     font-weight: bold;
     display: inline-block;
     width: 110px;
     text-align: center;
     text-decoration: none;
     text-transform: uppercase;
   }
   nav ul li a:hover,
   nav ul li a:active {
     opacity: .6;
   }
   nav ul li.selected {
     background-color: rgba(184, 140, 199, .8);
   }
   #logo {
     margin-top: 5px;
     float: left;
     display: inline-block;
   }
   #en {
     margin-top: 0px;
     display: inline-block;
     float: right;
   }
   #it {
     float: right;
     margin-top: 0px;
     display: inline-block;
   }
<nav>
  <a href="../index.html">
    <img src="../images/sblogo.png" alt="Logo" img id="logo">
  </a>
  <ul>
    <li class="selected"><a href="home.html">Home</a>
    </li>
    <li><a href="news.php">News</a>
    </li>
    <li><a href="artist.html">The Artist</a>
    </li>
    <li><a href="gallery.html">Gallery</a>
    </li>
    <li><a href="contact.html">Contact</a>
    </li>
  </ul>
  <a href="../it/home.html">
    <img src="../images/italian.jpg" alt="Cambio Lingua in Italiano" img id="it">
  </a>
  <a href="../en/home.html">
    <img src="../images/eng.jpg" alt="Change Language to English" img id="en">
  </a>
</nav>

Answer №3

To achieve the desired layout, Flexbox is the way to go. Below are the essential CSS rules you'll need, with some adjustments for better visibility of the outcome. The justify-content property controls how the content inside the navigation bar is aligned. For a comprehensive guide on using CSS flexbox, check out this helpful resource here.

nav {
  display: flex;
  justify-content: space-between;
}

img {
  width: 50px;
}
<nav>
<a href="../index.html">
<img src="../images/sblogo.png" alt= "Logo" img id="logo">      </a>
<ul>
  <li class="selected"><a href="home.html">Home</a></li>
  <li><a href="news.php">News</a></li>
  <li><a href="artist.html">The Artist</a></li>
  <li><a href="gallery.html">Gallery</a></li>
  <li><a href="contact.html">Contact</a></li>
  </ul>
<a href="../it/home.html">
   <img src="../images/italian.jpg" alt= "Cambio Lingua in Italiano" img   id="it"></a>
<a href="../en/home.html">
    <img src="../images/eng.jpg" alt= "Change Language to English" img id="en"></a>
   </nav>

Alternatively, you can use the following approach. This snippet ensures that the ul element expands as much as possible, pushing other items to the sides.

nav {
  display: flex;
}

ul {
  flex: 1;
}

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

HTML5 coding can be enhanced by applying unique CSS rules for alignment purposes

html { font-family: "Open Sans", sans-serif; font-weight: 100; background-color: #fbfbfb; } body { font-family: "Open Sans", sans-serif; font-weight: 100; } body h1 { font-weight: 100; } body h3 { font-family: "Open Sans", sans-serif; fo ...

Certain components display with greater height on Internet Explorer

When viewing my website in Internet Explorer, I noticed that some elements appear taller compared to other browsers. After investigating further, I discovered that all these elements contained SVG images. It seems like they have a fixed height even though ...

Tips for aligning the navigation bar in the center

I recently came across a simple tutorial on w3schools (article) for creating a responsive menu. However, I've been struggling to center it horizontally for the past few days. Here is the HTML code: <ul class="topnav"> <li><a href=" ...

"Enhance Your Design With CSS3 Styling for Labels and

HTML <div class="select"> <div> <label for="kitty" class="kitty-label kitty-label-1 l-center"> </label> <input type="checkbox" name="cats" value="1"> <label>Kitty One</label> ...

Expanding Table Rows in DataTables

Currently, my DataTable has hidden child rows to display additional information. Each parent row represents a gift card, while the hidden child rows show a log of that gift card. A gift card can have multiple logs. For example: Gift Card 1: 0 rows; the ...

Is the CSS3 bar chart flipped?

I'm currently developing a responsive bar chart, and everything seems to be going smoothly except for one issue - when viewing it in full screen, the bars appear to be flipped upside down. It's quite puzzling as all other elements such as text an ...

Implementing a specialized CSS handler for Node.JS

Currently, I have set up a Node.JS server for a new project at my workplace. As part of the project, I have created an optimizer function that removes unnecessary elements such as tabs, newlines, and comments from HTML, JavaScript, and CSS files. Strangel ...

Completed Animation with Callback in AngularJS CSS

Currently working with AngularJS and hoping to receive a notification upon completion of an animation. I am aware that this can be achieved with javascript animations such as myApp.animation(...), but I'm intrigued if there is an alternative method wi ...

The FormData function is unable to retrieve the input fields of a form

My Unique Code <!DOCTYPE html> <html> <body> <div id="custom-form-sample"> <form enctype="multipart/form-data"> <input type="text" name="custom-text" /> <input type="radio" name="custom-radio" ...

Create a JavaScript and jQuery script that will conceal specific div elements when a user clicks on them

Within my navigation div, there exists another div called login. Through the use of JavaScript, I have implemented a functionality that reveals additional divs such as login_name_field, login_pw_field, register_btn, do_login_btn, and hide_login upon clicki ...

Ways to modify the screen when a click event occurs

To make the display block when clicking this icon: <div class="index-navi-frame-box"> <p onclick="expandFunction()"> <i class="fa fa-bars"></i> </p> </div> Here is what it should change to: <div class=" ...

Adding additional text within the paragraph will result in the images shifting backwards when utilizing flexbox

It seems like there might be something obvious that I'm missing here, but essentially my goal is to create a footer with 2 columns and 2 rows. In each column, there will be an icon (32x32) and 2 lines of text next to it. The issue I'm facing is ...

overlay appears as I reveal the slide-out menu

Struggling with adding an overlay to an expanding navigation bar, similar to Youtube's overlay when the slide-out nav is open. Need help with the implementation. Here is the javascript code for the expanding navigation using jQuery: 'use strict ...

Two dropdown menus opening simultaneously

Recently, I encountered an issue while using a dropdown menu within a div. Even though it was functioning properly, I noticed that when I included two dropdown menus on the same page, clicking on the first one would cause both the first and second dropdown ...

Submitting information via jQuery

https://i.stack.imgur.com/VU5LT.jpg Currently, I am working on integrating an event planner into my HTML form. However, I am facing difficulty in transferring the data ("meetup", "startEvent", "break") from HTML to my database. Using jQuery, I was able to ...

Fetching Information from Firebase and Populating Dropdown Menus on a Website

Displayed below is a code snippet from a .JSON file stored in a Firebase database. "questionnaires" : { "ORANGE" : { "location" : "ny", "major" : { "engineering" : { "ECE" : { "3rd sem" : { "2018 ...

Guide to showcase Material UI drawer with a margin at the top

I'm having trouble implementing a Material UI drawer with some top margin instead of starting from the very top of the page. I've tried applying the marginTop property, but it's not working. You can view my code on CodeSandbox here: Drawer. ...

Experiencing difficulties replicating two auto-scrolling divs

I have implemented a script to automatically slide two different divs. Here is the code I am using: The HTML: <div id="gallery"> <div id="slider" style="width: 6000px; left: -500px;"> <div><aside class="widget widget_testimoni ...

Position the link to the right of the menu using CSS styling

I'm attempting to position a link next to my menu using CSS, but due to my limited experience with CSS I'm struggling to achieve the desired result. Currently, the link appears at the bottom left of the menu section. The code for the menu and lin ...

A mistake was found in the HTML code labeled as "enhance-security-request."

The Content Security Policy 'upgrade-insecure-requests' was included through a meta tag that is not allowed outside the document's head. Therefore, the policy has been disregarded. I encountered this issue when trying to add Bootstrap in Bl ...