Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide a div if it overlaps another div

I have my main content in the center of the page about 900px wide. on a large screen there is enough space between the right margin of my content and the right side of the browser window that I can display a small 100x100px div in the bottom right corner and it looks good because there is no overlap between that div and the main content. When the screen size is less that div which is relatively positioned overlaps with the bottom right corner of my content. How can I set the display=none of the div if it comes within 20px of my content? Thanks

like image 656
user852974 Avatar asked Aug 02 '11 09:08

user852974


People also ask

How do you stop one div from overlapping?

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 ).

How do I hide a specific div?

To hide an element, set the style display property to “none”.

How do you hide a div and show another?

To show and hide div on mouse click using jQuery, use the toggle() method. On mouse click, the div is visible and on again clicking the div, it hides.

Why are my div tags 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

I'd go for a pure CSS solution here. Sounds like a perfect case for media queries:

#rightdiv {
    position: relative;
    width: 100px;
    height: 100px;
}

@media screen and (max-width: 1000px) {
    #rightdiv {
        display: none;
    }
}

That CSS will only display the #rightdiv element when the browser window size has at least 1000px width. If it gets smaller, it applies the display: none property.

Example: http://jsfiddle.net/7CCtH/

like image 155
jAndy Avatar answered Sep 27 '22 22:09

jAndy