Reordering floated and non-floated elements using flexbox styling

I need to rearrange the order of floated and non-floated elements, with floating changes based on media queries.

Currently, the page is displaying Block A followed by Blocks B and C, but it should be Block B, Block C, and then Block A.

Any solution using flexbox would be much appreciated.

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

section.one {
  box-sizing: border-box;
  width: 48%;
  margin-right: 2%;
  column-count: 1;
  float: left;
}

section.two {
  box-sizing: border-box;
  width: 48%;
  margin-left: 2%;
  column-count: 1;
  float: left;
}

.block {
  background-color: red;
  width: 100%;
  height: 200px;
  clear: both;
  padding: 1em 0;
}

.block h2 {
  text-align: center;
}

@media (max-width:500px) {
  section.one,
  section.two {
    width: 100%;
    margin-right: 0;
    margin-left: 0;
  }
}
<article>

  <div class="content">
    <h1 class="heading">Heading</h1>
    <section class="one">
      <h2>Block B</h2>
      <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
        aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo doloreslita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
    </section>
    <section class="two">
      <h2>Block C</h2>
      <p>At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
        erat, sed diam voluptua. At vero eos et accusam et justo duo dolores rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
    </section>
    <div class="block">
      <h2>
        Block A
      </h2>
    </div>
  </div>
</article>

View the demonstration here: https://jsfiddle.net/mqfcz46z/4/

Answer №1

Using flexbox eliminates the need for floats entirely. In fact, floats do not function within a flex container.

You have the ability to utilize flex properties to size, align, and rearrange your items as needed.

.content {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

section.one {
  flex: 0 0 48%;
}

section.two {
  flex: 0 0 48%;
}

.block {
  flex: 0 0 100%;
  height: 200px;
  background-color: red;
}

.block h2 {
  text-align: center;
}

@media (max-width:500px) {
  section.one { order: 2; }
  section.two { order: 1; }
  .block      { order: 0; }
}
<article>
  <h1 class="heading">Heading</h1>
  <div class="content">
    <section class="one">
      <h2>Block B</h2>
      <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
        aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo doloreslita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
    </section>
    <section class="two">
      <h2>Block C</h2>
      <p>At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
        erat, sed diam voluptua. At vero eos et accusam et justo duo dolores rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
    </section>
    <div class="block">
      <h2>Block A</h2>
    </div>
  </div>
</article>

Check out the jsFiddle here

To delve deeper into the order property, visit: Is there a “previous sibling” CSS selector?

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

How to line up Font Awesome icons both horizontally and vertically in Bootstrap 4

I am currently facing a challenge with aligning a Font Awesome icon horizontally and vertically within Bootstrap 4. I am aiming to achieve this using only the built-in CSS provided by Bootstrap 4, without the need for any custom CSS. While I have been suc ...

What is the best method to organize HTML tables in Knockout by utilizing a Breeze navigation property?

Currently, I am utilizing Breeze.js to manage my data model within Knockout.js. This involves a simple view model with an adapter library for handling sorting tables on entity properties. The tables are sorted using tablesorter, along with a knockout bindi ...

Combining input values with predefined defaults

When the user selects the first option in a dropdown menu on my short form, three fields will appear. The first field has a default value while the other two need to be filled out by the user. After that, I need to calculate the total of all three fields. ...

AngularJS enables you to easily manipulate image width and height using the ng-file-upload feature

Seeking assistance with validating image width and height based on a 1:3 ratio prior to uploading using ng-file-upload. The validation should occur before sending the image to the server. Unsure how to retrieve the dimensions of the selected image for val ...

Instructions for changing the background of a specific area to gray

Could use some assistance in changing the red area to grey, tried numerous solutions but can't figure it out. Any help would be highly appreciated! Looking to change the background to a light grey excluding the top bar and navigation. Image Current ...

Is there a way to eliminate the pop-up window background in MUI?

Is there a way to get rid of the white pop-up background in MUI's default CSS? <Dialog open={open} onClose={() => { setOpen(false); }} > <DialogContent> <h1>Do you really ...

Struggling to set a background image for multiple div elements

Hey everyone! I'm working on a small school project website and I've run into an issue with the background on multiple div elements. Here's my HTML code snippet: <div class="bloc-1-text"> <h3> </h3&g ...

Modifying background with CSS and JavaScript

I am currently facing a dilemma and could really use some fresh perspectives on how to tackle this problem. var vaction = "{{vaction}}"; if(vaction === "Driving") document.getElementByClassName("cover").style.backgroundImage = url(https://media.na ...

Utilizing Selenium to Extract Data from ESPN Website

I am currently working on a project that involves scraping data from ESPN, performing calculations on the scraped data, and then iterating through a dataframe to process player information. While I have successfully completed this task for one player, I am ...

Clicking on the checkbox will trigger an AJAX request to cache the

Encountering a problem with an old system I am currently updating: In the <div id='list'>, there is a checkbox list. Upon clicking a checkbox, it triggers an ajax request that returns JavaScript to execute. The JavaScript in the Ajax requ ...

Display multiple values in a select dropdown using Bootstrap 5 properties

I am stuck with the following code<hr> HTML <select id="select" placeholder="Choose Select" class="selectpicker" multiple></select> <div class="container"> <div id="all" clas ...

Interacting with an HTML menu using Three.js

I am currently developing an HTML menu to overlay onto the Three.js viewport. However, I am facing a challenge with achieving consistent interaction with the menu. While I can successfully click on my <input type="range"> sliders and other elements, ...

Retrieve data from the database and dynamically populate a dropdown list based on the selection made in another dropdown menu

I created a JSP page where the values of the second dropdown should change based on the selection in the first dropdown. Both dropdown values are retrieved from the database. The values for the second dropdown are fetched using the foreign key from the t ...

jQuery struggling to parse HTML retrieved through AJAX call

While working on parsing some XML data received from an AJAX request, I encountered a special case that required additional handling. Occasionally, the server would return HTML content, prompting the need for a page reload. However, when attempting to iden ...

Is there a way to create a component that will vanish when hovered over using only CSS and HTML?

Despite my attempts, it seems that the desired functionality is not working as demonstrated here. Here is the CSS code I used: div { background-color: yellow; padding: 20px; display: block; } div:hover { display: none; } Is it necessary ...

Utilizing CSS to showcase various sections of a webpage

Here is a basic example of an HTML file available at this link: http://jsfiddle.net/WhupP/2/ The webpage consists of 4 main sections - header, left-column, righ-column, and footer. Additionally, there are 2 @media elements named screen and print. When att ...

Can solid grid lines be shown using CSS instead of the default dashed grid lines?

Is there a method to achieve consistent and solid grid lines in C3 using CSS without specifying exact line values? For instance, consider C3's basic Grid Line example: var chart = c3.generate({ data: { columns: [ ['sampl ...

How to apply CSS classes to dynamically injected HTML in Angular 7

One of the challenges I'm currently facing is how to assign a CSS class based on a calculation in my component. Here's a snippet from my component class: @Component({ selector: 'app-render-json', template: `<div [innerHtml ...

Styles for Material UI 5 class names

After upgrading from Mui 4 to 5, I am facing a challenge in using class names effectively. While the SX property allows me to apply styles to individual components, I am struggling with applying the same class to multiple components. In version 4, my code ...

Updating background image according to the time in a specific location

I've been struggling with this issue for more than 4 hours now. None of the solutions provided on Stack Overflow seem to work for me. Here's the code snippet I've been working on: <html> <head> <style> .day { ...