displayflex给第二个子元素设置居中只使用flex
可以使用justify-content属性来设置子元素在主轴上的对齐方式,使用align-items属性来设置子元素在交叉轴上的对齐方式。
如果要将第二个子元素设置为居中,可以将justify-content设置为center,将align-items设置为center。
HTML代码:
<div class="container">
<div>子元素1</div>
<div>子元素2</div>
</div>
CSS代码:
.container {
display: flex;
justify-content: center;
align-items: center;
}
这样就可以将第二个子元素居中显示了。
原文地址: https://www.cveoy.top/t/topic/ibkf 著作权归作者所有。请勿转载和采集!