我正在尝试使用div创建一个2×2网格.一些div可能包含一个图像,但它可能会被设置为背景,选项background-size:cover.
这是我创建的笔:http://codepen.io/qarlo/pen/vLEprq
.container { display: flex; flex-wrap: wrap; justify-content: space-between; margin: auto; max-width: 960px; width: 80%; } .container__item { align-content: center; border: 1px solid #333; display: flex; flex-basis: 1; font-size: 3em; justify-content: center; height: 100%; margin-bottom: 1em; min-height: 300px; width: 47%; }
@H_404_10@
最佳答案要保持项目长宽比,一种非常简单的方法是使用CSS Viewport units我修改了你的笔,看看这个单位是如何工作的:http://codepen.io/vladbicu/pen/wMBmOb
.container { display: flex; flex-wrap: wrap; justify-content: space-between; margin: auto; max-width: 960px; width: 80%; } .container__item { align-content: center; border: 1px solid #333; display: flex; flex-basis: 1; font-size: 3em; justify-content: center; margin-bottom: 1em; // maintain aspect ratio width: 30vw; height: 30vw; }
@H_404_10@希望能帮助到你.