Numerous floating elements are being displaced by the left-aligned ones, causing disarray in the layout

Here is the code snippet I'm having trouble with:

html:

<div class="left">left</div>
<div class="left">left</div>
<div class="left">left</div>
<div class="right">right</div>
<div class="right">right</div>
<div class="right">right</div>

css:

.left{
  float: left;
  clear: left;
  border: 1px solid blue;
}
.right{
  float: right;
  clear: right;
  border: 1px solid red;
}

jsfiddle: https://jsfiddle.net/839okvsb/4/

The left divs are positioned correctly, but the right ones start after the second div instead of aligning on top.

What could be causing this issue, and how can it be fixed? This problem has me really puzzled.

Note: Combining all left divs into one large div is not an option because they need to be separate (depending on the width required).

Update: Even mixing them up as shown here: https://jsfiddle.net/839okvsb/5/ does not resolve the issue.

Additional Update: Packing them all into a single div is not feasible as they need to have the flexibility demonstrated here: https://jsfiddle.net/839okvsb/8/ when necessary.

Answer №1

To achieve a 2 "column" layout using floats, it's important to structure your markup correctly. Mixing float left/right without proper clearing can result in unexpected layouts. Here are some examples to help you understand how floats work:

.left{
  float: left;
  clear: left;
  border: 1px solid blue;
  width: 48%;
}
.right{
  float: right;
  clear: right;
  width: 48%;
  border: 1px solid red;
}
<div class="left">left 1</div>
<div class="right">right 1</div>
<div class="left">left 2</div>
<div class="right">right 2</div>
<div class="left">left 3</div>
<div class="right">right 3</div>

Another approach is to wrap your floats within container elements:

.left{
  float: left;
  clear: left;
  width: 48%;
}
.right{
  float: right;
  clear: right;
  width: 48%;
}
.left .left{
  border: 1px solid blue;
  width: 100%;
}
.right .right{
  width: 100%;
  border: 1px solid red;
}
<div class="left">
  <div class="left">left 1</div>
  <div class="left">left 2</div>
  <div class="left">left 3</div>
</div>

<div class="right">
  <div class="right">right 1</div>
  <div class="right">right 2</div>
  <div class="right">right 3</div>
</div>

In cases where the columns don't always fit the traditional left and right alignment, consider floating all elements to the left with specific styling:

.left,
.right {
  float: left;
  border: 1px solid blue;
  box-sizing: border-box;
  width: 48%;
  margin-left: 4%;
}
.left:nth-child(1) {
  width: 100%;
  margin-left: 0;
}
.right {
  margin-left: 0;
}
.right{
  border: 1px solid red;
}
<div class="left">left 1</div>
<div class="right">right 1</div>
<div class="left">left 2</div>
<div class="right">right 2</div>
<div class="left">left 3</div>
<div class="right">right 3</div>

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

Unable to add padding to <b> tag

Can anyone explain why the padding-top property is not taking effect for the <b> tag? Check out this link <b>hello world</b>​ b { padding-top: 100px; } ​ ...

A method for utilizing wildcards in conjunction with the .load function

Currently, I am utilizing jquery, jquery mobile, js, html, and css within Phonegap to develop my android/ios application. However, my understanding of js & jquery is somewhat limited. In my index.html file, I use .load to load other html files into s ...

Is anyone else seeing a mysterious gray outline surrounding the image?

When visiting my website, I encountered an issue with the main menu. Specifically, when hovering over 'Populaire boeken', the first 2 menu items display properly with an image on mouseover. However, there seems to be a pesky grey border surroundi ...

Unable to show the table row color using Angular in Internet Explorer

I've customized a table by changing the row color based on a property value, and I'm using ng-repeat to display the rows. Here's my table: <table class="tab table span6 tabhover" style="margin:0;" > <thead> ...

Modify the background of a div according to the size of the screen. Implement this functionality using JavaScript and

