Reputation: 9762
So what I'm trying to accomplish is to have a div centered on the page (margin: auto auto;
) with a navigation div
just to the left of it.
The idea being the navigation div
can be switched on or off (so may or may not be there). If its there or not should not interferer with the centering of the main div.
Below is an example
I've tried a few things
Wrapping both divs with a main div. Setting the main div to margin: auto auto
and then setting both child divs to float: left
. The problem is that when the nav div dissapears the entire thing shifts left.
Keeping the middle div margin: auto auto;
floating the nav div left and then using margin-left
but this changes when the page gets bigger or smaller.
Any pointers would be appreciated in the best way to do this. I was hoping to avoid tables.
Upvotes: 3
Views: 133
Reputation: 56
Try this:
In your html:
<body>
<div class="encasing">
<div class="leftmenu"></div>
</div>
</body>
In your css:
html, body
{
width: 100%;
height: 100%;
}
div.encasing
{
top: 50px;
margin-left: auto;
margin-right: auto;
width: 70%;
height: 500px;
background-color: green;
position: relative;
}
div.leftmenu
{
right: 100%;
width: 10%;
height: 300px;
background-color: red;
position: absolute;
}
The important parts are:
The idea here is to make the left menu use the position of the center block and then adjust itself. Right: 100% will put the right edge of the menu on the left edge of the menu.
In the end, a really good trick in css is that absolute positioned elements adjust themselves relative the the nearest relative or absolute positioned parent. :)
Upvotes: 2
Reputation: 20706
A few solutions I can think of:
display: inline-block
on the three columns. The difference is in how small windows are handled (try it out). Again, you can counter undesired effects by setting a min-width on the body.@media
queries to adjust your layout to other screen resolutions.You should also try to find a site that does what you want to do, and see how they did it (inspect HTML, CSS, and maybe Javascript).
Upvotes: 0