elements in suspension over non-suspended element:

I implemented a two column layout in my website, where one column is floated to the left and another is not:

<div id="left">
    LEFT
</div>

<div id="right">
    RIGHT
</div>

Here is the CSS code for the layout:

#left {
    float: left;
    width: 200px;
    background: yellow;
}

#right {
    margin-left: 200px;
    background: orange;
}

In the non-floated right element, I have structured like this:

    <div id="nav">
        <div class="item">LINK</div>
        <div class="item">LINK</div>
        <div class="item">LINK</div>
        <div class="item">LINK</div>
        <div class="item">LINK</div>

        <div class="clear"></div>
    </div>

    <h1>Welcome World</h1>

The CSS styles for nav and item are as follows (notice that item is floated):

#nav {
    background: blue;
}

.item {
    background: green;
    float: left;
    margin-right: 10px;
}

The definition of the clear element is:

.clear {
    clear: both;
}

However, the result of this setup differs from what I expected. It seems that the issue lies with the clear element affecting the floated element (#left). What I anticipated was:

Check out the fiddle demo here

Answer №1

Instead of cluttering your HTML code with unnecessary mark-up, a simpler solution is to just include overflow: hidden; in the styles for #nav. This will establish a new block-formatting context for the list-items inside #nav, as floated elements disrupt the normal flow (you can see this by noticing that the <body> does not extend down to #left in my example)

Referencing the Visual Formatting Model, 9.4.1:

Floats, absolutely positioned elements, block containers (like inline-blocks, table-cells, and table-captions) that are not block boxes, and block boxes with 'overflow' other than 'visible' (except when that value has been propagated to the viewport) create new block formatting contexts for their contents.

#nav {
background: blue;
overflow: hidden; /* Establishes a new block-formatting context
                     for floated elements */
}

http://jsfiddle.net/bJFUj/9/

Answer №2

See the Live Demo on jsFiddle

To create a responsive navigation menu, style your #nav element with display: inline-block and a width of 100%:

#nav {
    background: blue;
    display: inline-block;
    width: 100%;
}

Answer №3

I approached this problem from a different angle. By adjusting the placement of the clear property, you can achieve varying outcomes while still resolving the underlying issue at hand. Feel free to choose the option that best fits your needs.

(http://jsfiddle.net/bJFUj/4/ OR http://jsfiddle.net/bJFUj/6/)

In both scenarios, I made modifications to the nav's CSS to ensure that the background displays as intended.

#nav {
    background: blue;
    overflow: hidden;
}

Personally, I recommend moving the clear property to the container that holds both div#left and div#right. Placing it within div#right appears to be causing some intriguing effects in terms of the height of div#right.

Answer №4

Sample on JSFiddle

Extracted from the navigation bar

<div class="clear"></div>

Inserted new style to the navigation bar

overflow: hidden;

Answer №5

When dealing with clearing floats, it's important to consider elements that are floated. In this case, you have cleared an element that is not floated either left or right, specifically the #nav. To fix this, simply add float: left; to your #nav so that your

<div class="clear"></div>
will function as intended.

Check out the demo here

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

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 ...

Utilizing Python's Requests Library to Submit a Form without Specifying Input Names

My task is to perform a POST request using the Python Request module. However, I am facing an issue where the input I need to work with only has an id attribute and no name attribute. Most of the examples I have come across involve using the name attribute ...

Tips for identifying repeating id numbers within a form?

I recently completed a form with approximately 800 fields, only to realize that I accidentally gave some of the fields the same ID. Can someone please advise me on how to track down these duplicate IDs? ...

Building an HTML table dynamically with JavaScript

Can anyone help me figure out why my JavaScript code isn't populating the HTML body table as expected? var shepard = { name: "Commander", victories: 3, ties: 1, defeats: 6, points: 0 }; var lara = { name: "RaiderOfTombs", victories: ...

Creating a form with an input field and an image side by side in HTML

