How can CSS columns be utilized to create a dynamic layout with stacking elements

Is it possible to reorder three columns from left to right into a stacked layout when the screen size is reduced using a media query? The desired outcome is to have the middle column on top, followed by the left column underneath, and then the right column. I'm struggling with this and would appreciate any guidance or assistance. Below is the HTML and CSS code, as well as a link to the CodePen example: https://codepen.io/Macast/pen/jZworE.

This is the layout I aim to achieve: https://i.sstatic.net/8Vjw0.jpg

HTML:

<body>
<div class="columnContainer">
    <div class="leftColumn" style="background-color:#aaa;">
      <h2>Left Column</h2>
    </div>
    <div class="middleColumn" style="background-color:#bbb;">
      <h2>Middle Column</h2>
    </div>
    <div class="rightColumn" style="background-color:#ccc;">
      <h2>Right Column</h2>
    </div>
  </div>
</body>
</html>

CSS:

* {
    box-sizing: border-box;
}

body {
    margin: 0;
}

.columnContainer {
    width: 100%;
}

.leftColumn {
    float: left;
    width: 33.33%;
    padding: 10px;
    height: 200px;
    text-align: center;
}

.middleColumn {
    float: left;
    width: 33.33%;
    padding: 10px;
    height: 200px;
    text-align: center;
}

.rightColumn {
    float: left;
    width: 33.33%;
    padding: 10px;
    height: 200px;
    text-align: center;
}

.columnContainer:after {
    content: "";
    display: table;
    clear: both;
}

/* Media Query */
@media (min-width: 320px) and (max-width: 480px) {
    /* Column Stacking Here */
}

Answer №1

You have the option to utilize a combination of flex and order properties;

* {
    box-sizing: border-box;
}

body {
    margin: 0;
}

.columnContainer {
    width: 100%;
    display: flex;
}

.leftColumn {
    width: 33.33%;
    padding: 10px;
    height: 200px;
    margin: 0 auto;
    text-align: center;
}

.middleColumn {
    width: 33.33%;
    padding: 10px;
    height: 200px;
    margin: 0 auto;
    text-align: center;
}

.rightColumn {
    width: 33.33%;
    padding: 10px;
    height: 200px;
    margin: 0 auto;
    text-align: center;
}

/* Media Query */
@media (min-width: 320px) and (max-width: 480px) {
  /* Arrangement of Columns in Mobile View */

  .columnContainer {
      flex-direction: column;
  }

  .leftColumn {
      order: 2;
  }

  .middleColumn {
      order: 1;
  }

  .rightColumn {
      order: 3;
  }

}
<body>
<div class="columnContainer">
    <div class="leftColumn" style="background-color:#aaa;">
      <h2>Left Column</h2>
    </div>
    <div class="middleColumn" style="background-color:#bbb;">
      <h2>Middle Column</h2>
    </div>
    <div class="rightColumn" style="background-color:#ccc;">
      <h2>Right Column</h2>
    </div>
  </div>
</body>
</html>

Answer №2

/* Adjusting Layout for Small Screens */
@media (max-width: 480px) {
  .leftColumn, .middleColumn, .rightColumn {
    float: left;
    width: 100%;
    padding: 10px;
    height: 200px;
    text-align: center;
}
}

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

Ways to incorporate a reverse transition effect

I am encountering an issue with a sliding effect on a div. When it has a class hddn, the overlay will have right: -100% from the viewport. However, when the hddn class is removed, it should return to the viewport with right: 0. How can I animate this movem ...

How do you manage conditional formatting within HTML forms?

On one of my HTML pages, I am using the following code snippet: {% for bet in recent_bets %} {% if bet.status__name == "Won" or bet.status__name == "Half Won"%} <div class="container col recent-form_col bg-success" data-toggle="tooltip" da ...

The SelectOneMenu component in Primefaces is not vertically aligned with the InputText field

When placing a Primefaces SelectOneMenu next to a Primefaces Input, I noticed that they do not align at the same height in the browser across different browsers such as Firefox, IE and Chrome: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/x ...

"Utilize the attr attribute to showcase an image within an HTML select element

I'm trying to showcase the flag of each country in a dropdown menu. Here is the code I attempted: <select id="slcCountry"> <option flag="https://restcountries.eu/data/afg.svg">AFG</option> <option flag="https://restcountr ...

