且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CSS:一个< div>中的元素元件

更新时间:2022-04-27 06:00:58

>

设置text-align:center;到父div,和margin:auto;到子div。

Set text-align:center; to the parent div, and margin:auto; to the child div.

JsFiddle演示

HTML

<div id="parent">
    <div id="child1" class="block center">a block to align center and with text aligned left</div>
    <div id="child2" class="block left">a block to align left and with text aligned left</div>
    <div id="child3" class="block right">a block to align right and with text aligned left</div>
</div>

CSS

#parent {
    text-align:center;
    background-color:blue;
    height:400px;
    width:600px;
}
.block {
    height:100px;
    width:200px;
    text-align:left;
}
.center {
    margin:auto;
    background-color:green;
}
.left {
    margin:auto auto auto 0;
    background-color:red;
}
.right {
    margin:auto 0 auto auto;
    background-color:yellow;
}

UPDATE

这是一个很好的资源,主要以任何内容为中心。

http://howtocenterincss.com/

This a good resource to center mostly anything.
http://howtocenterincss.com/