Change the order of flex items when a line break occurs

Is it possible to configure the line breaks so that the middle item is dropped instead of the last one? Also, how can I prevent elements a and c from breaking onto new lines?

    .container{
      width: 80%;
      height: 31em;
      margin: auto;
      display: flex;
      justify-content: center;
      flex-wrap: wrap;
      background-color: lightgrey;
    }
    .a{ 
      flex: 1 0 20%; 
      min-width: 150px;
      height: 50%;
      background-color: pink;
    }
    .c{ 
      flex: 1 0 20%; 
      min-width: 150px;
      height: 50%;
      background-color: lightblue;
    }
    .b{ 
      flex: 1 0 60%; 
      min-width: 350px;
      height: 50%;
      background-color: lightgreen;
    }
    <div class="container">
        <div class="a">a</div>
        <div class="b">b</div>
        <div class="c">c</div>
    </div>

Answer №1

If the elements with classes .a and .c have a minimum width of 150px, and the element with class .b has a minimum width of 350px, they will all be displayed on a single row within the container as long as the container's width is at least 750px. Once the container's width falls below this threshold, the element with class .c will move to the next row.

Fortunately, there is a solution for this using CSS Container Queries.

Unfortunately, as of November 2021, support for this feature is limited, and we may need to wait a little longer before fully utilizing it (support).

That's why the code snippet provided below will only work in Chrome with the "Enable CSS Container Queries" feature flag enabled.

.container {
  width: 80%;
  height: 31em;
  margin: 20px auto;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  background-color: lightgrey;
  
  container-type: inline-size;
}

.a {
  flex: 1 0 20%;
  min-width: 150px;
  height: 50%;
  background-color: pink;
}

.c {
  flex: 1 0 20%;
  min-width: 150px;
  height: 50%;
  background-color: lightblue;
}

.b {
  flex: 1 0 60%;
  min-width: 350px;
  height: 50%;
  background-color: lightgreen;
}

@container (max-width: 750px) {
  .b {
    order: 1;
  }
}
<div class="container">
  <div class="a">a</div>
  <div class="b">b</div>
  <div class="c">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

Retrieve the id of the clicked hyperlink and then send it to JQuery

<a class = "link" href="#" id = "one"> <div class="hidden_content" id = "secret_one" style = "display: none;"> <p>This information is confidential</p> </div> <a class = "link" href="#" id = "two" style = "display: non ...

The PHP language includes a print language construct for outputting text

Having some trouble with a PHP script in my assignment related to page handling. When I submit it through another PHP page, it is showing the actual PHP code instead of executing properly, but it works fine when run standalone. I've created an HTML l ...

D3 group of legendary elements

Is there a way to group both the circle and text elements for each legend item together? I'm currently facing a challenge with the enter/exit methods. While I can create the groups (g), I'm unable to append the text and circle elements. li.sel ...

Using CSS to style multiple IDs within a class group

Can someone help me get this css code functioning properly? #inventoryGarbageSpot .id1,.id2,id3,id4{ padding:0px; padding-top: 0px; width:35px;} When I use a single id instead of multiple ids, it works fine. Appreciate your assistance, Rotem ...

Form in HTML with Automatic Multiplication Functionality in pure JavaScript

I'm struggling with integrating a simplified HTML form with JavaScript to dynamically adjust and multiply the entered amount by 100 before sending it via the GET method to a specific endpoint. Below is the HTML form: <body> <form method= ...

The functionality of jquery .serialize is not effective when used on forms that are generated dynamically

Currently, I am utilizing Java/Spring within a .jsp file. The issue arises when attempting to dynamically load a form (including form tags) from myForm.jsp into a div in myPage.jsp and making an ajax post call with the data. Upon using jQuery .serialize a ...

How can Rails resize images for display on views?

Is there a way to resize an existing image to specific dimensions without creating multiple versions? Currently, I am using Carrierwave and Rmagick to upload images to my Amazon S3 storage bucket. It generates two versions of the image: the original and a ...

Javascript code creates a blur effect on webpage bodies

On my webpage, I have multiple elements, including a "change BG" button. When this button is clicked, I want to hide all other content on the page except for the button using JavaScript. Alternatively, clicking the button could blur out all other elements ...

Automatically submit a PHP form if the input text box is left blank

I need a way to automatically refresh the data on my page when the textbox is empty. Manually submitting the form works perfectly, but I want it to happen automatically if the textbox is empty. The catch is that I don't want the form to submit while t ...

What is the best way to align this content in a specific area?

How can I center the content in each section of my webpage? Here is an image for reference: https://i.stack.imgur.com/PHEXb.png I want Section 1 and Section 2 to be 100vh with the content centered on the page. .content { height: 100vh; } <!do ...

AngularJS inputs using ng-model should be blank when in the pristine state

Check out this input element within an HTML form: <input ng-model="amount" /> Currently, the input displays as $scope.amount = 0 in the controller. I want to start with a blank input field instead. This way, users can easily input data without havi ...

Tips for correctly cloning a table row:

Currently, I am engaged with a Django project that involves incorporating a form within a table structure. <table name="mytable" id="table_purchase" role="grid"> <thead> <tr> <th class="text-center" hidden>No</th& ...

Unable to insert menu links into the container

Currently, I have a logo and image slider placed next to each other in a container. However, I am facing difficulty in adding 3 menu links horizontally under the logo. Every time I try to do so, the links end up going above the image slider, causing it to ...

The issue persists as the ng-change directive fails to function despite the presence of ng-model in AngularJS

Greetings everyone. Despite searching online for a solution to my issue, I have been unable to resolve it. Below is the HTML5 code snippet I am working with: <!DOCTYPE html> <html> <head> </head> <body ng-app="myApp ...

Using a background image in the navigation menu with CSS

Looking to create a horizontal menu with a responsive background image. Struggling with the issue of the 'ul' menu being misaligned with the background image when in full-screen mode. Attempted to use separate 'div' containers for the ...

Ways to extract information from a website using the data within a span element

I am struggling to extract the R1200 value from the "one Way drop off surcharge" section. Despite trying different methods, I have not been successful in retrieving this information. My goal is to copy the 1200 value and paste it into an Excel cell. I am f ...

Creating a wide mega dropdown menu using Bootstrap 5.2 for a full-width page

I am attempting to create a Bootstrap 5.2 Mega Menu with a full page-wide background, but I am encountering some challenges. Here is my basic navigation setup: <nav class="navbar navbar-expand"> <div class="container-fluid" ...

Utilizing Java to extract dynamically generated content from an HTML page

I have an HTML page that looks like this: <html> <head> <!-- necessary java scripts --> </head> <body> <div id="content"></div> </body> After the page renders, a lot of dynamic html content is placed within ...

ul background transparency only works with absolute positioning if using a Transparent PNG

Encountering a strange issue was the order of the day, as I found myself faced with an unordered list featuring a background image in the form of a semi-transparent PNG. To my surprise, the transparency only became visible once I designated the unordered l ...

Hidden Bootstrap 4 Layout: Utilizing 2 Columns

For my latest project, I'm designing a bootstrap webpage with a 4-column grid layout. The twist is that two of these columns will initially be hidden - one on the left and the other on the right. By clicking a toggle button on the left, the user can r ...