CSS elements unexpectedly remain in line

I have encountered an issue with a navigation bar on one of my website projects. The HTML elements I am trying to add afterwards are not aligning properly with the navbar. I have thoroughly checked for any open HTML tags, syntax errors, or conflicting CSS styles, but the problem persists. The only solution I have found so far is using absolute positioning, which I want to avoid as I aim for flexibility in the design of my site.

nav ul {
  list-style-type: none;
  padding: 0;
}

nav li {
  display: inline;
  float: left;
}

nav a:link,
nav a:visited {
  background-color: brown;
  color: white;
  display: block;
  width: 160px;
  text-align: center;
  padding: 10px 0;
}

nav a:hover {
  background-color: black;
}

header {
  margin: 0 auto;
  background-image: url("http://localhost/prosoft_noi_orizonturi/images/header-cover.jpg");
  margin-bottom: 70px;
}

header h1 {
  color: rgba(256, 256, 256, .8);
  margin-left: 30px;
  margin-top: 30px;
  font-size: 50px;
}
<header>
  <nav>
    <h1>Welcome,
      <?php echo $_SESSION['user']?>!</h1>
    <ul>
      <li><a href="home.php">Home</a></li>
      <li><a href="search.php">Search</a></li>
      <li><a href="w8form.php">W-8 Form</a></li>
      <li><a href="notification.php">Notifications</a></li>
      <li><a href="message.php">Messages</a></li>
      <li><a href="cont.php">Account</a></li>
    </ul>
  </nav>
</header>

A visual representation of the issue in the browser can be seen here.

I would greatly appreciate any assistance in resolving this problem!

Answer №1

Check out this code snippet:

nav ul {
  list-style-type: none;
  padding: 0;
  display: flex;
}

nav li {
  display: inline;
}

nav a:link,
nav a:visited {
  background-color: brown;
  color: white;
  display: block;
  width: 160px;
  text-align: center;
  padding: 10px 0;
}

nav a:hover {
  background-color: black;
}

header {
  margin: 0 auto;
  background-image: url("http://localhost/prosoft_noi_orizonturi/images/header-cover.jpg");
  margin-bottom: 70px;
}

header h1 {
  color: rgba(256, 256, 256, .8);
  margin-left: 30px;
  margin-top: 30px;
  font-size: 50px;
}
<header>
  <nav>
    <h1>Welcome,
      <?php echo $_SESSION['user']?>!</h1>
    <ul>
      <li><a href="home.php">Home</a></li>
      <li><a href="search.php">Search</a></li>
      <li><a href="w8form.php">W-8 Form</a></li>
      <li><a href="notification.php">Notifications</a></li>
      <li><a href="message.php">Messages</a></li>
      <li><a href="cont.php">Account</a></li>
    </ul>
  </nav>
</header>

Answer №2

If you want to make the navigation not-inlined, simply remove the float: left; from the nav ul.

nav ul {
  list-style-type: none;
  padding: 0;
}

nav li {
  display: inline;
  /* float: left; */
}

nav a:link,
nav a:visited {
  background-color: brown;
  color: white;
  display: block;
  width: 160px;
  text-align: center;
  padding: 10px 0;
}

nav a:hover {
  background-color: black;
}

header {
  margin: 0 auto;
  background-image: url("http://localhost/prosoft_noi_orizonturi/images/header-cover.jpg");
  margin-bottom: 70px;
}

header h1 {
  color: rgba(256, 256, 256, .8);
  margin-left: 30px;
  margin-top: 30px;
  font-size: 50px;
}
<header>
  <nav>
    <h1>Welcome,
      <?php echo $_SESSION['user']?>!</h1>
    <ul>
      <li><a href="home.php">Home</a></li>
      <li><a href="search.php">Search</a></li>
      <li><a href="w8form.php">W-8 Form</a></li>
      <li><a href="notification.php">Notifications</a></li>
      <li><a href="message.php">Messages</a></li>
      <li><a href="cont.php">Account</a></li>
    </ul>
  </nav>
</header>

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

Efficiently input text box values into a canvas in real-time using HTML canvas and keypress

There are three text boxes labeled textbox1, textbox2, and textbox3. I am looking to transfer the values entered into these text boxes directly onto a canvas (cnv) whenever a user types in them, and remove the value from the canvas when it is deleted fro ...

"Switching Classes with a Click Event: A Step-by-

This script is designed to work with SoundCloud's widget in order to enable remote text buttons. I am attempting to modify it so that it functions as a remote for an image button that toggles between different pictures for pause and play, rather than ...

problem with overflow in HTML and CSS opera browser

