Divs are piling up on one another, but they're not in the correct sequence

Currently, I have two divs placed side by side and utilizing a media query to stack them. However, my goal is to have the yellow div on top with the blue div below it once the script is executed. Unfortunately, I am unsure of how to achieve this.

#wrapper {
    width:1000px;
}
#mydivLeft {
    background:blue;
    height:250px;
    width:50%;
    float:left;
}

#mydivRight {
    background:yellow;
    height:250px;
    width:50%;
    float:right;
}

@media only screen and (max-width:768px){
    #wrapper {
        width:100%;
    }

    #mydivRight, #mydivLeft {
        display:block;
        float:none;
        width:100%;
    }

}
<body>
    <div id="wrapper">
        <div id="mydivLeft">
        </div>
        <div id="mydivRight">
        </div>
    </div>
</body>

Answer №1

You have the ability to utilize flexbox and the order property to rearrange how elements are displayed

#container {
  width: 1000px;
  max-width: 100%;
  display: flex;
  flex-wrap: wrap;
  height: 250px;
}
#container div {
  flex: 1;
}
#itemLeft {
  background: blue;
}
#itemRight {
  background: yellow;
}
@media only screen and (max-width: 768px) {
  #container div {
    flex: 0 100%
  }
  #itemRight {
    order: -1
  }
}
<body>
  <div id="container">
    <div id="itemLeft">
    </div>
    <div id="itemRight">
    </div>
  </div>
</body>

Answer №2

To create a responsive layout, consider utilizing flexbox. By default, the flex-direction property is set to row, but in your media queries you can adjust it to column-reverse.

#wrapper {
  max-width: 1000px;
  display: flex;
}
#mydivLeft, #mydivRight {
  flex: 1;
  height: 250px;
}
#mydivLeft {
  background: blue;
}
#mydivRight {
  background: yellow;
}
@media only screen and (max-width: 768px) {
  #wrapper {
    flex-direction: column-reverse;
  }
}
<div id="wrapper">
  <div id="mydivLeft"></div>
  <div id="mydivRight"></div>
</div>

Answer №3

You have the flexibility to rearrange the divs in your HTML code as you see fit.

#wrapper {
    width:1000px;
}
#mydivLeft {
    background:blue;
    height:250px;
    width:50%;
    float:left;
}

#mydivRight {
    background:yellow;
    height:250px;
    width:50%;
    float:right;
}

@media only screen and (max-width:768px){
    #wrapper {
        width:100%;
    }

    #mydivRight, #mydivLeft {
        display:block;
        float:none;
        width:100%;
    }

}
<div id="wrapper">
  <div id="mydivRight">
  </div>
  <div id="mydivLeft">
  </div>
</div>

For a more intricate approach, consider utilizing flexbox with properties like order or flex-direction: column-reverse; to modify the order of the flexible container's children.

#wrapper {
  width: 1000px;
  display: flex;
}

#wrapper > div {
  width: 50%;
  height: 250px;
}

#mydivLeft {
  background: blue;
}

#mydivRight {
  background: yellow;
}

@media only screen and (max-width:768px) {
  #wrapper {
    width: auto;
    flex-direction: column-reverse;
  }
  #wrapper > div {
    width: auto;
  }
}
<div id="wrapper">
  <div id="mydivLeft">
  </div>
  <div id="mydivRight">
  </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

Background image positioned behind a carousel

I'm currently working with a Bootstrap carousel that's functioning perfectly, but I have a desire to incorporate a background image behind this carousel. The background image should be at a width of 100% and 80% of the image should be covered by ...

Retrieve webpage using HTML stored in web storage

I am exploring a new idea for an application that utilizes local storage, and I am seeking guidance on the most effective way to load content after it has been stored. The basic concept of the app involves pre-fetching content, storing it locally, and the ...

The background color is showing the wrong shade

So, I recently created a simple HTML5 website and decided to set the background-color of the header div to #3D7D99. However, when I view it in the browser, it appears as #35728E. Can anyone explain what might be causing this discrepancy? UPDATE: Just to c ...

Detecting file corruption from a form-based web page file upload (using input type="file")

Can file corruption occur during the process of uploading a file through a web form? Specifically, when an input of type "file" is included in a form submission and the file is saved on the server (typically in a designated temporary upload directory). Is ...

