Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have "margin:auto" and "margin-left:offset" working together?

Tags:

I have a container on my test site:

#container {   margin: 0 auto; } 

Then I added the left vertical menu and on some small screens that menu is not fully visible.
Like my old laptop :-)

I want to keep the margin:auto setting in place but I want to move the whole #container a little bit to the right.

Could it be done some how?

I have tried #container {margin-left:10px;}, but to no avail.

like image 371
Radek Avatar asked Jun 04 '10 01:06

Radek


People also ask

Can we align an element by setting margin-left and margin-right?

Can we align an element by setting margin-left and margin-right? A Yes it's possible.

What happens when margin is set to auto?

The auto Value You can set the margin property to auto to horizontally center the element within its container. The element will then take up the specified width, and the remaining space will be split equally between the left and right margins.

What is the difference between margin-left and left?

Left is the position of your entire element, margin-left is the amount of your left margin.

How do you margin-left auto?

margin-left: auto; The auto keyword will give the left side a share of the remaining space. When combined with margin-right: auto , it will center the element, if a fixed width is defined.


2 Answers

Playing with firebug, it's good to use:

#container {  margin: 0 auto;  position:relative;  left:10px; } 

Hope it solves...

like image 110
Zuul Avatar answered Jan 03 '23 01:01

Zuul


The simplest approach would be to introduce another element (or style another element if it's already available). Thus, you might have:

<div style="margin-left: 10px;">    <div id="container" style="margin: auto;">...</div> </div> 

That way the centering is being done within a container div that's already got the appropriate left-hand padding.

like image 36
VoteyDisciple Avatar answered Jan 03 '23 00:01

VoteyDisciple