Tips for preventing the overlap of two divs positioned absolutely within a div that is positioned relatively

I have a code snippet that works perfectly when there is enough space to display all the divs without overlapping. However, when I resize the page to its minimum, the two absolute positioned divs start to overlap. Is there a way to prevent this from happening?

#div-chatroom {
  position: relative;
  height: calc(100% - 70px);
  /* IE9+ and future browsers */
  height: -moz-calc(100% - 70px);
  /* Firefox */
  height: -webkit-calc(100% - 70px);
  /* Chrome, Safari */
  padding: 0;
  text-align: center;
  margin: 0;
  border-right: 2px solid #333333;
  overflow: auto;
}

#div-messages {
  position: absolute;
  top: 10px;
  bottom: 110px;
  left: 10px;
  right: 10px;
  min-height: 200px;
  overflow: auto;
}

#div-sending {
  position: absolute;
  bottom: 10px;
  left: 10px;
  right: 10px;
  height: 100px;
}
<div id="div-chatroom">
  <div id="div-messages">messages here</div>
  <div id="div-sending">sending tools here</div>
</div>

UPDATE #1
I've added the code to JSFiddle. Despite trying different solutions, it seems like the issue persists if both divs are set with position: absolute.

Answer №1

Great news! I managed to achieve the same outcome by adjusting my approach.

CSS (JSFiddle):

#div-chatroom {
    position: relative;
    height: calc(100% - 70px); /* IE9+ and future browsers */
    height: -moz-calc(100% - 70px); /* Firefox */
    height: -webkit-calc(100% - 70px); /* Chrome, Safari */
    padding: 0;
    text-align: center;
    margin: 0;
    border-right: 2px solid #333333;
    background-color: #ffffff;
    overflow: auto;
}

#div-messages {
    position: relative;
    margin: 0;
    padding: 0;    
    min-height: 200px;
    height: calc(100% - 100px); /* IE9+ and future browsers */
    height: -moz-calc(100% - 100px); /* Firefox */
    height: -webkit-calc(100% - 100px); /* Chrome, Safari */
    background-color: green;
    overflow: auto;
}

#div-sending {
    position: relative;
    margin: 0;
    padding: 0;    
    height: 100px;
    background-color: red;
}

Answer №2

One of the main intentions behind using position:absolute is to allow an element to be independent from all other elements on the page (excluding its parent). This makes it impossible to achieve the desired alignment in this case. If you wish to align them correctly, consider using a different positioning style instead of absolute. Share your code on jsfiddle so that we can offer a suitable solution.

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

Customize the appearance of an ASP link button within a navigation menu

Just getting started with ASP.net and CSS, I want to customize my link button using CSS similar to other <a href> buttons, but I seem to be running into some issues. This is the code for my menu: <div id="templatemo_menu"> <ul ...

The meter.value update feature is functioning properly, however, it does not refresh the displayed "hover" value

When using JavaScript to update a meter's value, I encountered an issue: var LFRMSMeter = document.getElementById("LFRMSMeter"); LFRMSMeter.value = parseFloat(j[0]); Although the updating of the value works fine, hovering over the meter displays the ...

How can I navigate to a specific section within a Bootstrap modal without being redirected back to the origin of the modal?

Currently in the process of creating a basic front end website with Bootstrap for styling. One section I've developed includes modals, where users click a button to open a centrally located modal window. Desired Functionality I aim to enable users t ...

Tips for adding a border to an element when hovered over

I am looking to achieve a hover effect that displays a border around an element without actually adding the border to the element itself, as shown in this image: https://i.sstatic.net/iFpCA.png The challenge I'm facing is that I want to allow users ...

Is it possible to utilize the onClick event while preserving the use of "this" within the function it triggers?

There is a project with specific needs that require implementation. The task involves adding points to the total score when a button is clicked, without making any changes to the provided HTML code. This requires writing unobtrusive JavaScript code by atta ...

