首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Horizontally centering with "margin auto"

2012-11-19 
Horizontally centering with margin autoProblems:We want to keep the content displayed center hori

Horizontally centering with "margin auto"

Problems:

We want to keep the content displayed center horizontally in one container(div, p etc).

?

?

Solution:

We can use auto margin to center the element like below.

?

<div id="container">    <div id="center"><img src="./images/T1qjOGXaRqXXXXXXXX-72-34.png"/></div>?</div>

?

#container{    width:100%;}#center{    margin: 0 auto;/*Put the  img at the center of "container"*/    width:100px;?}

?

http://www.apple.com/support/ use this way to put the header, content and footer at the center.

?

Extension:

If we want to put content at the center, but with some pixels moved to one direction, we still can use this method with relative position.

?

?

<div id="container">    <div id="center"><img src="./images/T1qjOGXaRqXXXXXXXX-72-34.png"/></div>?</div>

?

#container{    width:100%;}#center{    margin: 0 auto;    width:100px;    position:relative;    left:-10px;/* move the position 10 pixels to the left*/}
?

An example of this extention would be tiger log in page (our current project)

Related Patterns:

Vertically Centering With Line-Height

热点排行