As the browser window is resized, HTML div elements will stack vertically on top of one another

I haven't dedicated much time to the UI aspect of my project. I have a main div with two nested divs. These two divs initially appear side by side, but when I resize the window, they stack on top of each other. My goal is for these divs to remain next to each other even when the window is resized, and for the browser window to display a horizontal scrollbar if needed to view content.

Despite trying different methods, I have not been able to achieve this desired layout.

body {
  background-color: white;
}

#mainContainer {
  overflow: hidden
}

#firstBox {
  background-color: gray;
  margin: 10px;
  padding: 10px;
  overflow-x: auto;
  width: 45%;
  height: 30%;
  float: left;
  display: inline-block;
}

#secondBox {
  background-color: gray;
  margin: 10px;
  padding: 10px;
  overflow: auto;
  width: 45%;
  height: 30%;
  float: left;
  display: inline-block;
  position: fixed;
}
<div id="mainContainer">
  <div id="firstBox" class="gridContainer">
    Lorem ipsum dolor sit amet, nullam eros posuere quam, non ipsum ornare vehicula. In integer, ac tempus aenean lorem nec amet ac, non blandit class, eget odio eu, mollis mauris massa. In augue erat convallis, morbi lectus lobortis tempor in lorem. Dis
    et morbi consectetuer non, tempor pretium. Leo dolor erat orci, lobortis adipisicing scelerisque integer diam lorem, tempus non, dolor libero vivamus velit. Viverra ipsum quam id in arcu, ligula tortor laoreet fermentum sit ipsum. Erat dapibus nulla
    justo vitae, feugiat nullam felis sollicitudin, dolor wisi mauris, tristique tempus nunc tristique. Velit non, duis auctor a. Ipsum pede maecenas vestibulum id ornare, ac facilisi wisi maecenas sem ultrices, non pede cupiditate mollis lorem condimentum,
    suscipit integer etiam mauris amet. Magna rutrum cras, tincidunt lacus ut, lectus donec
  </div>

  <div id="secondBox" class="gridContainer">
    Lorem ipsum dolor sit amet, nullam eros posuere quam, non ipsum ornare vehicula. In integer, ac tempus aenean lorem nec amet ac, non blandit class, eget odio eu, mollis mauris massa. In augue erat convallis, morbi lectus lobortis tempor in lorem. Dis
    et morbi consectetuer non, tempor pretium. Leo dolor erat orci, lobortis adipisicing scelerisque integer diam lorem, tempus non, dolor libero vivamus velit. Viverra ipsum quam id in arcu, ligula tortor laoreet fermentum sit ipsum. Erat dapibus nulla
    justo vitae, feugiat nullam felis sollicitudin, dolor wisi mauris, tristique tempus nunc tristique. Velit non, duis auctor a. Ipsum pede maecenas vestibulum id ornare, ac facilisi wisi maecenas sem ultrices, non pede cupiditate mollis lorem condimentum,
    suscipit integer etiam mauris amet. Magna rutrum cras, tincidunt lacus ut, lectus donec,
  </div>
</div>

Answer №1

Everything seems to be running smoothly with the code as it is now, without any stacking issues that I noticed. Can you pinpoint when exactly it started stacking one below the other?

Answer №2

To improve the layout, make sure to add width to both the container and child elements.

#mainContainer
{
    width:1490px;
    height:auto; 
    overflow-x: auto;
}   
#firstBox {
    background-color: gray;
    margin: 10px;
    padding: 10px;   
    width: 700px;
    height: auto;  
    float: left;
    
}

#secondBox {
    background-color: gray;
    margin: 10px;
    padding: 10px;
    width: 700px;
    height: auto;
    left:750px;
    float: left;
   
}
    <div id="mainContainer">
    <div id="firstBox" class="gridContainer">
            Lorem ipsum dolor sit amet, nullam eros posuere quam, non ipsum
            ornare vehicula. In integer, ac tempus aenean lorem nec amet ac, non
            blandit class, eget odio eu, mollis mauris massa. In augue erat
            convallis, morbi lectus lobortis tempor in lorem. Dis et morbi
            consectetuer non, tempor pretium. Leo dolor erat orci, lobortis
            adipisicing scelerisque integer diam lorem, tempus non, dolor libero
            vivamus velit. Viverra ipsum quam id in arcu, ligula tortor laoreet
            fermentum sit ipsum. Erat dapibus nulla justo vitae, feugiat nullam
            felis sollicitudin, dolor wisi mauris, tristique tempus nunc
            tristique. Velit non, duis auctor a. Ipsum pede maecenas vestibulum
            id ornare, ac facilisi wisi maecenas sem ultrices, non pede
            cupiditate mollis lorem condimentum, suscipit integer etiam mauris
            amet. Magna rutrum cras, tincidunt lacus ut, lectus donec
    </div>

    <div id="secondBox" class="gridContainer">
            Lorem ipsum dolor sit amet, nullam eros posuere quam, non ipsum
            ornare vehicula. In integer, ac tempus aenean lorem nec amet ac, non
            blandit class, eget odio eu, mollis mauris massa. In augue erat
            convallis, morbi lectus lobortis tempor in lorem. Dis et morbi
            consectetuer non, tempor pretium. Leo dolor erat orci, lobortis
            adipisicing scelerisque integer diam lorem, tempus non, dolor libero
            vivamus velit. Viverra ipsum quam id in arcu, ligula tortor laoreet
            fermentum sit ipsum. Erat dapibus nulla justo vitae, feugiat nullam
            felis sollicitudin, dolor wisi mauris, tristique tempus nunc
            tristique. Velit non, duis auctor a. Ipsum pede maecenas vestibulum
            id ornare, ac facilisi wisi maecenas sem ultrices, non pede
            cupiditate mollis lorem condimentum, suscipit integer etiam mauris
            amet. Magna rutrum cras, tincidunt lacus ut, lectus donec,
    </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 control the positioning of a tooltip for an input element?

