Any tips on how to position my navigation bar in the center of the screen?

My current issue involves my Nav Bar. I am striving to center it on the screen, positioned in the middle of everything. However, my Nav Bar stubbornly remains fixed to the left side of the screen.

I attempted to utilize Margin to rectify this problem, but found that it compromised its responsiveness to the rest of the layout, rendering that solution ineffective.

Below is the code for my Nav Bar:

HTML:

 <nav>
    <ul>
        <li>
            <a href="BlogTest.html">
                Home
            </a>
        </li>
        <li>
            <a href="BlogTest.html">
                About Me
            </a>
        </li>
        <li>
            <a href="BlogTest.html">
                Contact Me
            </a>
        </li>
        <li>
            <a href="BlogTest.html">
                Blog
            </a>
        </li>
    </ul>
</nav>  

Disregard the "href" at the moment, they will be adjusted later

CSS:

    *{
    margin: 0%;
    padding: 0%;
    background-color: rgb(53, 53, 53);
    text-decoration: none;
}

nav{
    width: 100%;
    height: 100px;
    text-align: center;
    display: inline-block;
}

ul{
    text-align: center;
}

ul li{
    list-style: none;
    display: inline-block;
    float: left;
    line-height: 100px;
}

ul li a{
    font-family: 'PT Sans', sans-serif;
    color: white;
    font-size: 200%;
    padding: 0px 20px;
    display: block;
    display: inline-block;}

ul li a:hover {
    color: red;
}

Despite reviewing other answers to similar queries, I have not had any success so far. Hopefully, you can provide some much-needed assistance!

Answer №1

To ensure that the ul element is only as wide as its contents and enable the text-align: center property for the nav, you simply need to include display: inline-block; in the CSS rule for ul:

* {
  margin: 0%;
  padding: 0%;
  background-color: rgb(53, 53, 53);
  text-decoration: none;
}

nav {
  width: 100%;
  height: 100px;
  text-align: center;
  display: inline-block;
}

ul {
  display: inline-block;
  text-align: center;
}

ul li {
  list-style: none;
  display: inline-block;
  float: left;
  line-height: 100px;
}

ul li a {
  font-family: 'PT Sans', sans-serif;
  color: white;
  font-size: 200%;
  padding: 0px 20px;
  display: block;
  display: inline-block;
}

ul li a:hover {
  color: red;
}
<nav>
  <ul>
    <li>
      <a href="BlogTest.html">
                Home
            </a>
    </li>
    <li>
      <a href="BlogTest.html">
                About Me
            </a>
    </li>
    <li>
      <a href="BlogTest.html">
                Contact Me
            </a>
    </li>
    <li>
      <a href="BlogTest.html">
                Blog
            </a>
    </li>
  </ul>
</nav>

Answer №2

Consider placing the navigation inside center tags for better alignment.

Here is an example:

<center> 
    <nav>
        <ul>
            <li>
                <a href="BlogTest.html">
                    Home
                </a>
            </li>
            <li>
                <a href="BlogTest.html">
                    About Me
                </a>
            </li>
            <li>
                <a href="BlogTest.html">
                    Contact Me
                </a>
            </li>
            <li>
                <a href="BlogTest.html">
                    Blog
                </a>
            </li>
        </ul>
    </nav>
</center>

If this suggestion doesn't address your issue, please let me know so I can provide further assistance.

Answer №3

Give this a shot. Avoid using the <center> tag, as it's considered to be outdated.

Here is the HTML:

<header>
<nav>
<ul>
<li>Menu</li>
</ul>
</nav>
</header>

And here is the CSS:

header { width: 100%; }
nav { width: 980px; margin: 0 auto; display: block; }

By adding margin: 0 auto; you can center the element. Just ensure that it has a specified width for this to take effect.

Answer №4

Check out this code for a stylish navigation bar!

* {
  margin: 0;
  padding: 0;
  background-color: rgb(53, 53, 53);
  text-decoration: none;
}

nav {
  position: absolute;
  width: 100%;
  height: 100px;
  text-align: center;
  display: block;
  margin: auto;
  border: 2px solid white;
}

ul {
  display: inline-block;
  text-align: center;
  width: max-content;
  
}

ul li {
  list-style: none;
  display: inline-block;
  float: left;
  line-height: 100px;
  margin: auto;
}

ul li a {
  font-family: 'PT Sans', sans-serif;
  color: white;
  font-size: 100%;
  padding: 0px 10px;
  display: inline-block;
}

ul li a:hover {
  color: red;
}
<nav>
    <ul>
      <li>
        <a href="BlogTest.html">
                    Home
                </a>
      </li>
      <li>
        <a href="BlogTest.html">
                    About Me
                </a>
      </li>
      <li>
        <a href="BlogTest.html">
                    Contact Me
                </a>
      </li>
      <li>
        <a href="BlogTest.html">
                    Blog
                </a>
      </li>
    </ul>
  </nav>

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

Providing a user with a special discount tailored to their email address?

