Is it possible to rearrange elements that reside within different parent elements?

I have a section on my website that I need to re-order for mobile devices. Currently, it looks one way on desktop and another way on mobile. Here's what I'm trying to achieve:

On desktop:

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

On mobile:

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

I've been working on this using JSFiddle. You can see a live demo here.

@media only screen and (max-width: 480px) {
  .flex-item1 {
    order: 1;
  }
  .flex-item2 {
    order: 3;
  }
  .flex-item3 {
    order: 2;
  }
}
<section id="main-content">
  <div class="container">
    <div class="row" id="top-content">
      <div class="col-lg-4" id="left-content">

        <h1 class="flex-item1"></h1>
        <div class="card-custom flex-item2">
        </div>

      </div>
      <div class="col-lg-8" id="right-content">
        <div class="flex-item3" canplay id="video-block">
        </div>
      </div>
    </div>
  </div>

</section>

Can someone help me figure out what I'm doing wrong?

Answer №1

You should consider utilizing CSS Grid for your layout. I experimented with a helpful Grid Generator tool that can assist you in creating the desired grid structure. It is recommended to view the output in full screen mode for better clarity.

Please Note: If there are any opportunities to simplify this code, feel free to optimize it as I am relatively new to working with CSS Grid.

Check out the JSfiddle Demo here

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr 1fr;
  grid-column-gap: 10px;
  grid-row-gap: 10px;
  height: 500px;
}

.div1 {
  grid-area: 1 / 1 / 3 / 3;
  background: #00A2E8;
}

.div2 {
  grid-area: 3 / 1 / 5 / 3;
  background: #22b14c;
}

.div3 {
  grid-area: 1 / 3 / 5 / 5;
  background: #ED1C24;
}

@media ( max-width: 600px) {
  .div1 {
    grid-area: 1 / 1 / 2 / 5;
  }
  .div2 {
    grid-area: 4 / 1 / 5 / 5;
  }
  .div3 {
    grid-area: 2 / 1 / 4 / 5;
  }
}


/* Additional styles */

.container>div {
  color: #fff;
  font-size: 2em;
  display: flex;
  justify-content: center;
  align-items: center;
}
<div class="container">
  <div class="div1">1</div>
  <div class="div2">2</div>
  <div class="div3">3</div>
</div>

Answer №2

My approach was to use the table tag instead of flexbox.

Feel free to give it a try and see if it works for you.

.flex-item1 {
  background: skyblue;
  width;
  150px;
  height: 100px;
}

.flex-item2 {
  width: 150px;
  height: 100px;
  background: green
}

.flex-item3 {
  width: 300px;
  height: 400px;
  background: red;
}

