Display the divs next to each other if there is ample space available; otherwise, stack them on top of each other

Within my website, I have a main container called #main-element whose width is dynamic and based on various factors including the window size. Inside this container, there are multiple elements as well as a footer container named #wrapper which always spans the full width of its parent container and houses two child divs, #left and #right.

https://i.sstatic.net/7KzGa.png

The #left div can scale freely down to a minimum width of 300px, while the #right div has a fixed width of 120px. The specific behavior I'm aiming for includes:

  • If the width of #wrapper is large enough to accommodate both child elements (greater than or equal to 320px), display them side by side with #left filling up the remaining space.
  • If the parent container cannot fit both child elements, allow #left to take up the entire width and display #right below it.

I have successfully implemented most of this functionality, but the one thing that eludes me is how to ensure that #right appears below

#left</code when they cannot fit together horizontally. I am certain there must be a simple solution that I'm overlooking.</p>

<p>I would prefer a solution that does not rely on JavaScript and is compatible with older browsers (e.g. excluding the use of flexbox).</p>

<p>Below, you will find the code I've developed so far. Please note that the JavaScript portion is irrelevant to the issue at hand and is included solely for demonstration purposes.</p>

<p><div>
<div>
<pre class="lang-js"><code>!function(t){t(document).ready(function(){t.fn.asize=function(){t(this).animate({width:t(this).css(t(this).width()+"px"==t(this).css("min-width")?"max-width":"min-width")},{duration:3e3,step:function(i){t("span",this).text(Math.round(i)+"px"),t("#left").text(Math.round(t("#left").width())+"px"),t("#right").text(Math.round(t("#right").width())+"px")},complete:function(){t(this).asize()}})},t("#main-element").asize()})}(jQuery.noConflict());
#main-element {
    /* this element HAS to be relative */
    position: relative;
    
    /* below rules are irrelevant */
    height: 100px;
    min-width: 360px;
    max-width: 570px;
    border: 1px solid #f00;
}

#wrapper {
    /* width is always 100% and always aligned to bottom */
    width: 100%;
    position: absolute;
    bottom: 0;
    left: 0;
}

#left {
    /* force div to have at least 300px and exclude #right from its width */
    overflow: hidden;
    min-width: 300px;

    /* below rules are irrelevant */
    background-color: #aa0;
    height: 50px;
}

#right {
    /* show on the right; width always set, height equal to #left's height */
    float: right;
    height: 50px;
    width: 120px;

    /* below rules are irrelevant */
    background-color: #990;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- main widget container -->
<div id="main-element">
    <!--some number of elements inside the widget -->
    <span></span> 
  
    <!-- footer of the widget-->
    <div id="wrapper">
        <div id="right"></div>
        <div id="left"></div>
    </div>
</div>

Answer №1

Here is the solution you've been searching for.

To achieve the desired layout, I utilized the display:table property for the wrapper class and applied display:table-column to the inner elements with the classes left and right.

Additionally, a media query is necessary in order to set screen width breakpoints. In this case, I referenced the width from your main-element class using

@media screen and (max-width: 570px)
. Feel free to adjust the value from 570px to any other size like 360px.

When the screen hits the designated breakpoint, the div with the class left will receive a min-width:100%; style, causing the div with the class right to move below it.

For the scenario where the right div drops down, I added a float:left property. This can be modified based on your specific needs.

This setup aligns with what I gathered about your requirements. If there are any adjustments needed, please let me know so I can assist further.

View DEMO on JSFiddle

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

Adaptive stationary slideable navigation bar

By utilizing Bootstrap, I have successfully implemented a sidebar feature. Initially, only the Font Awesome icon is visible, but upon hovering, it smoothly slides to the left revealing more content. Challenge: Unfortunately, the sidebar lacks responsivene ...

<div> arranged side by side with ample spacing

I am currently designing a footer titled <footer class="site-footer">. This footer consists of a container <div class="container"> with 3 divisions inside it: one col-sm-12 col-md-6, and two col-xs-6 col-md-3. My goal is to ...

Creating a custom form to generate a unique URL on a website

For instance, users on a platform like Craigslist can fill out a form specifying their requirements. Upon submission, the platform automatically generates a new URL hosted on its website. I am unsure of how this process works or what it is called. Can an ...

Challenges arise when media queries fail to activate properly

