0

So I created a navigation bar using UL's and LI's. But I'm having trouble centering it with my webpage. Here's my code:

    <style>
        ul {
            text-align: center;
            list-style-type: none;
            display: inline-block;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: rgb(111, 145, 156);
        }
        
        li {
            float: left;
            display:inline;
            border-right:1px solid rgb(79, 100, 107);
        }

        li:last-child {
            border-right: none;
        }
        
        li a {
            display: block;
            color: white;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
        }
        
        li a:hover {
            background-color: rgb(96, 35, 29);
        }

        li a:hover:not(.active) {
            background-color: rgb(79, 100, 107);
        }

        .active {
            background-color: rgb(127, 45, 37);
        }
    </style>

    <ul class="nav">
        <li><a href="{% url 'index' %}">Home</a></li>
        <li><a class="active" href="{% url 'aboutus' %}">About us!</a></li>
        <li><a href="{% url 'orderentry' %}">Order here!</a></li>
        <li><a href="{% url 'shipmenttracking' %}">Track your shipment</a></li>
        <li><a href="{% url 'ticketmanage' %}">Having trouble? Click here to get help!</a></li>
    </ul>

And it looks like this

You're probably thinking the issue is my li float: left;

But here's what it looks like when it's center

I'm at a loss. Any help is appreciated. Thanks.

kvc921
  • 11
  • 3

1 Answers1

1

You may take advantage of FLEX. Just add a div wrapping the ul tags and set a class to it, for example, a wrapper class name. After that, include this to your CSS:

.wrapper {
      width: 100vw;
      display: flex;
      justify-content: center;
    }

You can reproduce the solution here: https://jsfiddle.net/braganca/sm7x2qhy/22/

Here is the complete code:



<html>
   <head>
      <style>
         .wrapper {
         width: 100vw;
         display: flex;
         justify-content: center;
         }
         ul {
         text-align: center;
         list-style-type: none;
         display: inline-block;
         margin: 0;
         padding: 0;
         overflow: hidden;
         background-color: rgb(111, 145, 156);
         }
         li {
         float: left;
         display:inline;
         border-right:1px solid rgb(79, 100, 107);
         }
         li:last-child {
         border-right: none;
         }
         li a {
         display: block;
         color: white;
         text-align: center;
         padding: 14px 16px;
         text-decoration: none;
         }
         li a:hover {
         background-color: rgb(96, 35, 29);
         }
         li a:hover:not(.active) {
         background-color: rgb(79, 100, 107);
         }
         .active {
         background-color: rgb(127, 45, 37);
         }
      </style>
   </head>
   <body>
      <div class="wrapper">
         <ul class="nav">
            <li><a href="{% url 'index' %}">Home</a></li>
            <li><a class="active" href="{% url 'aboutus' %}">About us!</a></li>
            <li><a href="{% url 'orderentry' %}">Order here!</a></li>
            <li><a href="{% url 'shipmenttracking' %}">Track your shipment</a></li>
            <li><a href="{% url 'ticketmanage' %}">Having trouble? Click here to get help!</a></li>
         </ul>
      </div>
   </body>
</html>

If you don`t know CSS Flexbox yet, please consider reading https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Braganca
  • 106
  • 1
  • 8