@media only screen and (max-width: 600px) {
  table {
    width: 100%;
  }
  .flex-item1,
  .flex-item2,
  .flex-item3 {
    display: block;
    width: 100%;
  }
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<section id="main-content">
  <div class="container">
    <div class="row" id="top-content">
      <table>
        <tr>
          <th class="flex-item1">1</th>
          <th rowspan="2" class="flex-item3">3</th>
        </tr>
        <tr>
          <td class="flex-item2">2</td>
        </tr>
      </table>
    </div>
  </div>

</section>

Answer №3

For optimal viewing on mobile devices, utilize col-xs-12.

<section id="main-content">
           `enter code here`<div class="container">
                <div class="row" id="top-content">
                    <div class="col-md-4 col-xs-12" id="left-content">
                        <div class="row col-md-12>
                             <h1 class="flex-item1"></h1>
                        </div>
                        <div class="row col-md-12>
                             <h1 class="flex-item2"></h1>
                        </div>
                    </div>
                    <div class="col-md-8 col-xs-12" id="right-content">
                            <div class="flex-item3" canplay id="video-block">
                            </div>
                    </div>
                </div>
        </div>

</section>  

CSS Styling:

@media only screen and (max-width: 480px) {
.flex-item1 {
    order: 1; 
   }
.flex-item2{
    order: 3;
}
.flex-item3{ 
  order: 2; 
}
}  

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

Separate a string into substrings based on the number of spaces it contains

My goal is to split a string into multiple parts based on spaces, resulting in an array with 3 values. For instance, if I have the string "SIZZLING STEAK TERIYAKI BOY" and divide it into 3 parts, the output should be ["SIZZLING STEAK", "TERIYAKI", "BOY"]; ...

How can I prioritize my own stylesheet over Bootstrap in a React project where it is included in the index.js file?

In an attempt to customize the appearance of a Bootstrap component, I am creating my own stylesheet and importing it into my xyz.js file. Here is the setup: Xyz.js import React, {Component} from 'react'; import axios from 'axios'; imp ...

JavaScript and the importance of using commas in arrays

I am developing a system that displays text in a textarea when a checkbox is checked and removes the text when the checkbox is unchecked. The functionality is mostly working as intended, but I am facing an issue where commas remain in the textarea after un ...

Form Submission in C# Triggered by Empty Required HTML5 Fields

After creating a sign-in form using HTML controls and running them server-side to use in C#, I encountered an issue. <button id="btnSignIn" class="btn btn-lg btn-info btn-block" type="submit" runat="server" onServerClick="btnSignIn_ServerClick">Sign ...

Failure to Present Outcome on Screen

Seeking assistance! I attempted to create a mini loan eligibility web app using JavaScript, but encountered an issue where the displayed result did not match the expected outcome upon clicking the eligibility button. Here is the HTML and JavaScript Code I ...

Utilizing CSS nested spans and setting custom width attributes

I'm curious about how nested spans and CSS handle the width attribute. In the HTML code provided, the 'wide' class sets the width while the 'box' class adds a border. I've noticed that the width is only applied when both class ...

Tips for avoiding word fragmentation in <pre> code blocks

pre { white-space: pre-wrap; /* CSS 3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ word-wrap: break-word; /* Internet Explorer 5.5+ */ overflo ...

Is there a way to import an image from a different webpage using HTML?

Is it possible to load an image from another page using the load method? Here's an example: $("#result").load(url, 'tag.attribute') However, I am facing a challenge where I need to retrieve the path of the first image from a list on anothe ...

Enhance the numerical value displayed in jQuery UI tooltips

I have folders with tooltips displaying text like '0 entries' or '5 entries' and so on. I am looking for a way to dynamically update this tooltip number every time an item is added to the folder. The initial count may not always start a ...

Adding a command to open a new browser tab using JavaScript code can easily be accomplished by following these steps. By using Terminal, you can quickly and efficiently implement this feature

I'm new to coding and trying to create a terminal simulation. You can check out my code here: https://codepen.io/isdampe/pen/YpgOYr. Command: var coreCmds = { "clear": clear }; Answer (to clear the screen): function clear(argv, argc ...

Tips for making a div overlap with Twitter Bootstrap positioning

Currently, I am attempting to utilize Bootstrap 4.5 to position a div over two existing divs. Instead of explaining it through text, the image linked below provides a visual representation of what I am aiming for: https://i.sstatic.net/gbylW.png In this ...

How about creating a responsive layout using only CSS3 for dynamic width adjustments?

I'm struggling to solve a design challenge involving three columns: a navbar (dark gray), main content (dark red), and sidebar (dark green). The navbar should be expandable and collapsible, while the sidebar should slide in and out. My goal is to make ...

Ensure that the child div fills the entire parent area when resizing the page

I am facing an issue with a container div that contains two elements. When I resize the window, one inner div moves under the other. However, my requirement is that when one div moves under another on resizing, the first div should occupy the full width o ...

Is there a way to incorporate a CSS file into this without explicitly mentioning the color?

I have successfully implemented a PHP solution for changing themes with a cookie that remembers the selected theme color when the user leaves the site. However, I now need to switch this functionality to JavaScript while still utilizing the CSS file. How c ...

What is the best way to assign the "active" class to a navigation list item using JavaScript within Bootstrap 4?

Update: just to clarify, I'm working on activating the navbar button based on the current page. I have a navigation bar set up and I am trying to dynamically add an "active" class to the li element when you are on that specific page. However, for som ...

Displaying modal popups limited to one per session

Is there a way to display this Dialog Popup only once per session? I have looked into using cookies for configuration, but haven't been able to implement it in this scenario: $(document).ready(function() { ShowDialog(true); e. ...

What is the method for utilizing alpha transparency in MPDF?

I am currently working with MPDF to generate a PDF file with a full-page background. I want to create a layout similar to the image shown https://i.sstatic.net/RoH3r.png. I believe that I need to incorporate alpha transparency, but I am unsure of how to d ...

Argument for setInterval function

As a newcomer to programming, I am attempting to develop a basic javascript game. I have encountered an issue with the window.setInterval function and it seems to be causing everything to malfunction. I have been following a tutorial at this link and att ...

Using a jQuery UI dialog for multiple checkbox selection. The post array cannot be saved for a multi-select until the dialog is opened

CSS <td class="cell"> <a class="opener" id="opener_'.$iterator.'" href="#" rel="#type_dialog_<?= $iterator; ?>">Click here</a> <div id="type_dialog_<?= $iterator; ?>" name="t ...

A step-by-step guide on retrieving <input> values from an HTML form with PHP and saving them to a text file

I have put together this HTML code featuring a login form designed with bootstrap. Furthermore, I've created a file named hi.php to collect the user's input details (namely their email and password). However, upon logging in, I discovered that th ...