如何使flex
子不重疊頂部填充?Flexbox。對齊項目中心不忽略填充
在這個例子中,兒童.large
居中居中,但由於身高較大,其頂部位於其父母的頂部邊界之上。有沒有辦法來防止這種情況,並使.large
後.flex
填充頂部沒有JS? flex-start
將是一個不好的解決方案,因爲.flex
內的塊可以有很小的高度,並且必須位於.flex
的中心。 .flex
必須定位爲absolute
或fixed
。
https://jsfiddle.net/zoxamy9f/1/
.large {
background: red;
height: 200%;
flex: 1;
}
html, body {
height: 100%;
overflow: hidden;
}
.flex {
position: absolute;
left: 0;
top: 0;
width: 100%;
padding-top: 10%;
height: 100%;
background: black;
display: flex;
align-items: center;
justify-content: center;
overflow: auto;
}
<div class="flex">
<div class="large"></div>
</div>
也許我不理解不夠好,但基本上,如果你有一個以%表示的寬度和高度,你不能在%使用填充。 嘗試在'px'中給寬度和高度一個固定值。 –