Greetings! I am currently working on enhancing the responsiveness of my webpage using Media Queries. However, I seem to be facing a challenge with the second image not changing as expected when I reduce the browser size. In the HTML, 'top-background- ...

Make sure the div is always positioned above everything, including any built-in pop-up windows

When working with two forms, one for user input and the other as a pop-up window to display results, users sometimes close the pop-up window prematurely if they think there is a network issue causing a delay in data execution. To prevent this, I am consi ...

Guide to transforming date format in a form output

Here is my code snippet for generating a WhatsApp API URL based on form input. However, I'm facing an issue where the date format changes from DD/MM/YYYY to YYYY/MM/DD when the JavaScript function is called. I was aiming for the output to be in the f ...

Having trouble with the Bootstrap 5 Dropdown menu, not sure what's causing the issue

I have included my code below, however, I am encountering an issue with the dropdown menu not dropping regardless of what I press. At this moment, I do not have a main.js file in place. Below is the code snippet: <!DOCTYPE html> <html lang=" ...

When a specific JavaScript function is triggered, the value of an HTML control will reset to its original default value

I have a form with a specific requirement. I need to allow users to input data in a text field and press enter, which should dynamically create new controls like another text field, a dropdown menu, and another text field using jQuery. While the functional ...

Issue with displaying image in Angular 7 component on webpage. What could be causing the image not to appear?

I am currently working on a cat clicking game, but I'm encountering an issue where the image of the cat doesn't display when I include the path in the component's template using img. I've tried utilizing [img]="path" as a property and ...

Tips for causing additional items to materialize each time a predetermined item is selected

On my main page, I have an object that I want users to click on to reveal other objects one by one. I currently have 5 layers within my index.html file, but I'm unsure of the specific code needed in each layer to ensure that clicking the main page obj ...

What is the best way to execute two JavaScript functions within an inline onclick event?

I am currently working on the following code snippet: <script> function delay (URL) { setTimeout( function() { window.location = URL }, 2000); }</script> And then I have the following within the <body> element: <img src="small_rabbi ...

Steps For Adding Margins To A ProgressBar In Bootstrap To Give It A Spaced Look

Currently, I am endeavoring to design a progress bar that includes some spacing from the edges. The desired end result can be seen in the image below: https://i.sstatic.net/IvMFS.png The visual displays a red line within the gray progress bar, which is e ...

Display the final submenu of the Asp.net menu on the right side

The asp.net menu on my website is functioning properly with one exception: the last submenu does not change direction from left to right, and its content is getting cut off at the edges of the screen. This issue is particularly noticeable in the Master pa ...

Using PHP to create a loop that generates a multi-radio form: a step-by-step guide

Despite coding it this way, the multiple radio form is still not functioning as intended. I attempted to utilize fieldset, but to no avail. How can I rectify this issue? Could using various ID's for fieldset assist in creating multiple forms correctly ...

Basic jQuery template design

Currently, I am developing a user interface that allows users to add or delete items with a similar layout. The process starts with one item and by clicking 'add', more items can be added. These items come in several different types. My current ...

Execute AngularJS code when a button is clickeddoubleValue

I am in the process of developing a dynamic page where users can add anywhere from 1 to n products effortlessly. Although I have been exploring AngularJS for several months, I find myself facing some challenges when conceptualizing this scenario. The HTM ...

Should each navigation item be enclosed within its own div element?

Currently, I am studying HTML + CSS and developing a website that requires a vertical navigation bar on the left side with four interactive elements. I'm wondering if it is customary to enclose each of these four elements in a div, or if there is a mo ...

Tips on customizing the appearance of JFoenix components within JavaFX Scene Builder using CSS

I'm struggling to customize the appearance of a JFXButton using CSS stylesheets. While some properties are working fine, such as background color and size, I can't seem to get font color and weight to apply: .eliminarBtn { -fx-background-col ...

The collapsible navbar vanished, where did it go?

While experimenting with the carousel template page, I noticed that the NavBar collapses when the screen width is reduced below a certain pixel size. Initially, everything was working fine as intended. However, after making some modifications to the carou ...

Guide to highlighting a particular letter in a string variable using JavaScript and AngularJS

Looking to highlight specific letters within a string variable. I have the string stored as an AngularJS variable in a table like this: <td>{{variable}}<td> In my JavaScript file, I am passing a variable for Angular that contains the string & ...