Issue with Table Element's Full Width Within Parent Element

Here is a demonstration of the scenario in JSFiddle I have made updates to the JSFiddle demonstration here: http://jsfiddle.net/x11joex11/r5spu85z/8/. This version provides more detailed insight into how the sticky footer functions well, albeit with a hei ...

Tips for positioning your side navigation menu parallel to the page edge

Im looking to have the side navigation floated to the left as I scroll down the page. I tried using the sticky property, but it didn't work out. Here's a snippet of the code: .sticky-class { position: sticky; top: 30px; } This is how my HTML ...

Video background not playing on Bootstrap 5 Jumbotron

I created a sleek jumbotron design with a video background, but for some reason, the video isn't playing. I have no trouble loading the video from a URL separately, so I'm stumped as to what's causing the problem. Here's the simple HTM ...

Is it possible to adjust the positioning of this navigation style?

Discover a unique collection of navigation styles by following this link: http://tympanus.net/Development/ArrowNavigationStyles/ Be sure to explore the final style "Fill Path" at the end of the page. I'm interested in adjusting the position of the ...

Are you familiar with PowerShell and its innovative modular GUI capabilities?

One challenge I face is assisting clients who constantly monitor the performance of their servers, services, and network. Utilizing PowerShell, I envisioned creating a unified GUI interface for all these tasks, including remote log tailing capabilities. B ...

Tips for fixed positioning of a div on a webpage without being affected by the Windows logo shortcut keys + Left/Right arrow

Whenever I utilize the keyboard shortcuts to move a window from one side of the screen to another and then minimize it either upwards or downwards, I find myself faced with an issue. I can accomplish this action by using the Windows key in combination with ...

How can I attach a cookie to a div that becomes visible after a few seconds of video playback?

After 20 seconds of video play, a div element appears. I am trying to set up a cookie so that once the div is shown, it continues to appear unless the user clears their cache (cookie logic). This is my current attempt - http://jsfiddle.net/9L29o365/ Any ...

A tutorial on how to create the appearance of disabled buttons that look the same as enabled buttons through the use

My button includes a text field that is constantly disabled. I would like for the text field to appear as bright as it does when the button is enabled. After disabling the button, they both appear dimmer compared to others and the text field follows suit ...

Adjust image width in column

After searching through various tutorials and Stack Overflow posts, I am still unable to properly resize my image in the right column. I have a layout with a left and right column - the left contains a menu and the right holds some content along with a ba ...

Seems like JavaScript's .active functionality is failing to function properly in my case

I'm encountering an issue with my website's homepage. I have a list of services displayed, and the intention is for a description to appear when one of the services is clicked. However, clicking on the buttons does not trigger the expected action ...

The JSON ticker for BTC/LTC has stopped functioning

I've been relying on this JSON ticker for the past month and it's been smooth sailing. However, today it suddenly stopped working. Any ideas on what might have caused this issue? $(function () { startRefresh(); }); function startRefresh() { ...

Tips on personalizing the DateTimePicker component from Material-UI

I am currently working on customizing the DateTimePicker component provided by Material-UI. To access its documentation, please visit: One challenge I have encountered is the lack of styling information in the documentation. My goal is to change the main ...

Every time I attempt to visit my website on a mobile device, it seems to appear larger than normal, as

After developing a responsive website and adding media queries to make it mobile-friendly, I encountered an issue. Despite my efforts, the website's width is still larger than the mobile viewport, requiring users to zoom out to view it properly as sho ...

Arranging elements within a complex div structure

I have been working on styling this div that displays the products for an e-commerce website. I initially used CSS and a table within it, but it seems like using tables for content is not recommended, so I am trying to find a better solution. However, my a ...

Is there a way to tally up the number of green items and display a <p> tag when every item has been marked green?

I have created a checklist that includes checkboxes. I am looking to display some text when all the checkboxes are checked and green. Can someone assist me with writing the code for this functionality? $(document).ready(function() { $("i").click(funct ...

Eliminate the custom button feature from the Datatable

https://i.sstatic.net/7ndvv.png Is there a way to hide the Copy, CSV, Excel, PDF, and Print buttons located in the top right corner of the datatable? These seem to be default features of datatables, but I have been unable to find any information on how to ...