Switching the order of elements in a column using Flexbox

Here is the grid layout on PC: https://i.sstatic.net/4HRvd.png And here is the grid layout on mobile devices:

https://i.sstatic.net/URBjb.png

I am looking to rearrange the order and layout of the grid. I have tried using flexbox, but I need to group B and C on PC and make A equal in height.

Key requirements are:

  • On PC, A should have the same height as the combined height of B and C
  • On mobile, the order should be B, A, C

I am aware that I could duplicate the B block and use show/hide, but this is not an option for my project.

Answer №1

If I've grasped your needs accurately, here's the solution.

To enhance visibility, I opted for a hover state over a media query for the alteration.

The key is to remove A from the flex layout in the desktop version.

.container {
  border: solid 1px black;
  margin: 10px;
  display: flex;
  flex-direction: column;
  position: relative;
  width: 400px;
}

.container > div {
  width: 200px;
  font-size: 50px;
}

.a {
  background-color: tomato;
  height: 100%;
  position: absolute;
}

.b {
  background-color: lightgreen;
  height: 140px;
}

.c {
  background-color: lightblue;
  height: 200px;
  order: 3;
}

.container div {
  width: 50%;
}

.container .a {
  position: absolute;
  left: 0px;
  top: 0px;
  width: 50%;
  height: 100%;
}

.container div {
  align-self: flex-end;
}


.container:hover .a {
  position: static;
  height: 130px;
  order: 2;
}

.container:hover div {
  width: 100%;
}
<div class="container">
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
</div>

Answer №2

Check out this demonstration to understand how the height of the outer wrapper affects the layout. In this scenario, the height of the wrapper is set to the viewport height:

body {
  margin: 0;
}
* {
  box-sizing: border-box;
}
.wrapper {
  display: flex;
  flex-wrap: wrap;
  height: 100vh;
}
.wrapper > div {
  border: 1px solid;
  width: 50%;
  height: 50%;
}
.wrapper > div:first-child {
  height: 100%;
}
.wrapper > div:last-child {
  margin-left: auto;
  position: relative;
  top: -50%;
}
@media only screen and (max-width: 650px) {
  .wrapper {
    display: flex;
    flex-direction: column;
    flex-wrap: initial;
  }
  .wrapper > div {
    width: 100%;
  }
  .wrapper > div:first-child {
    order: 2;
  }
  .wrapper > div:last-child {
    order: 3;
    top: 0;
  }
}
<div class="wrapper">
  <div>A</div>
  <div>B</div>
  <div>C</div>
</div>

Here is an example when you wrap B and C in a container. Note that in mobile view, you can only achieve A-B-C, not B-A-C. See the snippet below:

body {
  margin: 0;
}
* {
  box-sizing: border-box;
}
.wrapper {
  display: flex;
}
.wrapper > div:first-child {
  width: 50%;
  border: 1px solid;
}
.wrapper > .inner-wrap {
  display: flex;
  flex-direction: column;
  width: 50%;
}
.wrapper > .inner-wrap > div {
  width: 100%;
  border: 1px solid;
}
@media only screen and (max-width: 650px) {
  .wrapper {
    display: flex;
    flex-direction: column;
  }
  .wrapper > div:first-child {
    width: 100%;
  }
  .wrapper > .inner-wrap {
    width: 100%;
  }
}
<div class="wrapper">
  <div class="content">
    <p>A</p>
    Lorem ipsum...
  </div>
  <div class="inner-wrap">
    <div class="content">
      <p>B</p>
      Lorem ipsum...
    </div>
    <div class="content">
      <p>C</p>
      Lorem ipsum...
    </div>
  </div>
</div>

Summary:

If you're unsure about the height of the flexbox, achieving the intended order in both mobile and PC layouts might be challenging.


2022 Update

Utilizing CSS grids offers a solution - a two-column layout to organize the grid items. By defining grid-row (or grid-column), you can switch the order of items. Check out the example below:

body {
  margin: 0;
}
.wrapper {
  display: grid; /* grid container */
  grid-template-columns: 1fr 1fr; /* 2 column layout */
  grid-template-rows: auto 1fr; /* 2 rows */
  height: 100vh;
}
.wrapper > div {
  border: 1px solid;
  /* flexbox for centering */
  display: flex;
  justify-content: center;
  align-items: center;
}
.wrapper > div:first-child {
  grid-row: span 2; /* span two rows */
}
@media only screen and (max-width: 800px) {
  .wrapper {
    grid-template-columns: 1fr; /* one column */
    grid-template-rows: auto; /* reset row definiton */
  }
  .wrapper > div:first-child {
    grid-row: 2; /* place in second row */
  }
}
/* styles for presentation */
.wrapper div:first-child {
  background: #67c36e;
  font-size: 5em;
}
.wrapper div:nth-child(2) {
  background: #ec897c;
  font-size: 2em;
}
.wrapper div:last-child {
  background: #7cd0ec;
  font-size: 5em;
}
<div class="wrapper">
  <div>A</div>
  <div>B</div>
  <div>C</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

Web pages that dynamically update without the need for reloading

