I was trying to make concept for my website, I was creating a responsive menu bar, Everything worked fine until i scaled browser when elements started overlapping each other.
I don't know if it's even possible to stop overlapping with absolutely positioned elements, After big research i have found nothing useful, i have tried clear:both
as well.
HTML:
<div class="maincontainer">
<div id="block1" class="blocks">
</div>
<div id="block2" class="blocks">
</div>
</div>
CSS:
.maincontainer {
display: block;
background-color: black;
margin: 0 auto;
width: 100%;
min-width: 600px;
height: 250px;
}
body {
background-color: white;
}
.blocks {
background-color: red;
position: absolute;
display: inline-block;
width: 100px;
height: 50px;
}
#block1 {
background-color: green;
top: 100px;
left: 0;
right: 0;
bottom: 0;
margin-left: auto;
margin-right: auto;
}
#block2 {
top: 100px;
left: 0;
right: 25%;
bottom: 0;
margin-left: auto;
margin-right: auto;
}
I could have stopped the overlapping problem by using px
dimensions instead of %
, but since my main goal is to make a fully responsive menu, I am trying to stop their movement once they get close enough to overlap.
What could be the best way to stop this overlap? Can this be done by absolutely positioned elements at all? If not then what's the better way of doing it?
Note: I only want vertical aligning in this case, and i'm trying to stop overlapping in the specific case, where green block is fully in center and red block is left.
Please check Fiddle and Embedded result.