【发布时间】:2016-12-18 04:11:28
【问题描述】:
我成功创建了一个响应式网格,如 sn-p 所示。
目前,我看到方框向左对齐。
如何在不使用填充的情况下将容器放在页面中心?
我尝试使用margin:auto,但没有成功。
.container{
display:flex;
flex-wrap:wrap;
text-align:center;
margin:auto;
}
.box{
width:100%;
max-width:30%;
border:1px solid red;
margin:5px;
}
@media only screen and ( max-width:768px ){
.box{
width:100%;
max-width:40%;
border:1px solid red;
}
}
<section class="container">
<div class="box">
<h1>This is the first box</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci voluptates, aut totam eaque possimus molestiae atque odio vero optio porro magni, quis culpa ea. Perferendis, neque, enim. Rem, quia, quisquam.</p>
</div>
</section>
【问题讨论】:
-
为你的容器类添加宽度
-
要使用
margin:auto,您的容器必须有width。 -
我在容器中添加了 100% 的宽度,但没有效果。这里是我的 sn-p:jsfiddle.net/cx1svoky/3
-
以 px 为单位给出宽度,例如 500px
-
@chirag。我实际上尝试了 1000px,但它并没有真正居中。
标签: css html alignment flexbox