Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid overlapping between two divs positioning absolute inside a div positioning relative?

The following code works if the page has enough space to host all divs, but if I resize at minimum the page the two divs positioning absolute overlap. How can I avoid that?

#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
As required the code on JSFiddle (but if the two divs have position: absolute it doesn't seem to work).

like image 336
f_ficarola Avatar asked Jan 13 '14 01:01

f_ficarola


People also ask

How do you stop overlapping in absolute position?

You need to give your middle column position: relative; and negatively position them out of it (meaning in the html the left and right divs must go inside the middle div also). Absolutely positioned elements are removed from the document flow, which means they don't affect elements further down in the markup.

How do you stop divs from overlapping each other?

Just remove the min-width from your CSS! And give min-width to the container with margin: auto to make it center. Save this answer.

How do you keep the absolute position inside a div?

position : absolute Inside Other Positioned ElementsThe inner div element has position set to absolute and top to 0px and right to 0px . This places the inner div at the top right corner inside its parent element (because the parent element has position: relative set).

Can a div be position absolute and relative?

Clearly, nothing can be absolute and relative at the same time. Either you want the element to position itself based on another ancestor's position or based on the page flow.


1 Answers

Ok, I got an equivalent result by changing 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;
}
like image 160
f_ficarola Avatar answered Oct 10 '22 17:10

f_ficarola