Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent fixed positioned element overlapping others elements in css?

Tags:

html

css

Say I have three divs:

<div id="outer"></div>   <div id="one"></div>   <div id="two"></div>   <div id="three"></div> </div>  #one {   position:fixed;   top:0px;   left:0px; }  #two {   width: 80%;   height:500px; }  #three {   width: 80%;   height:500px; } 

Divs "two" and "three" appear to be overlapped by div "one" because of position fixed.

1) What is the best way to make it such that they do not overlap?

2) What is the best way to make it such that my fixed div takes up 100% of the height, even if the user scrolls down? (like a sidebar, preventing any new divs that i want to run along the same side as divs two and three) Does the best way involve floats for #two and #three?

like image 365
Rolando Avatar asked Jun 05 '13 21:06

Rolando


People also ask

How do you control overlap in CSS?

If you need more precise overlap control for multiple elements, assign specific z-index values to your elements, the higher the value the more on top the element will be. You can also use minus values.

Why are my CSS elements overlapping?

This could happen for several reasons, especially when you consider the position problems with divs from one website to another. Similarly, box elements may overlap for a few reasons, ranging from positioning errors to overflow issues and simple float problems.


2 Answers

By changing the position from position: fixed; to position: sticky; solved my problem.

#one{  position: sticky;     } 
like image 94
Chetan J Rao Avatar answered Oct 06 '22 12:10

Chetan J Rao


1:

By adding margin-left you can make sure that long fixed div doesn't overlap the others.

#two, #three {     margin-left:20%;     width:80%;     height:500px; } 

2:

Adding the bottom:0px; makes it the height of the window because it expands from the top to the bottom.

#one {         position:fixed;     top:0px;     bottom:0px;     left:0px;        width:20%;  } 

Note: I also added a flexible width of 20% to the #one div so that it plays nicely with the other two flexible width divs.

Fiddle: http://jsfiddle.net/ZPRLd/

like image 34
iambriansreed Avatar answered Oct 06 '22 13:10

iambriansreed