中心子div在父div中

时间:2021-12-09 20:32:45

I have a parent div that must stay at 100% with 3 child divs inside. I need to center the 3 child divs, but don't know how.

我有一个父div,必须保持100%,里面有3个子div。我需要将3个孩子的中心集中,但不知道如何。

.parent
{
    width: 100%;    
    border: 1px solid blue;
}

.child
{
    float: left;    
    border: 1px solid red;
    margin: 2px;
}

<div class="parent">
    <div class="child">child1</div>
    <div class="child">child2 - center us child divs! :)</div>
    <div class="child">child3</div>
    <div style="clear: both"></div>
</div>

Here is some example code:

这是一些示例代码:

http://jsfiddle.net/qdHH3/2/

http://jsfiddle.net/qdHH3/2/

Thanks!

谢谢!

1 个解决方案

#1


24  

Try using display: inline-block and text-align: center

尝试使用display:inline-block和text-align:center

.parent {
    width: 100%;
    border: 1px solid blue;
    text-align: center;
}

.child {
    display: inline-block;  
    border: 1px solid red;
    margin: 2px;
}

jsFiddle: http://jsfiddle.net/qdHH3/3/

jsFiddle:http://jsfiddle.net/qdHH3/3/

#1


24  

Try using display: inline-block and text-align: center

尝试使用display:inline-block和text-align:center

.parent {
    width: 100%;
    border: 1px solid blue;
    text-align: center;
}

.child {
    display: inline-block;  
    border: 1px solid red;
    margin: 2px;
}

jsFiddle: http://jsfiddle.net/qdHH3/3/

jsFiddle:http://jsfiddle.net/qdHH3/3/