incorrect sequence of div elements appearing vertically

I'm having trouble organizing the header, page title, and navigation bar on my webpage. Despite my attempts to structure them correctly, they are not displaying in the desired order as shown below: https://i.stack.imgur.com/A3rQe.jpg The issue arises when I try to style the background of the navigation items by enclosing them in a div with a background color. This results in the box overlapping the page title, creating an undesirable layout like this: https://i.stack.imgur.com/flT3M.jpg

How can I ensure that the navigation box aligns properly with the navigation menu options?

Below is the code snippet I am currently using:

/* HEADER */

header .headerBox {
  background-color: #000000;
  width: 100%;
  min-height: 50px;
}

.container {
  width: auto;
... (rest of CSS code) ... 

<body>

  <!-- HEADER -->

  <header>
    <div class="headerBox">
      <div class="container">
        <nav>
          <ul>
            <li><a href="index.html"><span class="name">Company name</span></a></li>
            <li><a href="account.html"><span class="account">Account</span></a></li>
          </ul>
        </nav>
      </div>
    </div>
  </header>

  <!-- PAGE TITLE -->

  <div>
    <section class="page-title">
      <div class="container">
        <h1>Page Title</h1>
      </div>
    </section>
  </div>

  <!-- NAVIGATION BAR -->

  <div class="navigationBox">
    <div class="container">
      <ul>
        <li><a href="menu-option-1.html"><span class="menu-item">Menu Option 1</span></a></li>
        <li><a href="menu-option-2.html"><span class="menu-item">Menu Option 2</span></a></li>
      </ul>
    </div>
  </div>

</body>

Answer №1

This code snippet demonstrates the use of the CSS float property without any clearfix. I have included a link to the fixed version of the code on JSFiddle.

CSS

/* HEADER */

.clearfix::after {
  content: "";
  clear: both;
  display: table;
}

header .headerBox {
  background-color: #000000;
  width: 100%;
  min-height: 50px;
}

.container {
  width: auto;
  margin-left: 10%;
  margin-right: 10%;
}

header .name {
  font-family: 'Helvetica', sans-serif;
  font-weight: normal;
  color: #ffffff;
  float: left;
  margin-top: 10px;
}

header .account {
  font-family: 'Helvetica', sans-serif;
  font-weight: normal;
  color: #ffffff;
  float: right;
  margin-top: 10px;
}

/* PAGE TITLE */

.page-title {
  width: 100%;
  color: #000000;
  font-family: 'Helvetica', sans-serif;
  font-weight: normal;
  text-decoration: none;
  font-size: 35px;
  float: left;
  padding: 0 0 0 0;
  margin: 80px 0 80px 0;
  background-color:green;
  color:white;
}

/* NAVIGATION BAR */

.navigationBox {
  background-color: rgba(80, 120, 180,1);
  width: 100%;
  height: 32px;
}

.menu-item {
  color: #000000;
  font-family: 'Helvetica', sans-serif;
  font-weight: 300;
  text-decoration: none;
  font-size: 16px;
  float: left;
  padding: 0 0 0 0;
  margin: 0 120px 0 0;
}

ul {
  margin: 0 0 0 0;
  padding: 0 0 0 0;
}

li {
  display: inline;
  margin: 0 0 0 0;
  padding: 0 0 0 0;
}

HTML


  <!-- HEADER -->

  <header>
    <div class="headerBox">
      <div class="container">
        <nav>
          <ul class="clearfix">
            <li><a href="index.html" class="name"><span>Company name</span></a></li>
            <li><a href="account.html" class="account"><span>Account</span></a></li>
          </ul>
        </nav>
      </div>
    </div>
  </header>

  <!-- PAGE TITLE -->

  <div class="clearfix">
    <section class="page-title">
      <div class="container">
        <h1>Page Title</h1>
      </div>
    </section>
  </div>

  <!-- NAVIGATION BAR -->

  <div class="navigationBox">
    <div class="container">
      <ul class="clearfix">
        <li><a href="menu-option-1.html" class="menu-item"><span>Menu Option 1</span></a></li>
        <li><a href="menu-option-2.html" class="menu-item"><span>Menu Option 2</span></a></li>
      </ul>
    </div>
  </div>

Answer №2

To ensure proper alignment in your CSS, you can add the property clear:both:

.navigationBox {
  clear:both;
}

The clear property dictates which sides of floating elements are prohibited from floating. It defines the position of the element with respect to floated objects. If the element can fit next to another floated element horizontally, it will.

Syntax:

clear: none|left|right|both|initial;

Using clear:both restricts floating elements from floating on both sides simultaneously.

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

PHP Warning: Attempt to access undefined variable

Whenever I try to insert a new record into my HTML, I keep encountering errors. I'm unsure if it's due to incorrect code on my end or if the issue lies with inserting the records. <?php $servername = "localhost"; $dbusername = "r ...

When a CSS fluid layout includes a containing div with margin or padding, it may lead to

On my jsfiddle , the outermost wrapper div has a width of 100%. I am trying to create an inner div (tb_margin or tb_padding) where the content starts 40px from the left. I have attempted to use both margin and padding for this left spacing, but in both cas ...

Guide to accessing HTML elements and saving them in a JSON formatted text using JavaScript

If you have an html form with labels, select boxes, and radio buttons, you can use javascript to store the child elements of that form in a json string format. Here is an example: { "label": { "content": "This is a label" }, "textbox" ...

The AJAX request to the URL is failing

Creating a webpage with the ability to control an IP camera's movements such as moving up and down or zooming in and out involves calling specific URLs. While trying to implement this feature asynchronously, I encountered issues using AJAX which did n ...

Learn how to align a form to the right using HTML, CSS, and Bootstrap

I am completely new to front-end technology. I have been attempting to write some code for my backend app using Html, Css, and Bootstrap. Currently, I am trying to place a search form on the right side of my webpage. Can anyone help me figure out what I am ...

What is the process for inserting an image into a table using el-table and el-table-column components in Vue.js while utilizing ui-elements?

I'm new to Vue.js and successfully built a basic table using the ui-element. The el-table element was utilized for constructing the table, with columns displayed using el-table-column and prop (see code below). Now, I want to incorporate images/avatar ...

AngularJS: resolving route dependencies

I have a variable $scope.question that contains all the questions for the page. My goal is to loop through the questions page by page. To achieve this, I created a function called questionsCtrl and I am calling this function in the config while setting up ...

Image broken despite accurate file path in next.js and tailwind

https://i.stack.imgur.com/2ZqPa.png In my components folder, specifically in navbar.js, I have a path to the image folder <img className="block lg:hidden h-8 w-auto" src='../images/logo_injoy.png' alt="Logo" /> U ...

My JavaScript functions are not compliant with the HTML5 required tag

I am currently developing input fields using javascript/jQuery, but I am facing an issue with the required attribute not functioning as expected. The form keeps submitting without displaying any message about the unfilled input field. I also use Bootstrap ...

Tips for wrapping text within a span element that is positioned absolutely

Need a long string for the bullet text as shown in the code, they can't be separate words. Having trouble with overflow auto, it's cutting off the bullet text. The bullet text must remain in absolute position. How can I make the text wrap to t ...

The alignment of inline-block elements is not being maintained on the same line

Here's a question I have about my embedded form. Why does the display property of inline-block make the "submit" and "terms" elements appear higher than the email field? And more importantly, how can I fix this issue? I've attempted to use the t ...

The astonishing font-icon animation feature is exclusively functional on code-pen

I have a problem with a font-icon animation code. When I run it on my local server, the animation doesn't work. It only works on http://codepen.io/TimPietrusky/pen/ELuiG I even tried running it on http://jsfiddle.net/qjo7cf3j/ @import url(http: ...

keeping variables hidden from the user until a certain action is taken

In my project, I am working with three main code classes: 1) node.js (utilizing express framework) 2) index.html (serving as the home page when users visit the site) 3) sck.js which performs a specific function (details to follow on the first and second fi ...

Meta tags are causing issues with HTML5 validation on the website

Striving to enhance the HTML validity of my website, I encountered several errors in the meta tags that are marked as invalid. Unfortunately, I am unsure how to modify them to rectify these errors. <meta http-equiv="X-UA-Compatible" content="IE=edge,ch ...

DIV size issue resolved

Content within a div is surpassing the set fixed size determined by CSS. <style> #fixeddiv { position: fixed; margin: auto; max-height: 300px; max-width: 700px; } </style> <div id="fixeddiv"> <div id="M77 ...

Unable to present the information due to a codeigniter error in retrieving the data

Encountering an issue where the data cannot be displayed and it's showing an error message of undefined variable: avg. Below is the script being used: The controller function: public function rate_config() { $rt = $_POST['hasil_rating&a ...

Navigation bar's active area does not span the full height

I'm facing some issues with the navigation bar on my responsive website. One issue is that the clickable area for the links does not extend to the full height of the nav bar, and I would like it to cover the entire height. Another problem arises whe ...

Is it possible to dynamically load a specific div when the page loads, relying on the

I am using JQuery SlideUp and slideDown methods to toggle the visibility of panels. How can I load or display the first record's contact information as a default? Currently, it loads blank because I set the panel's display property to none: < ...

Tips for showing text beyond the confines of a <div></div> container

<div class="signup"> <div>Tenxian アカウントに必要な情報</div><br/> </div> <br/><br/><br/><br/><br/><br/> <div align="center">@2009 Tenxian &nbs ...

Steps to resolve background image problems

Currently encountering issues with the application where the background image is not showing up behind the login form. This problem arises while using a bootstrap template for the website. Attempted to set the background image in the .main-content div with ...