The properties of margin:auto and text-align:center are failing to function as expected

I'm facing an issue while creating a website. The 'margin: auto' and 'text-align: center' properties in my CSS code seem to not be functioning as expected. Can someone please review my code in inspect element and identify the problem?

body {
  font-family: sans-serif, 'Helvetica';
  background-color: #f9f9f9;
}
.dropdown {
  position: relative;
}
.dropdown-content {
  display: none;
  position: absolute;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
  display: block;
}
.dropdown:hover .dropdown-content {
  display: block;
}
#my_cycle_head {
  text-align: center;
}
#main_navbar {
  text-align: center;
  margin: 0 auto;
  margin-right: auto;
  margin-left: auto;
  width: 800px;
}
#main_navbar nav a {
  text-decoration: none;
  text-align: center;
}
#main_navbar nav {
  display: inline-block;
  text-align: center;
  padding-right: 20px;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Cycle - Home</title>
  <link rel="stylesheet" href="css/style.css" type="text/css" />
</head>

<body>
  <header>
    <h1 id="my_cycle_head">My Cycle</h1>
    <navbar id="main_navbar">
      <nav><a href="#">Home</a>
      </nav>
      <nav class="dropdown">
        <a class="dropbtn">Dropdown</a>
        <div class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </nav>
      <nav><a href="faq.html">FAQ's</a>
      </nav>
      <nav><a href="about.html">About</a>
      </nav>
    </navbar>
  </header>
</body>

</html>

Answer №1

<navbar> isn't a recognized HTML element. If you had run your code through a validator, this error would have been caught.

Unidentified elements are inserted into the DOM with default styling that sets them to display: inline.

Setting auto margins and using text-align won't have any impact on elements set as display: inline.

Make sure to use proper HTML by replacing <nav> (which defaults to display: block) instead.

Answer №2

Your usage of navbar has been corrected to nav. A series of ul and list items have been created within it to replace the individual nav elements. The purpose of the nav element is to contain navigation elements rather than marking off each individual point of navigation (more details can be found here: ).

The CSS has also been rearranged, utilizing the child selector (https://css-tricks.com/child-and-sibling-selectors/) to prevent styles from affecting the dropdown submenu. In this revised version, all text is now centered.

    body {
      font-family: sans-serif, 'Helvetica';
      background-color: #f9f9f9;
    }
    #my_cycle_head {
      text-align: center;
    }
    #main_navbar {
      text-align: center;
      margin: 0 auto;
      margin-right: auto;
      margin-left: auto;
      width: 800px;
    }
    #main_navbar > ul > li {
      display: inline-block;
      text-align: center;
      padding-right: 20px;
    }
    #main_navbar > ul > li > a {
      text-decoration: none;
      text-align: center;
    }
    .dropdown {
      position: relative;
    }
    .dropdown-content {
      display: none;
      padding:0;
      position: absolute;
      min-width: 160px;
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    }
    .dropdown-content > li {
      display: block;
    }
    .dropdown:hover .dropdown-content {
      display: block;
    }
<header>
  <h1 id="my_cycle_head">My Cycle</h1>
  <nav id="main_navbar">
    <ul>
      <li><a href="#">Home</a>
      </li>
      <li class="dropdown">
        <a class="dropbtn">Dropdown</a>
        <ul class="dropdown-content">
          <li><a href="#">Link 1</a>
          </li>
          <li><a href="#">Link 2</a>
          </li>
          <li><a href="#">Link 3</a>
          </li>
        </ul>
      </li>
      <li><a href="faq.html">FAQ's</a>
      </li>
      <li><a href="about.html">About</a>
      </li>
    </ul>
  </nav>
</header>

Answer №3

Kindly update the navigation bar using a div element

<body>
  <header>
    <h1 id="my_cycle_head">My Cycle</h1>
    <div id="main_navbar">
      <nav><a href="#">Home</a>
      </nav>
      <nav class="dropdown">
        <a class="dropbtn">Dropdown</a>
        <div class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </nav>
      <nav><a href="faq.html">FAQ's</a>
      </nav>
      <nav><a href="about.html">About</a>
      </nav>
    </div>
  </header>
</body>

Sample implementation: https://jsfiddle.net/Lt0y3ba1/

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

Establish the minimum and maximum values for the text field depending on the selection made in the previous dropdown menu

For my project, I need to create a dropdown menu and a text box positioned next to it. I want the text box to have specific character limits based on the option selected from the dropdown. For example, if "Option 1" is chosen, then the textbox should only ...

Inject the variable into location.href

