html - 响应式div的 float 和居中

标签 html css centering

现在我已经将 float:left 放在我的绿色 div 上,但这使它们粘在左边(有道理)。现在,如何在调整蓝色容器大小时使 div 居中?

如图所示:

enter image description here

蓝色:父容器 绿色:分区

最佳答案

您可以在子元素上使用display: inline-block,在父元素上使用text-align: center

<div class="container">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>
.container {
    background-color: blue;
    width:500px;
    text-align: center;
}

.item {
    display: inline-block;
    background-color: green;
    width: 100px;
    height: 100px;
}

https://jsfiddle.net/g3vp2fyf/

关于html - 响应式div的 float 和居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29783438/

相关文章:

c - 如何在 C 中打印居中对齐的字符串

javascript - jQuery 触发器没有按预期工作

css - Flexbox、最小高度、边距自动和 Internet Explorer

css - 无法在页面上水平居中元素

html - 没有内容高度设置问题的纯 css 垂直选项卡

php - Wordpress 添加高亮类以使用 php 链接

css - 无法使用 CSS 将 3 张相邻照片居中

html - 我怎样才能得到一个结构良好的 CSS 样式的 html 配置文件?

css:div定位(2个多div的列(左-右)+居中div)

javascript - 谷歌图表 : get rid of line offset?