Is there a way I can rearrange the display order by combining elements from two different child divs?

Here is the current HTML structure:

<div class="main-parent">
    <div class="column-left">
        <div class="1">
          1
        </div>
        <div class="2">
          2
        </div>
    </div>
    <div class="column-right">
        <div class="3">
          3
        </div>
        <div class="4">
          4
        </div>
    </div>
</div>

The CSS code for this layout is as follows:

.main-parent {
  display:flex;
  flex-direction:column;
}
.1 {
  order:4;
}
.2 {
  order:3;
}
.3 {
  order:2;
}
.4 {
  order:1;
}

I am facing issues with changing the order property due to having two different children divs under the main parent with a flex display.

Is there a way to resolve this problem without removing the left and right columns?

Answer №1

Utilizing Flexbox has the potential to achieve this functionality, although it is generally not applicable when dealing with children of different parent elements.

Nonetheless, by overriding the display property of the parent elements to display:contents, it becomes possible for the children to be rearranged.

contents

These elements do not form a specific box on their own but are replaced by their pseudo-box and child boxes

MDN

Current support for this feature is quite extensive (excluding IE) - refer to CanIUse

In essence, the use of display:contents causes the "box" of the parent element to vanish, allowing the children to then function as flex-items of the grandparent.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

 ::before,
 ::after {
  box-sizing: inherit;
}

.child {
  background: lightblue;
  border: 1px solid blue;
  padding: 1em;
}

.grand {
  display: flex;
  flex-wrap: wrap;
}

.moved {
  order: 2;
  background: lightgreen;
}

.parent {
  display: contents;
}

.child {
  flex: 0 0 50%;
}
<div class="grand">
  <div class="parent">
    <div class="child moved"> Child 1.1 Moved
    </div>
    <div class="child"> Child 1.2
    </div>
  </div>
  <div class="parent">
    <div class="child"> Child 2.1
    </div>
    <div class="child"> Child 2.2
    </div>
  </div>
</div>

Answer №2

To achieve the desired layout, simply apply a display flex to both column-right and column-left elements. Here is the revised code snippet for your reference.

.main-parent {
  display:flex;
  flex-direction:column;
}
.column-left {
  order: 2;
}
.column-right {
  order: 1;
}

.column-left,
.column-right {
  display: flex;
  flex-direction: column;
}
.column-left .box-one {
  order: 2;
}
.column-left .box-two {
  order: 1;
}
.column-right .box-three {
  order: 2;
}
.column-right .box-four {
  order: 1;
}
<div class="main-parent">
    <div class="column-left">
        <div class="box-one">
          1
        </div>
        <div class="box-two">
          2
        </div>
    </div>
    <div class="column-right">
        <div class="box-three">
          3
        </div>
        <div class="box-four">
          4
        </div>
    </div>
</div>

Answer №3

I believe utilizing the flex display is a great option. By using media queries, you can easily adjust the flex-direction of your containers for mobile devices.

Check out these resources:

Here's an example:

.c {
  padding: 10px;
  margin: 10px;
  border: 1px solid black;
}

.main-parent {
    display: flex;
    flex-direction: column-reverse;
}

@media only screen and (min-width: 768px) {
    .main-parent{
        flex-direction: row;
    }
}
<div class="main-parent">
    <div class="column-left">
        <div class="c 1">
          1
        </div>
        <div class="c 2">
          2
        </div>
    </div>
    <div class="column-right">
        <div class="c 3">
          3
        </div>
        <div class="c 4">
          4
        </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

Cannot Display CSS Background Image Locally

Apologies for the simple inquiry, but I am facing an issue with displaying my background image using background-image:url() in the CSS. Strangely, it does not work with this method, but when I use content:url(); it works just fine. Interestingly, backgrou ...

Hidden bootstrap 4 custom checkbox issue

I tried to implement Bootstrap 4 custom control for a checkbox by following the documentation, but the checkbox isn't appearing. Can anyone spot what I might be doing incorrectly? <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css ...

Incorporating CSS Styles into an HTML Page

I'm encountering an issue where the CSS layout doesn't load when I try to view the document. Here's the content: <!DOCTYPE html> <html> <head> <body> <title>Waden B. Greaux Jr.</title> & ...

The text color is being influenced by the transparent background behind it

