基本CSS问题 - 父DIV与未知的子DIVS,居中的父DIV [英] Basic CSS problem - parent DIV with unknown child DIVS, centered parent DIV

查看:135
本文介绍了基本CSS问题 - 父DIV与未知的子DIVS,居中的父DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我对中一个DIV我会给它一个宽度,并把 margin:auto 上,但我有两个问题这种情况。

When i'm centering a DIV i will give it a width and put margin:auto on it, however i have two problems with this situation.


  1. 这个父DIV中有2或3个DIV,float:left。子DIVS的宽度不会改变,但数字可能为(2或3)。

  1. There are going to be either 2 or 3 DIVS inside this parent DIV with float:left on them. The width of the child DIVS won't change but the number might (2 or 3).

因此这意味着我不能在父DIV通过不知道内部的DIV的数量。

This therefore means i cant put a width on the parent DIV through not knowing the number of DIVs inside.

样本标记:

<div class="parent">
 <div class="child"></div>
 <div class="child"></div>
</div>

<div class="parent">
 <div class="child"></div>
 <div class="child"></div>
 <div class="child"></div>
</div>

总而言之,我需要让父DIV以浮动左边的DIV为中心。

So in summary, i need to have the parent DIV centered with the DIVs inside floated left.

推荐答案

请参阅: http ://jsfiddle.net/qk4dW/

.wrapper {
    text-align:center;
}
.parent,.child {
    display:inline-block;
}

<div class="wrapper">
    <div class="parent">
        <div class="child"></div>
        <div class="child"></div>
        <div class="child"></div>
    </div>
</div>

它应该可以工作。

这篇关于基本CSS问题 - 父DIV与未知的子DIVS,居中的父DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