【发布时间】:2021-12-20 00:34:10
【问题描述】:
我想知道是否可以扩展一个 div,该 div 位于中心,仅在添加新元素时从下方使用 justify-content: center; 和 align-items: center;。
const items = document.getElementById('items');
const add = () => {
const newItem = document.createElement('div');
newItem.innerText = 'Item';
items.append(newItem);
}
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
#body{
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
}
#container{
background-color: whitesmoke;
min-height: 200px;
width: 200px;
}
<div id="body">
<div id="container">
<button onClick='add()'>Add item</button>
<div id="items"></div>
</div>
</div>
如您所见,元素使其从上方和下方扩展。我想做的只是从下面展开。
【问题讨论】:
-
移除 justify-content 和 align-items 然后应用 margin:auto 到子元素