Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select options overlapping an absolute positioned DIV?

I have an absolute positioned div which works like a tooltip. On mouse over of an element it shows and then hides at mouse out. I have few <select> elements in the page which is placed above the tooltip element. In normal case the tooltip div will appear over the select tag. But when a user clicks on select tag and the options are shown, it overlaps the tooltip. Giving a higher z-index for select or options tag did not work.

enter image description here

Here is a sample code to illustrate the problem.

<body>
<h1>Select Options Overlapping Absolute Positioned DIV</h1>

<select name="some-name">
    <option>United States</option>
    <option>Canada</option>
    <option>Mexico</option>
    <option>Japan</option>
</select>

<div id="top-layer">
   <h2>Overlapping Div</h2>
</div>
</body>

CSS

select, options{ z-index:10;}

#top-layer { 
   background:#ccc; 
   color:#000; 
   border:solid 1px #666; 
   position:absolute; 
   top:45px; 
   left:70px; 
   z-index:100;
}
like image 214
Sandeep Paul Avatar asked Nov 18 '11 19:11

Sandeep Paul


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 I div overlap another div?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

Why is my div 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.


1 Answers

No element will apply the z-index value without also having a position attribute (absolute, relative, fixed, etc).

Try adding position:relative to your select so that it inherits a z-index value.

See this screencast for a more in depth explanation.

like image 96
Damon Bauer Avatar answered Oct 20 '22 00:10

Damon Bauer