My gradient is not displaying properly in the Opera browser. Can anyone help me troubleshoot? Check out the code on jsfiddle for reference:) http://jsfiddle.net/KtDTK/7/ <div class="product_1"> <div class="product_block"> <div cla ...

error in URI (not a valid URI):

Encountering an issue with a URI that is triggering a bad URI error. http://localhost:3000/api/v1/company_donations.json?token=foo&donation={&amount=101}&comment=Ordered The goal is to have the URL carry 2 attributes Token Donation obje ...

Improving the adaptive design of a breadcrumb navigation trail

.breadcrumb input[type="radio"] { display: none; } .breadcrumb input[type="checkbox"] { display: none; } .question-header input[type="radio"] { display: none; } .panel-body input[type="radio"]~label { cursor: pointer; width: 100%; text-align ...

Place the input field and submit button side by side horizontally

How can I align my submit button next to the input text in a single row, with the submit button on the right side of the input text? This is my code: <div class="mt-12"> <input id="grid-text" ...

Tips for disabling automatic sliding in Bootstrap 5 carousel specifically on larger screens

I am working with a Bootstrap 5 carousel that I need to configure to stop autosliding on large screens, but maintain autoslide functionality on mobile devices. Is there a way to achieve this using only HTML and CSS/SCSS? I am aware of the data-bs-interval ...

Customize jQuery Datepicker to default to 01-01-1970 if input box is left empty

Hey everyone, I'm having a problem with the Datepicker in my input box labeled "Order Date." Here is what my input field looks like: Whenever I submit my form with an empty date field, it automatically sets to 01-01-1970. It looks like this: So, wh ...

Get rid of unnecessary spacing in flexbox items

Hey there! I have a little design puzzle that needs solving. On mobile, everything looks great with 3 items displaying in full width. But on desktop, I'm facing a challenge: I want section A to be right above section B, while section C should remain ...

Exploring Circuits in HTML5

I am new to HTML and created a code that displays two images - one for start and the other for stop. Clicking 'start' should play a looped audio file, which should stop when 'stop' is clicked. Although the audio stops when I click &apo ...

Tips for transferring control from the first button to a JavaScript function when clicking the second button

As a newcomer to javascript and html, I have a simple query. In my javascript file, there is a snippet of code that looks like this: function setColor(btn, color) { if (btn.style.backgroundColor == "#f47121") { btn.style.backgroundColor = color; } els ...

Use a variable to determine which image to display on various pages

As a PHP beginner, I have created a loop on my page to display images in a gallery format. Each image has a title displayed below it, which is actually a link leading to a second page where only that specific image is shown. The issue I'm facing is th ...

Tips for positioning Material Ui buttons beside a list of images

I have a list of images and I would like to display buttons beneath each image Here is my current code: export const Media = (stuff) => { const { medias } = stuff; console.log(medias); const classes = useStyles(); return ( <div classNam ...

Angular CSS: ng-style applied to only one side, the other remains unaffected

I am working on an Angular application that requires a split screen with two halves, each displaying the same array values. The width and height values are set using a service and accessed by a controller. The style is applied using ng-style in the DOM. Ho ...

Tips for resolving the issue with "Text Animation"

Could someone please help with fixing this CSS animation? I want to align the text animation to the paragraph properly. I'm not confident if I am approaching this in the correct way, so if there is a simpler solution out there, I would appreciate lea ...

Creating a border for a checkbox using CSS

I need help with styling a checkbox using jQuery $("#reg_checkbox").css("border","thin solid red"); The border displays correctly in Internet Explorer, but not in Mozilla. How can I ensure browser compatibility for this styling? ...

PHP unable to display echo containing line breaks and text in nested lists

Currently, I am tackling an issue with a multi-select list in my PHP pages. The problem arises when I select text and it appears in the wrong order due to the lack of wrap or text break on each item. Below is the PHP code I have written: <select id="SK ...

What is the best method for converting a list tag into an array of objects with XPath?

I am attempting to extract the ordered list and generate an array of list tags along with their content. I have experimented with different paths, such as: //li[div/@class="business-info"] //li[div[@class="business-info"]] //li[descendant::div[@class="bu ...

Ensure that the div is aligned properly with the div above

I've been struggling with the alignment of divs on my website for the past day. Any tips on how to properly align the image (the text image) inside a div? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1 ...

Is it possible to designate a specific position for absolutely positioned elements within a webpage?

Is it possible to specify a reference element for an absolutely positioned element in addition to its closest positioned ancestor? I am curious if there are any CSS or jQuery methods that allow for this customization. ...