Is it possible to reorganize several divs for mobile screens?

My setup is quite intricate.

I have a layout with 2 rows consisting of three divs each on larger screens.

It looks something like this:

<div class="row row-1">
  <div class""square">square 1</div>
  <div class""square">square 2</div>
  <div class""square">square 3</div>
</div>
//some more html
<div class="row row-2">
  <div class""square">square 4</div>
  <div class""square">square 5</div>
  <div class""square">square 6</div>
</div>

On mobile devices, I need to rearrange the squares so that the first two squares from each row are in the top row, and the last square from each row is in the bottom row. It should look like this:

<div class="row row-1">
  <div class""square">square 1</div>
  <div class""square">square 2</div>
  <div class""square">square 4</div>
  <div class""square">square 5</div>
</div>
//some more html
<div class="row row-2">
  <div class""square">square 3</div>
  <div class""square">square 6</div>
</div>

What would be the most efficient way to achieve this? I'm considering using jQuery to detect screen size and move the squares accordingly, but I'm open to other suggestions.

Appreciate any help!

Answer №1

Transferring elements between wrappers using CSS alone is not possible, so JavaScript becomes the only viable option. However, it may be feasible to achieve the desired result by utilizing CSS-Grid, depending on the structure of your additional HTML content.

One approach could involve utilizing display:contents to separate the wrappers and then employing CSS-Grid to position the relevant child elements...

It's important to note that this solution is quite specific and hinges on the specifics of the extra HTML content. For the purposes of illustration, let's assume it consists of a single div element.

.row {
  display: contents;
}

