What is the best way to rearrange three divs into two columns?

I am trying to rearrange the order of three divs in my markup layout.

The desired layout is to display div one first, followed by three underneath it in the first column. Then, in the second column, I want to only show div two. My current CSS configuration is not able to achieve this. How can I reposition div two into the second column?

.main {
  width: 400px;
}
.box {
  width: 50%;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}
.box>* {
    flex: 1 1 100%;
    border: 1px solid red;
}
.box>div:first-child{
    flex: 1 1 70%;
    width: 70% !important;
}

.box>div:nth-child(2) {
    flex: 1 1 30%;
    order: 3;
    width: 30%;
    float: right;
}

.box>div:nth-child(3) {
    flex: 1 1 70%;
    width: 70%;
}
<div class="main">
  <div class="box">
    <div>One</div>
    <div>Two
      <p>paragraph</p>
    </div>
    <div>Three</div>
  </div>
</div>

Answer №1

Since your widths are already specified in the CSS file,
it might be simpler to not use the flex system in this scenario:

.main {
  width: 400px;
}

.box {
  position: relative;
  width: 50%;
}

.box > * {
  box-sizing: border-box;
  border: 1px solid red;
  width: 70%;
}

.box>div:nth-child(2) {
  position: absolute;
  top: 0;
  right: 0;
  width: 30%;
}
<div class="main">
  <div class="box">
    <div>One</div>
    <div>Two
      <p>paragraph</p>
    </div>
    <div>Three</div>
  </div>
</div>

I hope this information proves useful.

Answer №2

Utilizing dynamic height can present a challenge when attempting to wrap content into multiple columns. The browser struggles to determine the appropriate placement for wrapping the second child without clear direction on the flex container's height. To achieve the desired result of content in two columns, setting a specific height and adjusting the flex declarations to flex: 1 1 auto and flex: 1 1 100% will prompt the necessary column wrapping.

.main {
  width: 400px;
}
.box {
  width: 50%;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  
  /* Adding a defined height ensures the
     content wraps into a second column.
     Customize the height to suit your needs. */
  height: 100px;
}
.box>* {
    flex: 1 1 auto;
    width: 70%;
    box-sizing: border-box;
    border: 1px solid red;
}
.box>div:nth-child(2) {
    flex-basis: 100%;
    width: 30%;
    order: 3;
}
<div class="main">
  <div class="box">
    <div>One</div>
    <div>Two
      <p>paragraph</p>
    </div>
    <div>Three</div>
  </div>
</div>

Answer №3

Previously, I utilized the table method to achieve a certain layout design. Although grid layout is another option, I opted for tables due to potential compatibility issues with Internet Explorer and grid CSS.

.main {
      width: 400px;
    }
    .box {
      width: 50%;
      display: flex;
      flex-direction: column;
      flex-wrap: wrap;
    }
    .box td {
        flex: 1 1 100%;
        border: 1px solid red;
    }
 
 <div class="main">
      <div class="box">
      <table>
      <tr>
        <td>One</td>
        <td rowspan=2>Two
          <p>paragraph</p>
        </td>
        
        </tr>
        <tr>
        <td>Three</td>
        </tr>
        </table>
      </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

PHP script to populate a table with images based on certain conditions