I've been attempting to display an HTML input element and an SVG image on the same line without success. I've tried various solutions from stackoverflow, but none seem to work for me. Here's my current HTML code: <div id = "inline_eleme ...

Output information to the specified divID using Response.Write

Currently, I have knowledge of javascript and I am currently expanding my skills in ASP.NET C#. My goal is to achieve the following task using javascript: document.getElementById('divID-1').innerHTML= '<p>Wrong Password< ...

Iterating over a map object using ngFor

I'm having some trouble grasping the concept of how ngFor interacts with map. Here is the HTML code I am working with: <div *ngFor="let country of wars.getCountrys()"> And this is the TypeScript code: wars: Map < number, Array < count ...

dormant HTML toggle for guests

I am looking to include an HTML switch on my webpage, but with a twist - I want to be the only one able to change its status while visitors can only view it. This is what I currently have: .onoffswitch { position: relative; width: 122px; -webkit- ...

Creating void placeholders in a repeated loop

Suppose we have the data array stored in "$my_array". Below is an example of the expected output based on this data. Please note that due to restrictions, I am unable to modify the way data is inserted into the database. Hence, I need to create empty colu ...

Problem with the icon and toggle button in Bootstrap navbar

While working on the collapsible navbar, I encountered an issue with the logo image not being responsive. To address this, I implemented a media query to resize it. However, upon shrinking the screen, I noticed that the toggle button shifted down and the i ...

"Enjoy a full-screen background that adjusts its height while you scroll

Here is how my HTML elements are nested: body { app-root { app-block-page { app-block-content { div.wrapper { *content* } } } } } body has width:100% and ...

My div is not displaying the background image properly after adding the Bootstrap CSS

Strangely enough, Bootstrap seems to be interfering with the background image of a div. The following is the HTML code: <div class="wide"> <div class="col-xs-5 line"> <hr> </div> <div class="col-xs-2 logo"> Log ...

Real-time Exchange of HTML Data Forms

Hey everyone, I am completely new to programming and I would really appreciate your guidance through this process. Let's imagine a scenario where two users need to communicate through HTML forms. The first user fills out an HTML form (first name, la ...

I rely on Nebular for my Angular application, however, I am looking to make some manual CSS changes that I am having difficulty implementing

While using Nebular for my Angular app, I have run into issues with changing some CSS manually. Specifically, when using nb-select or nb-toggle, I am unable to customize the CSS as desired. Do you have any suggestions to help me with this problem? enter i ...

Display a div using Jquery when the bottom button is clicked

<div id="Slider-First Slider-Common"> <div class="Slider-Item Slider-First"> <img src="img/dog1.jpg"/> </div> <div class="Slider-Item Slider-Second"> <img src="img/cat1.jpg"/> </div> ...

Issue with hiding the overflow of the document's body

Despite finding a solution to a previous issue on Stack Overflow, I am now facing a new challenge. Adding margin:0em auto; to my page is causing it to have a 'jumpy' effect. .align-center-public { width:1000px; margin:0em auto; overf ...

Tips for transferring information between two distinct pages utilizing the jQuery POST technique

I'm dealing with two PHP files called card_process.php and payment.php. My goal is to transfer data from the cart_process page to the payment page. Here's a snippet of the code: In cart_process.php: $paynow = "<button type='submit' ...

Looking for a JavaScript tool to create XHTML content from input parameters?

Do you have a moment for a rather complex query? :) I am interested in sending data in JSON format from the server to the client. The data will then be utilized by JavaScript to create XHTML based on this information. Currently, I am in search of a libr ...

I'm having trouble getting the font to display properly on Safari (mac) similar to how it appears on

Recently, I completed a website for a client at . The owner requested the footer text to be styled similarly to the footer text on . However, upon review, it seems that the font in the lists on desiringgod appears thinner compared to the site I built. Thi ...

Unveil the socket connection using web sockets

My current setup involves a server generating a socket on port 8181. I am interested in accessing this socket from a web page viewed in Google Chrome version 14. It seems that direct access may not be feasible, as Chrome only supports Web Sockets and not s ...