I have difficulty getting divs to float the way I want them to

I have a total of 5 divs inside one main div, and I want to arrange them so that 2 are floating on the left and the remaining 3 are floating on the right:

.container {
  height: 400px;
  border: 1px solid black;
}
.first, .second, .third, .fourth, .fifth {
  width: 50%;
}

.first {
  height: 300px;
  background-color: red;
  float: left;
}

.second {
  height: 100px;
  background-color: blue;
  float: left;
}

.third {
  height: 100px;
  background-color: green;
  float: right;
}

.fourth {
  height: 200px;
  background-color: yellow;
  float: right;
}

.fifth {
  height: 100px;
  background-color: aquamarine;
  float: right;
}
<div class="container">
    <div class="first"> FIRST </div>
    <div class="second"> SECOND </div>
    <div class="third"> THIRD </div>
    <div class="fourth"> FOURTH</div>
    <div class="fifth"> FIFTH </div>
</div>

I want them to be arranged as follows:

FIRST and SECOND on the left
THIRD, FOURTH, and FIFTH on the right.
However, they are currently arranged like this:
FIRST and FIFTH on the left
SECOND, THIRD, and FOURTH on the right.

Can anyone help me fix this issue? Here is the code: https://jsfiddle.net/82bkdbpn/

Answer №1

To achieve the desired layout with a fixed height on the container element, you can utilize Flexbox by setting flex-direction: column.

.container {
  height: 400px;
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}
.first, .second, .third, .fourth, .fifth {
  width: 50%;
  height: 100px;
}
.first {
  height: 300px;
  background-color: red;
}
.fourth {
  height: 200px;
  background-color: yellow;
}
.second {background-color: blue;}
.third {background-color: green;}
.fifth {background-color: aquamarine;}
<div class="container">
  <div class="first"> FIRST </div>
  <div class="second"> SECOND </div>
  <div class="third"> THIRD </div>
  <div class="fourth"> FOURTH</div>
  <div class="fifth"> FIFTH </div>
</div>

Answer №2

To create a layout with two columns, you have the option of placing the divs in Columns and floating them, or utilizing flexbox. A highly informative response from Nenad Vracar can be found on this topic.

Below is an illustration featuring two column wrapper div elements:

.container {
  height: 400px;
  border: 1px solid black;
}

.col {
  width: 50%;
  float: left;
}

.first {
  height: 300px;
  background-color: red;
}

.second {
  height: 100px;
  background-color: blue;
}

.third {
  height: 100px;
  background-color: green;
}

.fourth {
  height: 200px;
  background-color: yellow;
}

.fifth {
  height: 100px;
  background-color: aquamarine;
}
<div class="container">
  <div class="col">
    <div class="first"> FIRST </div>
    <div class="second"> SECOND </div>
  </div>
  <div class="col">
    <div class="third"> THIRD </div>
    <div class="fourth"> FOURTH</div>
    <div class="fifth"> FIFTH </div>
  </div>
</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

Convert form data into JSON format while maintaining numerical values

