Is it possible to rearrange div elements within different containers using media queries and CSS in Bootstrap4?

I've been utilizing the first/last utility in BS4, but my scenario requires relocating an element to and from a completely different section of the page.

For instance, let's imagine I have the following setup:

<div id='area1'>
   <div id='myElement'> My element </div>
</div>

<div id='area2'>

</div>

The objective is for #myElement to transition into #area2 and back efficiently.

Is there a way to accomplish this purely with CSS rules? If not, what would be the most minimalistic approach using JavaScript?

Answer №1

I am unsure of your requirements. If I understand correctly, you can achieve what you want using the data-* attributes such as data-title, data-description, and data-label.

It appears that your content is dynamic and your HTML structure should look something like this:

<div id='area1'>
   <div id='myElement'> My element </div>
</div>

<div data-area1element="My element" id='area2'>

</div>

The above HTML will function without any CSS for desktop and mobile view. You can display the content of id="myElement" in id="area2" when the browser window is less than or equal to 767px. The content of id="myElement" should be included in the data-area1element="" attribute.

Below is the CSS styling:

#myElement{
    color: green;
}
@media (max-width: 767px){
    #myElement{
        display: none;
    }
    #area2{
        display: flex;
        flex-direction: column;
        padding: 10px;
        border: 2px solid red;
    }
    #area2:before{
        content: attr(data-area1element);
        font-size: 15px;
        font-weight: 700;
    }
}

A code snippet is also provided below for reference:

#myElement{
  color: green;
}

@media (max-width: 767px){
    #myElement{
        display: none;
    }
    #area2{
        display: flex;
        flex-direction: column;
        padding: 10px;
        border: 2px solid red;
    }
    #area2:before{
        color: blue;
        content: attr(data-area1element);
        font-size: 15px;
        font-weight: 700;
    }
}
<div id='area1'>
   <div id='myElement'> My element </div>
</div>

<div data-area1element="My element" id='area2'>

</div>

I believe this CSS solution will address your needs.

Upon further consideration:

If you wish to handle this dynamically, a jQuery solution may be necessary as pure CSS won't suffice. Here's an example where a duplicate element is created for smaller screen sizes:

<div class="container py-4">
  <div class="card p-3 mb-3">
    <h1 class="text-center my-4">Desktop Form</h1>
    <div id="desktopFrom" class="d-none d-lg-block">
      <form action="" method="POST">
        <div class="form-group">
          <label>Your Name</label>
          <input placeholder="Your Name" type="text" name="text" class="form-control" />
        </div>
        <div class="form-group">
          <buton type="submit" role="button" class="btn btn-primary">Send Form</button>
        </div>
      </form>
    </div>
  </div>
  <!-- Desktop Card -->
  <!-- Mobile Card -->
  <div class="card p-3 mb-3">
    <h1 class="text-center my-4">Mobile Form</h1>
    <div id="mobileFrom"></div>
  </div>
</div>

The Bootstrap@4 framework is utilized in the above HTML code.

$(function(){
  var desktopForm = $('#desktopFrom').html();
  $(window).on('load resize', function(){
    if ($(window).width() <= 991) {
        $("#mobileFrom").html(desktopForm);
    } else {
      $("#mobileFrom").empty();
    }
  });
});

This jQuery script provides a custom solution.

You can test out the functionality on this CodePen link. Give it a try!

Answer №2

To address this issue, utilize CSS for a solution.

#box1{
 width:200px ;
     height:200px ;
     
     background:green;
}

#box2{
 width:200px ;
     height:200px ;
     
     background:#ff0055;
}
.element{
 width:50px ;
     height:50px ;
background:yellow;
}
#myBox1{
display: block;

 
}
#myBox2{
  display: none;
  
  
  }

@media only screen and (max-width: 415px) {
#myBox1{
display: none;
}
#myBox2{
  display: block;
  }
}
<div id='box1'>
   <div id='myBox1'class="element"> Content here </div>
</div>

<div id='box2'>
  <div id='myBox2' class="element"> Content here </div>
</div>

Answer №3

When it comes to tackling this issue, the approach will vary based on the specific task at hand. One quick solution could involve adding #myElement to #area2 and assigning it the class d-none. The timing of when you choose to implement this solution will depend on your unique situation. Consider a configuration like the following:

<div id='area1'>
 <div id='myElement' class="d-none d-md-block"> My element </div>
</div>
<div id='area2'>
 <div id='myElement'class="d-block d-md-none"> My element </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

Is it possible to convert nested CSS into Material-UI's CSS in JS method?

Here is the code for embedding an iframe: <style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: abso ...

What is the process for incorporating styles into an external widget?

I am currently navigating my way through NextJS and attempting to incorporate a bespoke stylesheet into my code using a third-party widget. Unfortunately, I have hit a roadblock in this process. The names I have assigned to the style sheet align with what ...

