Incorrect Media Query causing Divs to be Aligned Side by Side in the Wrong Order

It seems quite obvious, but I can't seem to find a solution.

I have two divs that I want stacked on top of each other when the screen width is small, and placed next to each other when it's large. The right div has a fixed width, while the left one has a variable width. However, when the screen width is small, they appear in the wrong order. If I switch the order of the divs in the HTML, they no longer align properly.

Here is the fiddle link: http://jsfiddle.net/CwMTU/2/

HTML

<div class="right"> right content fixed width </div>

<div class="left"> left navbar variable width </div>

CSS

.right {
    width: 200px;
    float: right;}

.left{
    margin-right: 200px;}

@media (max-width: 500px) {       
.left {
    width: 100%;}

.right { 
    width: 100%;}
}

Answer №1

For this solution, I wrapped the content in an additional div and utilized display:table/table-cell (switching back to block within a media query)

http://jsfiddle.net/CwMTU/4/

Answer №2

Reorder the <div>'s

<div class="right">
    right content fixed width
</div>

<div class="left">
   left navbar variable width
</div>

Adjust the float property in both classes. Include a width:100%; and set margin-right to -200px in order to align the right div next to it.

.right {
    width: 200px;
    float: left;
    background:green;
}

.left {
    background:red;
    float: left;
    width: 100%;
    margin-right: -200px;
}

You can access the updated JsFiddle through this link:

@media (max-width: 500px) {

.left {
    width: 100%;
    margin-right: 0;             <--------
}

Your JsFiddle has been revised.

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

Utilizing ng-repeat, filter, and the uib-popup-datepicker to refine and display specific data within a table column

I'm currently facing a common scenario in my Angular application where I need to filter items from an ng-repeat using an HTML5 input of type 'date' or Angular-UI-Bootstrap's 'uib-popup-datepicker'. Despite extensive research, ...

To generate a new <div> element by clicking a button and then clicking on it using C# Selenium

Before clicking the button, this is the initial HTML code for the page: <div id="layerContainer"> </div> After clicking the button, the code changes as shown in the picture here I have been attempting to locate the new button, but I keep rec ...

struggling to find the precise value in the array

my original result is displayed below: [status] => 1 [schedule_status] => 1 [cid] =>3 [cdate] => 16-10-18 01:10:52 [did] => 16 [jid] => 4 However, when I try to extract individual array values using the following code: $count = count($ ...

Ways to emphasize search outcomes in Flask/HTML?

I am currently working on creating a search box using HTML and the Flask framework in Python. Below is the layout I am working with: Layout My goal is to input text into the search box and have it highlighted within the text area on the same HTML page. F ...

Executing $(this).parent().remove() triggers a full page reload

A unique feature of my webpage is that with just a simple click of a button, users have the ability to dynamically add an endless number of forms. Each form contains fields for user input and also includes a convenient delete button. var form = " Name ...

Identify whether a specific Instagram post is a video by utilizing Python with selenium

Good day, After reviewing the content on this post: () I managed to extract specific data using the following code: likes = driver.execute_script('''return document.getElementsByClassName('Nm9Fw')[0].lastElementChild.getElement ...

Displaying elements in a Django app using React JS is limited to rendering on a single HTML page within the app's DOM

I've encountered an issue with my Django app that utilizes ReactJS for the frontend. Within my project, I have two HTML files and one App.js file containing two classes. The problem I am facing is that React only renders the elements from the first ...

Modify the width to fit the available space using flex containers

I'm currently working on adjusting the width of a container in case it cannot accommodate an additional character box. IMAGE: Eliminate excess spacing by resizing the width Here is the HTML code <section id="list_users"> <div i ...

Generating interactive HTML content using a JSON file with AngularJS

Currently, I am working on a project where I have a nested JSON object within AngularJS. In order to create a menu from this JSON using the Bootstrap navbar, I need assistance with parsing the highly nested structure and generating dynamic HTML code to d ...

Placing pins on Google Maps

I'm attempting to display two separate markers on two individual maps positioned next to each other on my website. <script type="text/javascript"> var map, map2; function initialize(condition) { // setting up the maps var myOptions = { zoo ...

Why is Emmet's document snippet failing to include the "http-equiv='X-UA-Compatible'" meta tag?

While experimenting with the Emmet boilerplate in VS Code for HTML files, I noticed a discrepancy. Online tutorials show that there should be 3 meta tags when using Emmet, but when I try it in VS Code, I only see 2 meta tags. One meta tag seems to be missi ...

What is the best way to align the start of my array in the middle of the screen?

I decided to implement a basic map function to display our array on the screen: let numbers = new Array(41); for (let i = 0, num = 0; i < numbers.length; i++, num += 5) { numbers[i] = num; } {numbers.map((item, index) => { retur ...

Refresh the data in a table upon clicking the menu

I am looking to develop an accordion MENU, but unsure if I should use divs or <ul>. When a submenu is clicked, I want to execute a mysql query to update the data in the table next to the menu. I'm not sure of the best approach and whether to uti ...

JavaScript button for displaying and hiding all content in one click

I came across a tutorial on W3Schools that showed how to create collapsibles, and I thought it would be great to have buttons that could expand or collapse all at once. I've been trying to implement this feature using the code provided in the tutorial ...

The submit button appears to be malfunctioning after a PHP form is loaded via jQuery load

I currently am in the process of building my website. One issue I am encountering is with the packages page. Upon clicking on the packages menu item, a jQuery load triggers to load a PHP file named wrap2.php. The wrap2 page consists of a large form with m ...

What is the best way to incorporate a counter into my JavaScript game?

I've created this JavaScript game: window.onload = function() { canv = document.getElementById("gc"); ctx = canv.getContext("2d"); document.addEventListener("keydown", keyPush); setInterval(game, 1000 / 7); } px = py = 10; gs = tc ...

Arrange Image on Top of Text in Div with Text Controlling Div Height

I'm looking to stack an image on top of text inside a fixed height div. The challenge is to make the image's size based on the amount of space left after accommodating the text within the same area. Ideally, I would like to achieve this using onl ...

The problem of a child div extending beyond the boundaries of its parent div

I am searching for a layout similar to this, click here. Here is the basic HTML code I have: #mainContent { margin:0; width:100%; height:600px; padding: 0 0 30px 0; } #mainContent #sidebar { /* sidebar inside # ...

How to turn off validation for md-input-container in Angular 2

While working on a form in Angular 2, I encountered an issue when adding the required attribute to my <input>. The problem resulted in unwanted margins as shown in this image: https://i.sstatic.net/W6JSF.png I am seeking a solution to only apply the ...

Django is giving an error message, indicating an invalid block tag. It is looking for either 'empty' or 'endfor' instead of 'else'. This may be due to not registering or loading the tag properly

I'm having some trouble getting this to load correctly. I'm still learning Python and working on understanding its formatting. I've gone through previous inquiries where there seemed to be a syntax error, but I can't seem to pinpoint an ...