"Learn the steps to save a JSON object as a file directly from your web

Is it possible to save a JSON object as a file on the desktop using JavaScript and HTML only? If so, how can this be done, and how can the file be read back afterwards? var canvas = new fabric.Canvas('c'); data = JSON.stringify(canvas) I am wor ...

Showing information from a SQL database in an HTML format

The PHP code below is used to retrieve data from a MySQL table, but I need assistance in displaying it in an HTML form. <?php $servername = "localhost"; $username = "root"; $password = "icsk"; $dbname = "yusuf"; // Create connection $conn = new mysqli ...

Creating word spacing in CSS paired with a navigation menu using Dreamweaver

As a beginner in Dreamweaver, CSS, and HTML, I may have made some mistakes or overlooked simple solutions. I am currently struggling with how to separate words in my navigation menu, as they always appear centered. Despite trying adjustments like "word-spa ...

Is there a way to only apply styles to the specific element I am hovering over in CSS and not affect the entire list hierarchy?

I am trying to display an "Icon" when hovering over each item individually. The issue I am facing is that when I hover over a child item, the parent item also displays its icon. .list { width: 150px; height: 19px; } .list >.item { background-co ...

Aligning vertical pills to the right using Bootstrap framework

Here is the relevant code snippet that I am working with: <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <scrip ...

Hovering over the paragraph will now allow you to shine a spotlight on

Can we highlight only the line being hovered over by the mouse pointer? For instance, if I have this paragraph: <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam in nisi eleifend, efficitur ligula vel, scelerisque risus. ...

Adjust the height of the drawer in material-ui

In my current project using react and material-ui, I am facing a simple issue that has me stumped. I am trying to create a drawer with a specific height so that it does not overlap the app bar when opened. Unfortunately, there is no built-in parameter in ...

I am currently collaborating on the Bootstrap5 project and, in order to accurately incorporate the data, I must align the calendar with the design specifications outlined

Currently, I am working on a project using Bootstrap5 and I need to adjust the calendar to match the design in Figma. At the moment, my calendar looks like this: https://i.sstatic.net/MBuKVbVpm.jpg However, I want it to look like the second image that I ...

To ensure proper formatting, I must include a comma operator for numbers while typing and limit the decimal places to two

Could someone assist me in formatting a number to include commas and restrict the decimal places to two using regex? I have attempted the following, but need help making it work properly. Currently, when I enter a number it shows up as 1231244, however I ...

Background image design for mobile using Bootstrap

On my website, I have a background image set. However, I noticed that when viewing it on a mobile device, the background image appears in different sizes on different pages, even though they are all linked to the same stylesheet and use the same code. You ...

Issues with checkboxes in HTML 5 showing inconsistent behavior

Utilizing: APS.NET MVC 4.0 Incorporating javascript/jquery to manage check boxes within a table, I have encountered an issue where the code functions correctly during the first and second passes. Initially, it checks all the checkboxes, while on the secon ...

App builder shows raw HTML code instead of generating a dynamic table

I am currently working on an application that requires gathering data in a csv format from Google Sheets and importing it into App Inventor. Additionally, I need to include HTML code to display the table in a more visually appealing way. Here is what the ...

Is it possible for a Div's height to be equal to 100% of its container's

<div id="wrapper"> <div id="main-content"> //my main content </div> </div> #wrapper{ height:100%!important; min-height:700px!important; } #main-content{ height:100%!important; } Despite setting the height of my main ...

Is there a way to update the styling of specific sections of an input field as the user enters text in React?

Just like in most markdown editors, I am looking to enable the ability to modify parts of an input field as the user enters text. For instance: When the user types "_above_", as soon as the second underscore is typed, the text should be styled accordingly ...

Javascript Unable to Update Value of Input Field

While experimenting with different functions in this decentralized application (dApp), I encountered an issue related to entering a specific value in a text field. My goal is to input a desired value into the text field located next to the 'Deposit&ap ...

Troubleshooting issue with CSS in Django_tables2

Working with the django_tables2 plugin for the first time has been an interesting experience. I followed the installation and quick start guide provided in their documentation. After pip-installing django-tables2, adding 'django_tables2' to my ...