custom dialog box appears using ajax after successful action

Recently, I created a custom dialog/modal box with the following code: <div id="customdialog" class="modal"> <div class="modal__overlay"></div> <div class="modal__content"> <h2><strong>Hello</strong&g ...

Emphasizing the text while making edits to an item within the dhtmlx tree

Whenever I need the user to rename an item on the tree, I trigger the editor for them: tree.editItem(tree.getSelectedItemId()); However, I want the text in the editor to be automatically selected (highlighted). Currently, the cursor is placed at the end ...

Can I apply zebra striping to specific columns in a b-table?

I am working with a basic b-table that has row striping enabled: <b-table :fields="myfields" :items="myData" striped> </b-table> Is it possible to apply striped formatting to only specific columns of the table, rather than ...

HTML elements within the parent are seemingly enlarged compared to the parent element

I'm currently working on a project for an angular 2 recipe application, and I've encountered an issue with one of my HTML elements. The list items within the unordered list (which contain checkboxes) appear to be larger than the parent UL element ...

Is it possible to dynamically update the contents of a modal body and modal footer using

I'm dealing with a modal that dynamically populates two sections: modal-body and modal-footer. However, the issue is that only the content of modal-body changes dynamically while modal-footer remains static. Here's an example of the HTML code (w ...

What is the best way to align an equal number of divs in each row?

I need help arranging my divs in rows, with 3 divs in each row. Can someone assist me with this? Here is the code I have written so far: .container { display: flex; flex-wrap: wrap; } .item { border: 2px solid black; width: 100px; height: 10 ...

The function you are attempting to use is not modal

When attempting to open a modal in my TypeScript file within the OnInit function, I encountered an error stating $(...).modal is not a function. To address this issue, I first loaded Jquery.min.js in my index.html and then proceeded to load bootstrap.min. ...

Sticky sidebar that adjusts to viewport size in a responsive layout

When designing a CSS fluid layout, most divs utilize percentages to adjust based on the viewport size. However, I have a specific scenario where I need my sidebar to maintain a fixed position. For instance: <aside style="width:25%; float:left; positio ...

Issues arise when attempting to smoothly scroll to an anchor point in a webpage

While working on my website, I have encountered a challenge. The issue arises when dealing with multiple div items. Upon scrolling slightly, the entire page focuses on the div with a height of 100vh, which works perfectly fine. However, my attempts to ...

Guide on submitting a form via Ajax on a mobile app

Looking for a way to submit the form located in components/com_users/views/login/tmpl/default_login.php using Ajax. <form action="<?php echo JRoute::_('index.php?option=com_users&task=user.login'); ?>" method="post"> <fie ...

The ellipsis styling is being ignored

Check out the code provided below. It pertains to the text box located in the top right corner, which reveals a drop-down box when clicked. My intention is for long text in this box to be truncated with ellipsis using text-overflow:ellipsis. From my unders ...

Obtaining POST information from Web2py to a personal HTML document

Dealing with heavy jQuery cross-domain issues, I have turned to using web2py as a workaround. I am sending POST data from a local HTML file to my web2py server, which then makes a Python POST request to a second server (solving the cross-domain problem) a ...

Tips for creating the X Mark using SVG and CSS

I'm struggling to create an animation of an "X" checkmark sign (for failure). Although I found a great example of an animated checkmark sign for success, the code uses a curve-bezier design that I can't replicate for the "X" sign. If anyone cou ...

How can I detect a click event in Angular using Typescript?

I am looking to transform the provided jquery scripts into typescript code. Jquery $(".room").click({ console.log("clicked"); }); TypeScript import { Component } from '@angular/core'; declare var $: any; export class AppComponent { } ...

Guide on adding an image to a Bootstrap card component

I'm facing a simple issue that I can't seem to solve. As a beginner in using bootstrap, I'm struggling to figure out how to insert an image into the card header. I've been searching online for solutions for the past 30 minutes with no l ...

Guide on altering the Class with jquery

Here is My jQuery Code: $('a#cusine1').on('click', function(){ $('div#product-list').html("LOADING..........").show(); $(".ccid").addClass("0"); document.getElementById("ccid1").className="acti ...

Utilizing jQuery to transfer values from one text box to another: a step-by-step guide

Possible Duplicate: Unable to get textfield value using jQuery i am currently working on an invoice where I need to iterate values from a list. The invoice contains a textbox named subtotal which holds a numerical value. Another textbox, named discoun ...

Automatically populate the input field with "text" type in a JSP page without using a form tag (JSP)

My attempt to add an autofill feature to my text field has been unsuccessful. The autocomplete attribute seems to be undefined, and I am hesitant to use a form action to solve this issue. Additionally, I have included the easy-autocomplete.min.css file, ...