body {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.row-2 :nth-child(1),
.row-2 :nth-child(2) {
  grid-row: 2;
}

.content {
  grid-row: 3;
  grid-column: span 2
}

div {
  outline: solid 1px lightgrey;
}
<div class="row row-1">
  <div class "square">square 1</div>
  <div class "square">square 2</div>
  <div class "square">square 3</div>
</div>
<div class="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod unde doloremque eum incidunt provident pariatur fugiat repellendus iste, vitae excepturi temporibus, illo voluptates rerum assumenda quo corrupti sapiente odit, labore tenetur dignissimos!
  Culpa, aliquam unde!</div>
<div class="row row-2">
  <div class "square">square 4</div>
  <div class "square">square 5</div>
  <div class "square">square 6</div>
</div>

Answer №2

If you're looking to simplify your layout and utilize flexbox, you can achieve the desired result by consolidating your div containers into one. By utilizing a media query, you can easily adjust the order of elements on the page.

Check out this fiddle for example

.main-container {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  width: 100%;
}

.main-container .item:nth-child(1) {
  order: 1;
}

.main-container .item:nth-child(2) {
  order: 2;
}

/* Additional item order styles go here */

.item {
  background-color: purple;
  color: #000;
}

.square {
  width: 100px;
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 1%;
  background-color: black;
  color: #fff;
}
<div class="main-container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
  <div class="item">Item 4</div>
  <div class="item">Item 5</div>
  <div class="item">Item 6</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

Modify input text using jQuery when a radio button is selected from multiple forms on a single page

I am interested in implementing a feature similar to what @JCOC611 did here: This involves changing the TEXT value based on the selection of a RADIO BUTTON. However, I would like to have multiple forms on the same page. Is there a way to achieve this? T ...

Button group malfunctions on smaller screens

After integrating a button group into my new website, I noticed that the first two buttons stop functioning properly on smaller screens. Surprisingly, if I remove the text-center div, all buttons cease to work entirely. Despite attempting various solution ...

Switch up your code and toggle a class on or off for all elements that share a specific class

I've been attempting to create a functionality where, upon clicking a switch, a specific class gets added to every element that is assigned the class "ChangeColors". Unfortunately, I have encountered some difficulties in achieving this task. The error ...

Make the child element's height 100% when the parent's width is not set

Having trouble implementing the height:100; property on hover in my portfolio section. I've tried several solutions from similar questions without success. Although it works with media queries, I believe there's a better way to achieve this but I ...

Is it possible to have Mobile WebKit store the click position?

I am in the process of developing a unique framework specifically designed for WebKit devices. One of the features I have implemented is a series of list views that activate a 'hover' class when users touch them. Below is the jQuery/JavaScript co ...

Having trouble with uploading images?

I'm having issues with using PHP to upload an image to a folder. Can someone please help me troubleshoot this? Thank you! Here is the code for setup.php <?php include("../includes/config.php"); ?> <?php if ($_SESSION["isadmin"]) { ...

Elevating the Sidebar: Transforming Bootstrap 4 Sidebar into a Top

Recently, I delved into the topic of creating a responsive sidebar menu in Bootstrap 4. After exploring various solutions, I opted for an alternate version. Here's the HTML code snippet that I implemented: <div class="container-fluid"> < ...

Step-by-step guide on crafting a background design similar to the one depicted in the image

How can I add a background color of #F74422 in the top right corner of my HTML page? https://i.sstatic.net/3fn1s.png ...

jQuery is unable to locate the FireBreath object

Currently, I am in the process of developing a web video player by using Firebreath to compile my C/C++ codec as a browser plugin. The plugin has been successfully loaded and is functioning properly (start, stop video, etc.). My next goal is to enable full ...

Jquery onchange event fails to fire

$('#selectclassid').trigger("change"); $('#selectclassid').on('change', function() { }); When trying to manually trigger the onchange event, it does not seem to be firing as expected. Although this format is commonly seen ...

CSS containers not lining up correctly

Currently in the process of developing a web application, I am facing challenges with my CSS panels constantly shifting around unexpectedly. These panels feature an image with a 4:3 aspect ratio along with a description section. While they appear stable ...

Implementing CSS classes onto HTML elements following their retrieval from a database

Is there a way to add CSS classes to HTML tags when retrieving them from a database for a blog? For instance, if the database contains the following in a column named body: <p> SO is great </p> Then when outputting it in the View using s ...

When attempting to choose two dates within this application, an error is encountered stating that the datepicker function is not recognized

I am encountering an issue while trying to select two dates in this program. When I attempt to execute it, the error message "datepicker is not a function" appears. datepicker is not a function Here is the code snippet: var startDate; ...

CSS HTML parent div cannot contain image properly

Creating a website using Bootstrap: <div class="row padding_zero"> <div class="col section px-0"> <img class="img-trans padding_zero" src="./svg/clouds-00.svg" style="margin-top: -150px& ...

Execute a callback for each item within a when().then() block

Looking for advice on this code snippet: var fetchItems = function(resources, successCallback, progressCallback){ var deferreds = []; for(var idx = 0; idx < resources.length; idx++){ ... deferreds.push(<some action>); } jQuery. ...

Reordering script files in Yii is possible by changing the order in which they

Currently, I am in the process of creating a widget for a Yii application. The main layout view is responsible for registering all common script files like jQuery and jQueryUI. However, my widget requires a jQueryUI plugin that depends on jQueryUI already ...

Instead of providing proper JSON, the system is giving back [object, Object]

Check out my function below: function retrieveData(){ $.getJSON( "viewcustomers.php", function(data) { var records = []; $.each( data, function( key, val ) { records.push( "<td>" + val + "</td>" ); ...

Arrangement of items in a grid featuring a row of three items, each with an automatic width

I'm facing challenges with achieving a specific layout: https://i.sstatic.net/a5ETh.png https://i.sstatic.net/I7fuS.png My goal is to have the left and right elements automatically adjust their width to cover all the empty space on either side of t ...

Instructions on setting a photo as a background image using a text-based model

I'm a beginner with Angular so I may have a simple question. I am using an image from the Google API, which is not a URL. How can I set this image as the background-image in a CSS tag that only accepts URIs? Thank you! ...

What causes bubbling among a parent's siblings?

When I create a simple slideToggle effect on click event, the '.close' event seems to be generating bubbles. Both unbind() and stopImmediatePropagation() have worked for me in the past, but for some reason e.stopPropagation() doesn't seem to ...