I downloaded a template and edit it. now I want to center the one_third
elements but the
margin-left: auto;
margin-right: auto;
doesn't work.
This is the template in jsfiddle.
Can anybody help me?
I downloaded a template and edit it. now I want to center the one_third
elements but the
margin-left: auto;
margin-right: auto;
doesn't work.
This is the template in jsfiddle.
Can anybody help me?
For margin-left:auto;
and margin-right:auto;
to work, the element needs to have a width. So try something like:
margin-left: auto;
margin-right: auto;
width: 600px;
UPDATE:
Since you seem to alter your examples all the time, here is a very basic understanding of how display
and margins
work:
To have any box centered within another box, you need to do two things:
a) Give the outer box a width (otherwise the inner box doesn't know what to center against
b) The inner box needs to have margin: 0 auto
(this is the same as margin-left: auto; margin-right: auto;
)
Also, both boxes need to be block level elements (i.e. have display: block
, NOT display: inline;
)
So put together it will look like this:
HTML:
<section id="outer">
<section id="inner"></section>
</section>
CSS:
#outer {
width: 600px;
display: block;
border: 1px solid red;
}
#inner {
margin-left: auto;
margin-right: auto;
width: 200px;
height: 200px;
border: 1px solid green;
}
This will give you a green box of 200px by 200px centered within a red box of width 600px.
Now, if you want two elements side by side within the green box, you can add your inline elements there, i.e.
<section id="outer">
<section id="inner">
<article>Hi</article>
<article>Hi agian!</article>
</section>
</section>
#outer {
width: 600px;
display: block;
border: 1px solid red;
}
#inner {
margin-left: auto;
margin-right: auto;
width: 200px;
height: 200px;
border: 1px solid green;
}
#inner article {
display: inline;
}