One question that is frequently asked, but I have yet to find a solution for, is how to create a JSON body from a form in which the quotes are placed around the property names rather than the values. The desired JSON body should look like this: { "salary1 ...

What is the best way to choose two <li> elements with multiple classes in my WordPress navigation menu?

I am looking for a JavaScript function that will add the class "current_highlight" when an element with the class "activo2" also has the class "active". Here is my HTML code: <div class="navbar-header"> <button type="button " class="navbar-to ...

Scaling SVG Graphics in Real-Time

I am currently developing a website where users can interact with an SVG image using textboxes. One of my goals is to make sure the SVG image scales to fit the container div. For example, if the SVG was the same height as the container but only 10 pixels ...

Guide to attaching a page content button to a fixed header

I am currently developing a webpage with a sticky header, and I want to include an animated button that moves onto the header when scrolled past. You can check out my code snippet here: https://jsfiddle.net/ykL50pjf/ The specific ID for the button is #t ...

What are some ways to implement webkit-line-clamp on a website?

I'm trying to shorten my paragraph using the following CSS code: white-space: nowrap; overflow: hidden; text-overflow: ellipsis; However, the issue is that it condenses everything into one line. I actually want the text to display on 3 lines before t ...

Designing a collection of inline divs that automatically stretch to fit the parent container, while also wrapping to a new line if the minimum width is reached

Welcome to my first post on this platform! I love a good HTML challenge, but I'm stumped on this particular issue and could really use some help. Thank you in advance to anyone who can assist. What I am trying to achieve is to create a series of inl ...

Troubleshooting: Resolving the error message "SyntaxError: Unexpected token '<'"

I am currently facing an issue with my code that is supposed to use LAT&LONG data saved in a txt file and display it as a Google Map in HTML. However, the PHP function to import the txt file is not working at all. Any suggestions on what might be causi ...

How to align buttons in the center of a Bootstrap grid column using Angular's *ngFor directive

I have been trying to center some buttons using the bootstrap 4 grid system within a column with a green border. Despite using justify-content-center, I have not been successful so far. <div class="row" style="border:1px solid red;"& ...

CSS file from outside does not impact the appearance of buttons

Take for instance this button: <input class="btn btn-primary btn-block btn-lg register_btn rounded-0 submit-button" type="submit" value="Submit"> and the CSS code within a custom custom.css file: .submit-button { ...

Toggling javascript functionality based on media queries

On one of my slides, I want to display images that are specific to different devices like iPhone, iPad, and desktop. I am looking for a way to filter these images based on the device. Using display:none won't work as it's JavaScript, but here is ...

What is the best way to align navigation arrows vertically?

I am working on a modal album that displays images of various sizes. My issue is with styling the navigation arrows to appear in the center of the page, regardless of the image size. I have included my code below: <div class="row img-box"> <d ...

Confirm Submission Issue in HTML Form

During my testing of the blacklist confirmation dialog, I encountered an issue where clicking the OK button did not submit the form as expected. Instead, it seemed to be stuck in a loop where clicking the button had no effect and the dialog remained on scr ...

the division does not span the entire width

I am faced with a dilemma involving two div elements structured as follows: <div id="left"><p>.....</p><br/> <p>.....</p> </div> <div id="right"><img ..../></div> Accompanied by CSS styling: #l ...

The object of type 'NoneType' does not have the 'next_element' attribute

Here we have an HTML snippet of a website with a single itemprop="brand": <dd itemprop="brand" class="attributeValue-2574930263"> <a class="link-3970392289 link__default-1151936189 attributeLink-2414863004&qu ...

Utilize ng-bootstrap in an Angular CLI project by integrating it with npm commands

I've been attempting to set up a project using Angular CLI with ng-bootstrap, but I'm having trouble getting the style to work properly. Here are the exact steps I followed (as outlined in the get-started page): Create a new project using `ng n ...

Typescript Style Tabs: A Stylish Approach

I've been struggling to customize the tabs I created using this code, but nothing seems to be working as expected. This article was suggested to me recently, however, when I tried implementing it, I encountered errors. I also looked at this stackoverf ...

Eliminate the grey border located above the arrow in the Bootstrap tooltip

Is there a reason why a thin border is showing up on all of my Bootstrap tooltips? I have looked extensively for answers but have not been able to find anyone else having the same issue. This border appears consistently in all of our MVC projects, and we h ...

Retrieve all div elements by their data attribute until reaching a certain condition

I am working on an innovative jquery accordion menu using divs instead of the typical li tags. Each div contains a unique data attribute (data-level) with values ranging from 1 to 4, and so on... To achieve the desired functionality, my "toggle" function ...

Designing materials involves organizing 5 items in a section, as dividing 12 by 5 results in an unattractive

Incorporating the Material Design Light library into my webpage, I have a section with 5 items that I want to evenly occupy the space: <section class="section--center mdl-grid mdl-grid--no-spacing mdl-shadow--2dp social-section"> <a href="http: ...

What is the best way to utilize toggle("slide") in order to reveal a message letter by letter?

I am currently experimenting with the `.toggle("slide") function to create an effect where a piece of text appears as though each letter is sliding in. However, I've noticed that instead of a smooth slide, it looks more like the text is flying in abru ...