Recently, I stumbled upon slack.com and was immediately captivated by its user interface. For those unfamiliar with it, navigating the site is quite simple: There's a sidebar on the left and a main content area on the right. When you click on an item ...

Click to make the arrow come to life through animation!

I am brand new to coding and this is my inaugural code snippet: .container_menu { position: relative; top: 0px; left: 0px; width: 25px; height: 25px; } .container_menu .line-1 { background-color: black; width: 59%; height: 2px; positio ...

How come Font-face isn't functioning properly on Internet Explorer 11?

I have designed a new website at , however, I am facing issues with my CSS on Internet Explorer 11 (version 11.608.15063.0). The font-face and dropdown menu are not displaying correctly. Can anyone assist me with this problem? ...

Tips for modifying style.display using JavaScript

On my website, the menu is structured like this: <nav id="menu"> <label for="tm" id="toggle-menu">Menu <span class="drop-icon">▾</span></label> <input type="checkbo ...

integrating the WordPress header into the WHMCS client portal

While working on a project, I am facing the challenge of aligning the WHMCS client area with my WordPress site. Progress has been made, but I have hit a roadblock. My goal is to integrate the header of my WordPress site into WHMCS. I initially attempted t ...

Styles coded in CSS are not reflecting on the displayed webpage, despite being visible in the Integrated

Custom Styling for ASP Membership Login in Visual Studio 2008 I am experimenting with customizing the ASP:Login control by converting it into a template. Here is the updated markup: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx. ...

Guide on Ensuring a Fixed Div Covers the Entire Width of the Screen

Hello everyone, I need some help with formatting my social media widgets on my website. I want them to span the entire width of the screen, align to the right, and remain fixed at the top of the page. You can check out the site totempole.ca for reference. ...

Center a single div vertically within a parent div by utilizing a fluid layout

Attempting to align text elements within a responsive layout presents challenges due to the fluid nature of <div> sizes. I recently discovered a fantastic method for horizontally and vertically centering a child <div> inside its parent <div ...

I'm attempting to create a text toggle button for displaying more or less content

I am currently working on implementing a show more/show less button feature. However, the current version is not very effective as I only used slicing to hide content when the state is false and display it all when true. Now, my goal is to only show the ...

The background image fails to show up on the mobile device display

Currently, I am working on an app with Ionic, but unfortunately, the background image is not showing despite all my efforts. I have gone through various solutions provided in previous questions, but none of them seem to work for me. Here is the CSS I am u ...

Chrome DevTools automatically translates all HEX color codes into their corresponding RGB values

Chrome DevTools now automatically converts all HEX colors to RGB values, even if the color is set in CSS or using DevTools. Is there a way to prevent this conversion? I know about the method of holding down Shift + clicking the color icon to convert color ...

Increase scrolling speed? - Background abruptly moves after scroll

I'm facing a minor issue. I want to create a parallax background effect similar to what can be seen on nikebetterworld.com. In my initial attempt, I managed to achieve some functionality, but I believe it can be improved. As I scroll, the background p ...

Tips for implementing a hover transition effect in the navigation menu

I have a specific query regarding a particular issue that I am unsure how to address but would like to implement on my website. 1) My requirement is that when the parent li element has a child, I want the navigation to remain in a hovered state with the s ...

Mastering the technique of showcasing landscape using CSS3

I'm working on an HTML and CSS3 report with multiple columns. I am trying to print it in landscape orientation using HTML and CSS3. I attempted rotating the body and table, but only half of the table is visible while the other half is cut off or hidde ...

Implementing function invocation upon scroll bar click

I have a Div element with a scroll bar. When a user clicks the scroll bar, I want to call a function based on that click event. How can I achieve this? On the client side, I am using jQuery and on the server side, I am using PHP. I am familiar with makin ...

Is Ursina the right choice for web development?

Looking for a method to compile Ursina for HTML5 using WebAssembly or another technology? I am seeking to make my Ursina Game compatible with Linux & Mac (and potentially Android/iOS with webview), but the current cross-platform compilation options for Py ...

Styling your Kendo grid in ASP.NET MVC with custom CSS styling

I am facing a challenge with displaying two kendo grids side by side on a single view page. To modify the CSS style of a kendo grid using ASP.NET, I typically use: .HtmlAttributes(new { style="width:50%"}) However, when attempting to change more than one ...

Shrinking the image within a card row in Laravel/Bootstrap 5

My issue involves creating a row of Bootstrap cards using Laravel. When I add the "row" class, the images in the cards shrink. However, if I remove the "row" class, the images display perfectly. How can I fix this problem? <h1>Vehicles</ ...

Obtain the beginning and ending positions of a substring within a larger string

As a beginner in the world of AngularJS and web development, I am faced with a challenge. I have a specific string that is currently selected on a tab, like so: var getSelectedText = function() { var text = ""; if (typeof window.getSelection !== "un ...

Utilizing the data sent to a modal to enhance the functionality of the code by combining it with the src attribute of an <embed> element

I am trying to pass a dynamic string of data to a Bootstrap modal using the data-val attribute in an HTML table. <td <button type="button" class="btn btn-success btn-xs" data-val="<?php echo $row['order_id']; ?&g ...