"Removing the blue initial loader from Filestack during initialization: A step-by-step guide

Currently, I am facing an issue with a button that triggers the filestack uploader in my React application. The functionality works as expected, but I am struggling to override the default blue spinner/loader that is displayed with the filestack uploader. ...

Creating a distinctive appearance for JavaScript's default dialogue box

Is there a way to enhance the design of my code that prompts the user for input using JavaScript's `prompt`? Currently, it appears too simplistic. Are there any CSS or alternative methods to improve its appearance? function textPrompt(){ var text = ...

Trouble with DevExpress ASPxButton: Some Properties Fail to Update

It's been a while and I'm facing a tough challenge. I need to change the image source of an ASPxButton from the code behind, but it seems like the height, width, and border style are getting lost in the process (resulting in an image with its own ...

Slider that allows range selection after midday

Currently facing a dilemma using the noUiSlider plugin. I have set up a time range picker starting from 6 am to 6 am the following day. Everything works smoothly until it reaches 23:59, but I need it to display 1:00, 2:00 instead of 25:00, 26:00 for the ...

CSS: Adjusting alignment differences between local host and server

I've been working on a webpage with rotating background images, and everything looked perfect on my localhost. But once I uploaded it to the server and viewed it in different browsers, some of the people in the images were not positioned as intended, ...

What is the best way to include a div element with a dynamic animation on a webpage?

I'm attempting to create a laser beam that can shoot enemies on the screen, much like in classic games such as Space Invaders or Galaga. However, I am encountering difficulties getting the laser to move when I click the button. Below is the code I hav ...

The html content is failing to refresh during an ajax request in Wordpress and Woocommerce

Every time I try to filter products, the AJAX function only seems to work on the first attempt. After that, the HTML does not update as expected. https://gyazo.com/6acc69cdda75fdc737513ff92645c2cb Below is the AJAX response when I try to filter the produc ...

What is the best way to access bootstrap col and row styles without needing to import the entire framework?

I am currently in the process of developing a react app using styled-components and I am looking to design a component that mimics the functionality of the col col-x classes from bootstrap. Additionally, I also want to create another component for the row ...

Showcasing a dynamic image as a task is being completed within a JavaScript function

Is there a way to show a loading image while waiting for a JavaScript function to finish? <script type="text/javascript"> function create() { //Perform operation } </script> I am looking for a solution to display a loading image until ...

Struggling to make an AJAX form function properly

I'm running into an issue while setting up a basic AJAX form. My goal is to have a message display on the same page upon successful login using a PHP form through AJAX, but currently, I get redirected to the PHP file after form submission. Can anyone ...

Adding hash history to a sortable showcase: A step-by-step guide

I am in the process of developing a filterable portfolio feature for my website. Once a user clicks on a filter, it adds a hashtag to the end of the URL. For example, clicking on 'design' would result in www.yourdomain.com/#design. I am wonderin ...

activating a button following the selection of a checkbox

My code includes a submit button that is currently disabled. I am looking to enable it when the user clicks on the "I agree" checkbox using jQuery. Can someone assist me with this? My code is written in HTML, JavaScript, and jQuery. <input id="agree" ...

JavaScript Bug Report

Currently, I am immersed in a project that encompasses various languages like HTML, JS, and PHP. While working on one of the PHP functions, I stumbled upon an unexpected anomaly. To dissect it better, I decided to break it down into simpler functions: &l ...

Arranging images in a row with the help of :before and :after styling

Looking to use the pseudo selectors :before and :after to create a border around a div with images, but running into some issues. The :after image is not positioning correctly on the right side of the element and the text is dropping below the image. Check ...

The function given to requestAnimationFrame is not being triggered as expected

I have successfully implemented an infinite loop animation using setInterval. However, I want to enhance the performance by switching to requestAnimationFrame(). Unfortunately, the function supplied to requestAnimationFrame() is not being called for some r ...