Currently, I am creating a filtering list that requires text input elements for each column due to limited space. Each input will initially display the name of the corresponding column and then clear when clicked by the user. To help users remember which ...

steering clear of iframes, yet incorporating certain iframe-like functionalities in Rails

One of my sites is my primary site, while the other serves as a help site with documentation. The main site is built using Rails, whereas the secondary one is just a basic WordPress blog. Right now, I have it embedded into the main site using an iframe. ...

Completing a form and reloading the page without including the reload in the browsing history

I am currently developing a compact web application using Flask, and I have encountered a challenging issue with navigating back to the previous page. Here are some approaches I have experimented with: Avoiding navigation away from the page - content ...

Increased wait time during initial execution

Currently facing an issue with delaying the first run of a function. I've developed a basic slideshow that is causing problems due to this delay in the initial run. My goal is to have the first run wait for 10 seconds and then maintain a 4-second del ...

The CSS module is disappearing before the Framer Motion exit animation finishes when the path changes

Source Code Repository: https://github.com/qcaodigital/cocktail_curations Live Site: I recently deployed my NextJS project to Netlify and everything seems to be working fine, except for one issue. Each page has an exit animation (currently set to change ...

Tips for verifying the status of an input element following the dynamic addition of a required element

I have implemented a required field for all input elements in a specific state using the following code: for (var i = 0; i < data.length; i++) { // activate required function v = data[i].Required; if (v) document.ge ...

Can Bootstrap be configured to use a single resolution exclusively?

Uncertain about the feasibility, I am initiating this inquiry. Can Bootstrap be utilized solely with one resolution (Medium desktop)? This question arises from the fact that the design I possess is catered towards Medium desktop (970px wide). My intentio ...

Shopping cart feature in PHP - Ajax response only functioning once

I'm in the process of developing a shopping cart. I start by adding products to the cart, then use Ajax to dynamically update the quantity when it changes. Everything works smoothly after the first change, but subsequent quantity updates do not refres ...

Learn how to smoothly transfer a URL from JavaScript to HTML and initiate the download process

I created a website that solely relies on HTML5, CSS and JavaScript to function. The core functionality of this website involves utilizing YouTube URLs. I have stored the URL of a specific YouTube video as a variable, now I am looking for a way to transfer ...

The slider's border-radius is not being maintained as it moves from left to right

We recently implemented a slider on our website, located at . While we added a border-radius to the slider, we noticed that when the images slide, the border-radius is slightly removed... I attempted to contain the parent element with overflow: hidden, bu ...

Ways to incorporate the X-shaped spinner hamburger menu into your design

Hello everyone, I am looking to create a toggle menu that switches between X and hamburger styles when clicked. I have shared my menu codes below but I'm not sure how to achieve this effect. If anyone can help, I would really appreciate it. Here are ...

Odoo XML Views: Implementing a vertical separator to distinguish between fields and their corresponding values

I am working with a view xml in Odoo V13 and I currently have the following code enclosed in div tags. My goal is to have a vertical separated pipe or line between the field name and its corresponding value. Here is the View: <group> <div> ...

What is the most effective way to obtain a customer's latitude and location by prompting them to drop a pin on Google Maps?

My Android app has an API, but on my website I'm wondering how I can retrieve a person's location by having them place a marker on a Google Map. Is there a standard method for this? I need to obtain the latitude and longitude coordinates and send ...

Using JQuery to target and style individual td elements within a database in a

Hey there! I'm currently in the process of learning jQuery and I've encountered a little issue with the code below: <table class="table table-bordered table-striped table-hover datatable"> <thead> <tr> & ...

Mirror the content of my div code onto a two-dimensional plane using an A-frame

Query I am currently developing a 3D scene using A-Frame () and I am in need of a solution to mirror an HTML div onto a plane within the A-Frame environment. For instance, imagine a scenario where there is a div at the bottom left corner of the screen fun ...

What is the best way to showcase JSON data while incorporating HTML tags in a React application?

My JSON data contains HTML tags interspersed within it and I have successfully fetched and displayed the data. See the code snippet below: componentDidMount() { this._callCake(); } _renderEachCake = () => { return ( <EachCake ...

Optimizing Performance by Loading Critical CSS in Next.js

Would it be possible to split critical CSS from non-critical in Next.js and load the non-critical asynchronously on each page? I have styled-components implemented in my pages and components along with a few external CSS libraries. Any suggestions or tip ...

Click on the link that surrounds an inline-block span in Internet Explorer

In Chrome and Firefox, the following code works fine and makes the container clickable. However, in Internet Explorer, only the inner div changes the cursor to indicate that it's clickable, not the span. I could use cursor:pointer to fix this issue, ...

Web Development using HTML and JavaScript

Here is the code for a search bar engine that I am working on. <form id="frmSearch" class="search1" method="get" action="default.html" /> <input class="search" id="txtSearch" type="text" name="search_bar" size="31" maxlength="255" value=" ...

Attempting to create a visually appealing gallery using a lightbox feature, but unfortunately, the images are not

Just starting out with Angular and I'm working on creating a gallery with a lightbox that pops up when an image is clicked. I've tried using examples with bootstrap and also attempted to code it myself. However, whenever I click on the image lin ...