I have a box with a transparent background color. Below is the CSS and HTML code that I'm using: CSS: #box { color: black; text-align: center; margin: 50px auto; background: blue; opacity: 0.1; border-radius: 11px; box-sh ...

Eliminate the content located between two specific words in the img src URL and then render the file from the new source by utilizing ht

My img tags have parameters in the url for ajax image crop functionality. Here's an example: <img src="/resize/45-800/files/myImage.jpeg" alt="Chania"> Add /resize/45-800/ before the url to instruct the ajax script to fetch the file from the f ...

Resolving formatting challenges in R markdown with Blastula

Dealing with formatting problems while trying to include an R markdown file in an email body. As a CSS novice, I'm struggling to find a solution and it's becoming quite frustrating. The table has the following CSS styling: <!DOCTYPE html ...

Obtaining JSON data in a separate JavaScript file using PHP

I have an HTML file with the following content: // target.html <html xmlns="http://www.w3.org/1999/xhtml"> ... <script src="../../Common/js/jquery-ui-1.10.3.js"></script> <script src="../../Common/js/select.js" type="text/javascript"& ...

using angularjs to dynamically apply css styles

Below is the input I have: The HTML code is shown below: <input type="number" ng-class="{negative: amount < 0}" ng-model="amount"/> This is the corresponding CSS code: .negative { color: red; } If the amount is positive, no specif ...

Help needed with CSS menu dropdown problem

Hello everyone, I am currently working on creating a menu for the top navigation of my website. My goal is to have a list of items appear below the menu when hovering over it with the mouse. Right now, I am testing this on a local HTML file before impleme ...

Prevent the cascading of CSS styles on child list items

When constructing my menu, I have organized it using the following HTML structure: <div id=”navigation”> <ul> <li class=”level1”>Top Level Menu item <div class=”sublevel”> <ul> ...

What is the best way to conceal a panel using animation.css slide effects in GWT?

After creating a panel using GWT as shown below: VerticalPanel myPanel = new VerticalPanel(); I attempted to add animation CSS to myPanel in order to achieve sliding effects when hiding the panel like this: myPanel.addStyleName("animated slideInRight"); ...

Crop images in a canvas using a customized rectangle with the help of JQuery

I am trying to crop an image inside a Canvas element using a selection rectangle. My project utilizes jQuery and I am in search of a plugin that can help me implement this custom selection rectangle on the canvas itself, not just on images. Are there any ...

How can you tell if a contenteditable div is currently in focus?

Essentially, my setup consists of: HTML: <div class="div1"> <div class="as-text-box" contenteditable="true"></div> </div> CSS: .div1{ position:absolute; height:50px; width:200px; background:white; border:1px solid #c ...

What are the essential Bootstrap CSS and JavaScript files that need to be connected to our HTML document?

I recently downloaded the most recent version of Bootstrap, which is version 4. Upon extraction, I noticed that it includes two folders: one for CSS and the other for JS. The CSS folder consists of approximately 12 CSS files, while the JS folder contains ...

The CSS Grid container fails to resize based on the content inside

I am facing an issue with the grid element inside a parent container with display: grid;. The parent container has a maximum size and allows scrolling for the grid if necessary. The problem is that the grid element does not expand to fit its children. Whe ...

Choose from the dropdown menu, not from the text

I'm having trouble aligning a select element in Bootstrap without centering the text inside it. Can anyone provide a solution for this issue? I've attempted to center the containing div and then set the text-align property to left for the select ...

CSS - Negative Margins Leading to Horizontal Scrolling Issue in Mobile Viewport

I’m working on some HTML and CSS code that looks like this: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content= ...

Iterate through an Array of Objects and exhibit a single object property in HTML one at a time by utilizing Javascript

I am working with an array of objects that contain two properties: "quote" and "author". I have a container div where I want the quotes to be displayed one by one at an interval of every 2 seconds. Currently, it is showing one letter at a time instead of d ...

How can you stop text in a textarea from spilling over into the padding?

When I add padding to a textarea element and type more than four lines of content, the content overflows into the padding. Is there a way to prevent this? I have looked at the suggestions in this thread and this one (specifically for google-chrome), but t ...

Expanding Issue with Bootstrap Navigation

Hello! I am experiencing an issue with my navbar. The menu collapses, but it does not expand. I have used an example from Bootstrap and made some tweaks, but for some reason, it still doesn't expand properly. Below is the code that I have been workin ...