<script type="text/javascript"> function updatePrice() { var price = document.getElementById("product").value; var size_price = document.getElementById("size").value; var a=parseInt(price);//parsed type price var b=parseInt(size_price);//pa ...

Discover the row and column of a selected table cell using vanilla JavaScript, no need for jQuery

In my JavaScript code, I am currently working on creating an onclick function that will display the row and column of a specifically clicked cell in a table. I have successfully implemented functionality to return the column number when the cell is click ...

Slider with FadeIn effect remains unresponsive to the FadeOut command (JQuery / Javascript)

I'm currently working on a slider that is supposed to fade in and out. However, I am facing an issue where the slide fades in correctly but instantly disappears instead of fading out. Do you have any insights into why this might be happening and any s ...

Height of dropdown items in the horizontal navbar

I am working on a navbar that includes a dropdown menu. Currently, the dropdown items are being displayed horizontally in full width with a larger height. However, I would like them to be displayed vertically in full width and with a smaller, normal height ...

The text link on an iOS device is trapped by a persistent blue border

I've encountered an issue on my iOS device. When I open the menu with a popup, there is a blue border around the text element, even though it's not a hyperlink underline. https://i.sstatic.net/WA3rz.jpg I attempted to resolve this using the fol ...

How can I expand and collapse all the Bootstrap panels simultaneously?

Is it possible to make all three panels collapse and expand simultaneously with just one button? <button data-toggle="collapse" data-target="#collapseOne-1,#collapseTwo-1,#collapseThree-1" ...></button> <div id="#collapseOne-1" class="coll ...

Adding a half circle connector in HTML can be accomplished by using SVG (Scal

My task is to replicate the construction shown in this image: https://i.sstatic.net/kaRMO.png I have written the entire code but I am unsure how to include a half circle next to the full circle and connect it with a line connector. Here is my code: .ps- ...

The favicon fails to appear properly when sharing the link on Instagram

Whenever I share my website link on Instagram, it shows an image that is not the one I want to appear. Oddly enough, when I send the URL through a text message, the Favicon displays correctly. How can I resolve this problem? https://i.sstatic.net/siQVs.pn ...

Can someone explain the purpose of adding "v=747" at the end of /site_media/base.css?v=747?

Observing a webpage's source code, I came across the following CSS declaration in the header: <link rel="stylesheet" href="/site_media/base.css?v=747" /> Could you please explain the significance of "?v=747" at the end of this CSS declaration? ...

Eliminate unnecessary transparency in React Material UI Tooltip / Popper by adjusting opacity restrictions

Looking to integrate the Tooltip component from the React Material UI Library into my project. The code snippet I am using is as follows: <WhiteOnDarkGreyTooltipWithControls disableTouchListener disableFocusListener title={ <Text selectable ...

Does text alignment apply to one tag but not the other?

Hello, I am in the process of creating a basic webpage and encountering some formatting issues. When I apply the style format to one element, it aligns perfectly, but for another element it doesn't seem to be working. I'm certain it's a simp ...

A <mat-card> does not have a raised appearance because it lacks a border

Looking for help with my HTML code. Here's the setup: <mat-card> <mat-card-content> <code>{{file}}</code> </mat-card-content> </mat-card> But when the mat-card element is rendered, it's not ra ...

Retrieving and storing information from a form without the need to submit it

I have been given the task of creating a load/save feature for a complex form. The goal is to allow users to save their progress and resume working on it at a later time. After much consideration, I have decided to implement server-side storage by saving ...

Unable to extract HTML text using Selenium Java

Here is the HTML code I am working with: <span id="slotTotal"> <span id="slotUsed">4</span> /16 </span> I am trying to extract the text "/16" but encountering an issue when using this code: slotTot = driver.findElem ...

Saving the order of elements dropped in jQuery UI Droppable into an input field

Link to setup: https://jsfiddle.net/7b8bqzsh/1/ You can view a full working example using the link provided above. The objective is to store the order of the links in an input field so that it can be saved to a database. Currently, only the latest ID is ...

How can the color of glyphicons be adjusted within an AngularJS ng-class that is constantly changing?

I am currently facing an issue with my function called lessThan(a,b) that checks if "a" is less than "b" and returns a boolean value. I have added a glyphicon to a form to display a check mark when true and an X when false, but I can't get the colors ...

Combine several svg elements within a single div row using Bootstrap 5

Is there a way to adjust the SVG settings in order to have two of them side by side within a div class="col" element? ...

Guide to repairing a CSS ball animation

I am attempting to create a simulation where a ball moves in the following pattern: bottom right -> top center -> bottom (/) left. However, my animation seems to be moving from top right -> bottom center -> top left (/). #stage { height:300 ...

utilizing a Bootstrap modal element throughout multiple HTML documents

I have a bootstrap modal dialog div that I want to use in all 4 html pages of my web application without repeating the code. I have a common JavaScript file for this purpose. What is the best way to achieve this? <!-- Modal --> <div class="modal ...

The css values for component _nghost-c0 in an Angular application

Recently, I've been delving into Angular 5 and couldn't help but notice the peculiar html tags with ng generated attributes like _nghost-c0 and _nghost-c1... This got me wondering, what exactly do these attributes signify? [_nghost-c3] .employee ...