I am dealing with a dynamic URL, and I want to achieve the following in my HTML: <a onclick="location.href='myVariable';"><span>Click here</span></a> The issue is that this approach displays the actual string 'myVar ...

AJAX: Displaying the contents of a folder. Issue with URL resolution

I'm attempting to showcase a collection of images stored in a specific folder within a div. My approach involves utilizing AJAX within a JavaScript file titled edit, which is positioned one directory away from the index route. This implementation is b ...

Using Angular 8 to Pass Form Data to Another Component via a Service

Is there a way to send all the Formgroup data as a Service in Angular to Another Component without using ControlValueAccessor? I want the receiver to automatically receive the value data whenever someone enters information on a form. I am attempting to mo ...

Modification in background when PNG image is hovered upon

Having an issue in Chrome that doesn't occur in Firefox. When hovering over a PNG within a carousel plugin or dragging an element with Jquery, the hover image briefly distorts the background. Looking for a solution to improve user experience. Let me k ...

Utilizing the CSS Flex property to position one element in the center and align the other element to either the left or right side

What is the best approach to center one element inside a container with CSS flex properties and align another element to the left or right? Even when I set ChatGPT's recommendations of margin-left and margin-right properties to auto for the right-ali ...

Javascript: Dynamically Altering Images and Text

One feature on my website is a translation script. However, I'm struggling to activate it and update the image and text based on the selected language. This is the code snippet I am currently using: <div class="btn-group"> <button type ...

Essential Understanding of HTML Query Strings Required

As a newcomer to the world of web design, I have taken on what seems like a challenging task for me: creating a webpage that can send a query string to the German Railway website (bahn.de) with the parameters I input. My question now is whether there is a ...

Automatic popup updates when submitting a form in a Chrome extension

Looking to develop a chrome extension popup window that, when clicked, will present a form for users to input their name and college. The goal is to save this data in chrome storage and then replace the popup with a new window displaying a greeting message ...

Transferring items between containers with stylish animations in AngularJS

Two of my divs have different contents, with one initially empty and the other filled with items. By clicking on an item in the first list, I want to transfer it over to the second list through a visual animation that implies movement from right to left. W ...

Issue with the flexbox div not fully occupying the vertical space

I can't seem to get flexbox to fill the spaces as I want it to. I've been trying to figure out what I'm missing, but I just can't wrap my head around it. Here's a recreation of the issue with a link to a codepen. I want the div in ...

CSS Horizontal Menu positioned left losing its background color due to float property

Can you explain why the background color of the ul element disappears when its child elements are floated? I have a vague memory of reading something about floats causing this issue, but I can't recall the details. (JSFiddle) ul { padding-left: ...

Creating a mind map: A step-by-step guide

I'm currently developing an algorithm to create a mind map. The key focus is on organizing the nodes intelligently to prevent any overlap and ensure a visually pleasing layout. Take a look at this snapshot (from MindNode) as an example: Any suggestio ...

Clicking the button to send the form using JavaScript

Currently, I am dealing with a shopping cart plugin that relies on a basic form on the product page to send values such as price and product name. However, I am facing an issue where this plugin uses a standard submit button to transmit the values, and I w ...

Determining the margin-left value of a centrally positioned table

I have a table with variable columns, meaning the columns of the table are dynamic. The number of columns will keep changing, causing the table to be centralized and the margin-left value to adjust accordingly. Now, I need to calculate the margin-left val ...

Dynamic rows in an Angular 2 Material data table

I'm currently working on dynamically adding rows to an Angular 2 Data Table ( https://material.angular.io/components/table/overview) by utilizing a service called "ListService". This service provides me with the columns ("meta.attributes") to be displ ...

Execute the function if the text or value begins with a certain character

I'm trying to determine whether the text within a span starts with the letter "x" and then execute a function. I've come across using "^" in strings for this purpose, but am having trouble implementing it to check the text... <span>xfoo&l ...

Fixing Half Screen Sidebars

I have a query regarding my coding problem. I am trying to create two pop-ups that occupy half of each screen. As I am new to JavaScript and jQuery, I want to ensure that I am doing it correctly. Is there a way for the left side to slide out from the left ...

The autocomplete attribute is not compatible with GroceryCrud functionality

I attempted to implement the autocomplete feature using an example from w3schools. https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_form_autocomplete Although I used jQuery to modify the form and add autocomplete="on" to both the form and input ...

Ensure that all links are opened in a new tab

When including _blank in the a href URL, my website contains various elements like iframes and ads from Adsense, Taboola,, etc. Currently, when a user clicks on an iframe or ad, it opens in the same window. Is there a way to ensure that all URLs (includin ...