I have a table in HTML that displays results from a SQL database on the right hand side. The code snippet for populating each cell is as shown below: <tr> <td>House</td> <td><?php echo $row_buyerdetails['tod_hous ...

Spontaneous visual paired with text upon refreshing

I am new to Java script and I am working on creating a web page that displays two random numbers every time it is refreshed. Depending on these numbers, specific text should be shown. These numbers are represented by images ranging from 0 to 5. Currently ...

template for login page with google closure UI design

Just starting out with google closure... are there any templates provided by Google for creating a basic user registration/login page? Most of what I've found so far seems to focus on UI manipulation and DOM creation. ...

Is there a way to customize Firefox's default styles similar to Chrome's?

Can you change the default styles of Firefox to match those of Chrome? ...

What is the most effective method for retrieving a PHP return value using Ajax?

As I work on creating a validation form in Ajax and PHP, I find myself unsure of how to fetch values from PHP. Can anyone guide me through this process? For instance: The validation form exists in index.php while the page containing the function is check ...

What is the process for transferring input field values to PHP?

I am working on setting up a database and login form, and I need to create a PHP script to validate the login credentials. What are the different types of data access and how can they be utilized? Specifically, how do I retrieve user input from elements ...

Deactivate a specific button within a row on a Primeng table by clicking on it

I'm trying to modify my primeng table so that each row has a button, and when I click on a button, only that specific button should be disabled. However, in my current code, all buttons are getting disabled with a single click. Can someone please help ...

Interact with the :before pseudo element when hovering over an element

Is there a way to manipulate the :before pseudo element when hovering over the main element? My goal is for the pseudo element to change its height when I hover over the main element. This is how my code looks: HTML <div class="object">& ...

How can you add draggable functionality to a Bootstrap dropdown menu?

My custom bootstrap dropdown design <div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> Dropdown <span cla ...

Button from Material-UI vanishes upon being clicked

I encountered an issue with a button that disappears when clicked on. Additionally, clicking the button once does not trigger any actions associated with it. In order to execute the button actions, I have to click the area where the button was located afte ...

Populating a Dropdown Menu with JSON Data using JavaScript

I am facing an issue with populating my dropdown menu using data from my JavaScript file. Whenever I click the button, three objects are added to the dropdown, but I only want them to show individually upon clicking. Additionally, I am getting [object obje ...

Javascript event fails to trigger following completion of Ajax request

I'm encountering an issue with my form (input, textarea, and submit button) and a list of items. I've written a script that should: Display the text content of a list item in the textarea when clicked Submit the form via ajax without refreshing ...

Loop through the elements of a class in JavaScript and choose all except for the one that was

Imagine having 5 div elements, each with a similar onclick-function that hides the other divs when clicked. HTML: <div id="1" class="divs" onclick="hide()"></div> <div id="2" class="divs" onclick="hide()"></div> <div id="3" cla ...

This JavaScript code is configured to only display content on half of the page

Has anyone experienced issues printing a full element (body) using the jquery plugin https://github.com/jasonday/printThis? I have only been able to print half of the element vertically. I also tried using raw javascript with window.print(); but encountere ...

Problem with overflow scroll in Internet Explorer 11

I'm working with two ID selectors, 'top' and 'bottom'. The 'top' div has a "position: relative" setting while the 'bottom' div has a fixed position and overlaps the 'top' div. I have also set an "overf ...

JQuery table sorter is unable to effectively sort tables with date range strings

I am facing an issue with sorting a column in my table that contains text with varying dates. The text format is as follows: Requested Statement 7/1/2014 - 9/16/2014 When using tablesorter, the sorting does not work properly for this column. You can see ...

Changing colors when hovering over different elements

I am struggling to create a hover color change effect that applies to all elements (icon, text, and button) from top to bottom. Currently, the hover effect only changes the color of either the text and icon or the button individually. Below is the code sn ...

In order to properly set up Require JS, you will need to configure the JS settings

Is there a way to specify the path from any property inside the require JS config section? We are trying to pass a property inside the config like so: The issue at hand: var SomePathFromPropertyFile = "CDN lib path"; require.config({ waitSeconds: 500 ...

Chose to conceal all child elements, but is unable to reveal all elements when clicked

Currently, I am in the process of creating a jQuery accordion by following the tutorial provided on css-tricks at this link. After making some modifications to the code, I was able to hide all the elements within the parent element. However, upon clicking, ...

Revamping the Jquery Waypoint

Greetings, I am currently delving into the world of Jquery waypoints and facing a bit of challenge getting it to work. My goal is to have an "alert" pop up once an element becomes visible on the screen, but for some reason, the alert doesn't show up w ...