【发布时间】:2016-05-25 08:37:19
【问题描述】:
这是关于在 1 个 div 中嵌套 4 个 div(正方形),这是 4 个 div 的一部分 ...
我在包装器中使用 display: flex 并用于被包装的物品本身,否则它将不起作用
对我来说,这感觉像是一个糟糕的 hack,但我尽量不使用 float。你会如何处理这个话题?
这是JSBIN上的演示。
这是 HTML 代码:
<body>
<div>
<div id="DIV">
<div id="div1_inside_div" style="border:none">
<div id="div2_inside_div1" style="border: none;">
<div id="div3_inside_div2" style="border: none;">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
</div>
</body>
这是 CSS 代码:
body {
width: 90%;
height: 90%;
margin-left:auto;
margin-right:auto
}
/* DIV */
#DIV {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
#DIV > div{
box-sizing: border-box;
height: 220px;
display: flex;
justify-content: center;
align-items: center;
min-width: 50%;
min-height: 50%;
background-color: lightgreen;
border: dashed 1px gray;
}
/* DIV1 INSIDE DIV */
#div1_inside_div {
flex-wrap: wrap
}
#div1_inside_div div {
box-sizing: border-box;
margin: 0px;
height: 50%;
display: flex;
justify-content: center;
align-items: center;
min-width: 50%;
background-color: yellow;
border: dashed 1px black;
}
/* DIV2 INSIDE DIV1 */
#div2_inside_div1 {
flex-wrap: wrap;
}
#div2_inside_div1 div {
box-sizing: border-box;
margin: 0px;
height: 50%;
display: flex;
justify-content: center;
align-items: center;
min-width: 50%;
background-color: pink;
border: dashed 1px black;
}
/* DIV3 INSIDE DIV2 */
#div3_inside_div2 {
flex-wrap: wrap;
}
#div3_inside_div2 div {
box-sizing: border-box;
margin: 0px;
height: 50%;
display: flex;
justify-content: center;
align-items: center;
min-width: 50%;
background-color: lightblue;
border: dashed 1px blue;
}
谢谢! J
【问题讨论】:
-
看起来不错。不错的工作。那么有什么问题呢?
-
感谢 Michael_B。对我来说,这似乎是一种黑客行为。由于在过去两周内我只做了几个晚上的 HTML 和 CSS,我不确定该解决方案是否合适。 ..但是根据您的反馈,似乎还可以。很高兴知道。贝斯特,J。
-
看起来不错。但是现在您已经完成了问题,我们可以查看代码。
-
代码在 JSbin 中也应该可见。
-
对,但根据 SO 准则,代码也应该发布在问题中。如果有一天你的 jsbin 链接失效了怎么办?那么这个问题就变得毫无参考价值了。
标签: html css alignment flexbox