After reviewing a specific post at , I find myself unsure of how to target a particular class for reference. I am certain that I want to implement document.onload = checkWidth(); window.onresize = checkWidth(); However, I am uncertain about the procedur ...

Limit the maximum column gap on the grid and stop it from expanding further

I'm currently facing an issue with some columns/rows on my website that are keeping content locked in the top left corner, reminiscent of early 00's web design. The site was originally designed for smaller screens like 1024x764, so when viewed on ...

The background-size property in CSS does not seem to properly display on iPhones

Hello there! I'm facing an issue with my CSS code when it comes to displaying the image on Safari browser. It works perfectly fine on other browsers, but for some reason on Safari, the image doesn't show up correctly. I tried using a media query ...

Leveraging this jQuery code across multiple div elements

As a newcomer to jQuery, I am currently experimenting with a test script. My goal is to create an effect where when an image is clicked, a div slides down displaying more information similar to the new google images effect. The issue with the current scri ...

Thumbnail vanishes upon being selected

To access my template site currently, please click here. Currently, the first thumbnail on my website is set up to display a gallery of images in FancyBox once clicked, similar to a question asked on StackOverflow that you can view here. Below is a snipp ...

Why might a responsive website automatically switch to mobile view?

My website has a responsive grid system that utilizes media queries with breakpoints. However, I am encountering an issue on IE8 where the "mobile version" of the site is being displayed as the default view even when the screen size is set to what should b ...

Create an interactive zone on the map

Is there a method to click only within the triangle region without causing any impact on the surrounding areas? I attempted to generate a triangular div using `clip-path`, but unfortunately, it is not compatible with Internet Explorer. Despite thorough r ...

Is there a way to connect an HTML page without using a hyperlink?

Is there a way to directly display linked HTML pages on my webpage instead of just creating a link? I'm looking for suggestions on how to arrange this. Thank you! ...

Creating a counter for a list using CSS

I am attempting to utilize the counter increment feature in CSS for my ordered list, but it doesn't seem to be functioning properly. Here is the desired format I want to achieve: 1. Acknowledgements 1.1 blah, blah .... 1.2 blah, blah .... 1 ...

I'm struggling to make a div stretch across the full width of the screen

Despite my efforts, I can't seem to eliminate the noticeable margin between the div and the edges of the screen. <!DOCTYPE html> <html> <head> <title>Test2</title> <link rel="stylesheet" href="https://maxc ...

Spacing between navigation links appears to be too wide on WordPress

Check out our website: I am facing an issue with aligning all my links in a single line on my menu. Currently, there is too much spacing between each link causing them to wrap onto the next line. Here is what I have so far. https://i.sstatic.net/YaHbJ.jp ...

Decrease the interactive area for an image

I have successfully designed a left navigation bar using buttons. Currently, I am facing two main issues that I need assistance with. Firstly, I am trying to limit the hyperlink area to just the background image. Secondly, the elements that are overlaying ...

Dropdown menu in Bootstrap gets cropped behind a scrollable container

I am encountering a problem with my Bootstrap Drop Down. The dropdown is located within a scrollable div with a fixed height. Whenever I click on the dropdown, the menu appears behind the scroll content. However, a normal select works perfectly fine. htt ...

What is the best way to position an <a> tag in the top right corner of a card using Bootstrap?

I own a card that features an image in the background. <div class="col-12 col-sm-8 col-md-6 col-lg-4 mt-4"> <div class="card"> <img class="card-img" style=" filter: brightness(75%);" src=& ...

Conceal Content from Onsite Search

Is it possible to conceal text from on-page browser searches (e.g. Ctrl+F) in a dependable manner while keeping it visible? I thought that adding aria-hidden="true" would address the issue, but the text remains searchable by browsers. ...

Customizing Material-UI elements in React using CSS styling

My goal is to style all the <Chip> elements within a <Grid> that has the class .table-header, but they are not changing their style (I have a <p> that should be colored in red and it works). Why does the styling work for the <p> ele ...