赞
踩
思路:
注意:不要忘了给父元素添加display:flex;,这是开启 flex 布局的钥匙
举例说明:
单行子元素实现水平垂直居中:
<div> <span>1</span> <span>2</span> <span>3</span> </div> <style type="text/css"> div{ display: flex; width: 800px; height: 400px; background-color: orange; justify-content: center; align-items: center; } div span{ width: 150px; height: 100px; background: skyblue; margin: 0 10px; } </style>
运行结果:
多行子元素实现水平垂直居中:
<div> <span>1</span> <span>2</span> <span>3</span> <span>4</span> <span>5</span> <span>6</span> </div> <style type="text/css"> div{ display: flex; width: 800px; height: 400px; background-color: orange; flex-wrap: wrap; justify-content: center; align-content: center; } div span{ width: 150px; height: 100px; background: skyblue; margin: 10px; } </style>
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。