我正在尝试使用具有不同宽度和高度的图像创建一个flexbox网格,当我在不同的屏幕尺寸上显示时,我想要表现出响应。我现在有网格,但我不知道如何让它响应。我试图在bootstrap中将响应类应用到当前布局但没有任何运气。我也尝试过砌体,但它没有解决我的问题。
所以问题是,我如何使这个响应,甚至是正确的方法?
HTML
%
CSS
<div class="flex_container">
<div class="flex_group__1">
<img src="holder.js/460x670" />
<img src="holder.js/460x408" />
</div>
<div class="flex_group__2">
<img src="holder.js/645x813">
<img src="holder.js/645x265">
</div>
<div class="flex_group__3">
<img src="holder.js/808x330"/>
<div class="flex_group__3_inner_bottom">
<img src="holder.js/452x748"/>
<img src="holder.js/356x748"/>
</div>
</div>
</div>
答案 0 :(得分:3)
我正在研究一个类似的项目,并找到了一个很好的例子,并根据我的需要对其进行了修改,所以这里是代码:
jsfiddle: https://jsfiddle.net/cukkjvtx/
HTML:
<title>Responsive layout</title>
<style type="text/css">
.columns.box {
padding:5px;
}
.box-wrap {
}
.height-1 {
height:auto;
}
.height-2 {
height:auto;
}
</style>
<body>
<div class="outer-wrap">
<div class="row">
<h1 class="metroslide-title">Welcome</h1>
<p class="metroslide-desc">Lorem ipsum dolor sit amet.</p>
</div>
<div class="box-outer-wrap row">
<div class="columns large-5">
<div class="row">
<div class="columns small-12 box">
<div class="box-wrap height-2 flag"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=540&q=75&w=814" /></div>
</div>
</div>
<div class="row">
<div class="columns small-6 box"><div class="box-wrap"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=260&q=75&w=396" /></div></div>
<div class="columns small-6 box"><div class="box-wrap"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=260&q=75&w=396" /></div></div>
</div>
</div>
<div class="columns large-7">
<div class="row">
<div class="columns large-5">
<div class="row">
<div class="columns large-12 box">
<div class="box-wrap"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=390&q=75&w=699" /></div>
</div>
<div class="columns large-12 box">
<div class="box-wrap height-2"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=810&q=75&w=699" /></div>
</div>
</div>
</div>
<div class="columns large-7">
<div class="row">
<div class="columns small-6 box"><div class="box-wrap"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=390&q=75&w=480" /></div></div>
<div class="columns small-6 box"><div class="box-wrap"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=390&q=75&w=480" /></div></div>
<div class="columns small-6 box"><div class="box-wrap"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=390&q=75&w=480" /></div></div>
<div class="columns small-6 box"><div class="box-wrap"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=390&q=75&w=480" /></div></div>
<div class="columns small-6 box"><div class="box-wrap"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=390&q=75&w=480" /></div></div>
<div class="columns small-6 box"><div class="box-wrap"><img src="https://unsplash.imgix.net/photo-1413787489051-bcbb6209ece1?fit=crop&fm=jpg&h=390&q=75&w=480" /></div></div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
CSS:
*,
*:before,
*:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box; }
html,
body {
font-size: 100%; }
body {
background: white;
color: #222222;
padding: 0;
margin: 0;
font-family: "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;
font-weight: normal;
font-style: normal;
line-height: 1;
position: relative; }
a:focus {
outline: none; }
img{
width:100%;
}
img,
object,
embed {
max-width: 100%;
height: auto; }
object,
embed {
height: 100%; }
img {
-ms-interpolation-mode: bicubic; }
#map_canvas img,
#map_canvas embed,
#map_canvas object,
.map_canvas img,
.map_canvas embed,
.map_canvas object {
max-width: none !important; }
.left {
float: left !important; }
.right {
float: right !important; }
.text-left {
text-align: left !important; }
.text-right {
text-align: right !important; }
.text-center {
text-align: center !important; }
.text-justify {
text-align: justify !important; }
.hide {
display: none; }
.antialiased {
-webkit-font-smoothing: antialiased; }
img {
display: inline-block;
vertical-align: middle; }
textarea {
height: auto;
min-height: 50px; }
select {
width: 100%; }
/* Grid HTML Classes */
.row {
width: 100%;
margin-left: auto;
margin-right: auto;
margin-top: 0;
margin-bottom: 0;
max-width: 62.5em;
*zoom: 1; }
.row:before, .row:after {
content: " ";
display: table; }
.row:after {
clear: both; }
.row.collapse .column,
.row.collapse .columns {
position: relative;
padding-left: 0;
padding-right: 0;
float: left; }
.row .row {
width: auto;
margin-left: -0.9375em;
margin-right: -0.9375em;
margin-top: 0;
margin-bottom: 0;
max-width: none;
*zoom: 1; }
.row .row:before, .row .row:after {
content: " ";
display: table; }
.row .row:after {
clear: both; }
.row .row.collapse {
width: auto;
margin: 0;
max-width: none;
*zoom: 1; }
.row .row.collapse:before, .row .row.collapse:after {
content: " ";
display: table; }
.row .row.collapse:after {
clear: both; }
.column,
.columns {
position: relative;
padding-left: 0.9375em;
padding-right: 0.9375em;
width: 100%;
float: left; }
@media only screen {
.column,
.columns {
position: relative;
padding-left: 0.9375em;
padding-right: 0.9375em;
float: left; }
.small-1 {
position: relative;
width: 8.33333%; }
.small-2 {
position: relative;
width: 16.66667%; }
.small-3 {
position: relative;
width: 25%; }
.small-4 {
position: relative;
width: 33.33333%; }
.small-5 {
position: relative;
width: 41.66667%; }
.small-6 {
position: relative;
width: 50%; }
.small-7 {
position: relative;
width: 58.33333%; }
.small-8 {
position: relative;
width: 66.66667%; }
.small-9 {
position: relative;
width: 75%; }
.small-10 {
position: relative;
width: 83.33333%; }
.small-11 {
position: relative;
width: 91.66667%; }
.small-12 {
position: relative;
width: 100%; }
.small-offset-1 {
position: relative;
margin-left: 8.33333%; }
.small-offset-2 {
position: relative;
margin-left: 16.66667%; }
.small-offset-3 {
position: relative;
margin-left: 25%; }
.small-offset-4 {
position: relative;
margin-left: 33.33333%; }
.small-offset-5 {
position: relative;
margin-left: 41.66667%; }
.small-offset-6 {
position: relative;
margin-left: 50%; }
.small-offset-7 {
position: relative;
margin-left: 58.33333%; }
.small-offset-8 {
position: relative;
margin-left: 66.66667%; }
.small-offset-9 {
position: relative;
margin-left: 75%; }
.small-offset-10 {
position: relative;
margin-left: 83.33333%; }
[class*="column"] + [class*="column"]:last-child {
float: right; }
[class*="column"] + [class*="column"].end {
float: left; }
.column.small-centered,
.columns.small-centered {
position: relative;
margin-left: auto;
margin-right: auto;
float: none !important; } }
/* Styles for screens that are atleast 768px; */
/*@media only screen and (min-width: 48em) {*/
@media only screen and (min-width: 601px) {
.large-1 {
position: relative;
width: 8.33333%; }
.large-2 {
position: relative;
width: 16.66667%; }
.large-3 {
position: relative;
width: 25%; }
.large-4 {
position: relative;
width: 33.33333%; }
.large-5 {
position: relative;
width: 41.66667%; }
.large-6 {
position: relative;
width: 50%; }
.large-7 {
position: relative;
width: 58.33333%; }
.large-8 {
position: relative;
width: 66.66667%; }
.large-9 {
position: relative;
width: 75%; }
.large-10 {
position: relative;
width: 83.33333%; }
.large-11 {
position: relative;
width: 91.66667%; }
.large-12 {
position: relative;
width: 100%; }
.row .large-offset-1 {
position: relative;
margin-left: 8.33333%; }
.row .large-offset-2 {
position: relative;
margin-left: 16.66667%; }
.row .large-offset-3 {
position: relative;
margin-left: 25%; }
.row .large-offset-4 {
position: relative;
margin-left: 33.33333%; }
.row .large-offset-5 {
position: relative;
margin-left: 41.66667%; }
.row .large-offset-6 {
position: relative;
margin-left: 50%; }
.row .large-offset-7 {
position: relative;
margin-left: 58.33333%; }
.row .large-offset-8 {
position: relative;
margin-left: 66.66667%; }
.row .large-offset-9 {
position: relative;
margin-left: 75%; }
.row .large-offset-10 {
position: relative;
margin-left: 83.33333%; }
.row .large-offset-11 {
position: relative;
margin-left: 91.66667%; }
.push-1 {
position: relative;
left: 8.33333%;
right: auto; }
.pull-1 {
position: relative;
right: 8.33333%;
left: auto; }
.push-2 {
position: relative;
left: 16.66667%;
right: auto; }
.pull-2 {
position: relative;
right: 16.66667%;
left: auto; }
.push-3 {
position: relative;
left: 25%;
right: auto; }
.pull-3 {
position: relative;
right: 25%;
left: auto; }
.push-4 {
position: relative;
left: 33.33333%;
right: auto; }
.pull-4 {
position: relative;
right: 33.33333%;
left: auto; }
.push-5 {
position: relative;
left: 41.66667%;
right: auto; }
.pull-5 {
position: relative;
right: 41.66667%;
left: auto; }
.push-6 {
position: relative;
left: 50%;
right: auto; }
.pull-6 {
position: relative;
right: 50%;
left: auto; }
.push-7 {
position: relative;
left: 58.33333%;
right: auto; }
.pull-7 {
position: relative;
right: 58.33333%;
left: auto; }
.push-8 {
position: relative;
left: 66.66667%;
right: auto; }
.pull-8 {
position: relative;
right: 66.66667%;
left: auto; }
.push-9 {
position: relative;
left: 75%;
right: auto; }
.pull-9 {
position: relative;
right: 75%;
left: auto; }
.push-10 {
position: relative;
left: 83.33333%;
right: auto; }
.pull-10 {
position: relative;
right: 83.33333%;
left: auto; }
.push-11 {
position: relative;
left: 91.66667%;
right: auto; }
.pull-11 {
position: relative;
right: 91.66667%;
left: auto; }
.small-push-1 {
left: inherit; }
.small-pull-1 {
right: inherit; }
.small-push-2 {
left: inherit; }
.small-pull-2 {
right: inherit; }
.small-push-3 {
left: inherit; }
.small-pull-3 {
right: inherit; }
.small-push-4 {
left: inherit; }
.small-pull-4 {
right: inherit; }
.small-push-5 {
left: inherit; }
.small-pull-5 {
right: inherit; }
.small-push-6 {
left: inherit; }
.small-pull-6 {
right: inherit; }
.small-push-7 {
left: inherit; }
.small-pull-7 {
right: inherit; }
.small-push-8 {
left: inherit; }
.small-pull-8 {
right: inherit; }
.small-push-9 {
left: inherit; }
.small-pull-9 {
right: inherit; }
.small-push-10 {
left: inherit; }
.small-pull-10 {
right: inherit; }
.small-push-11 {
left: inherit; }
.small-pull-11 {
right: inherit; }
.column.small-centered,
.columns.small-centered {
margin-left: 0;
margin-right: 0;
float: left !important; }
.column.large-centered,
.columns.large-centered {
position: relative;
margin-left: auto;
margin-right: auto;
float: none !important; } }
答案 1 :(得分:1)
这是一个布局更接近您要求的版本,更容易理解。
使用此功能,您可以在每个图像持有者上设置background-image
(就像我在下面的堆栈代码中所做的那样),或者在标记中添加img
元素。
html, body {
margin: 0;
}
.wrapper {
position: relative;
}
.wrapper:after {
padding-top: 56.25%; /* 16:9 ratio */
display: block;
content: ' ';
}
.flex-container {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
padding: 5px;
display: flex;
}
.flex-container > div { /* .flex-col-1, .flex-col-2, .flex-col-3 */
height: 100%;
display: flex;
flex-direction: column;
}
.flex-container > div > div { /* .flex-row-1, .flex-row-2 */
margin: 5px;
display: flex;
}
.flex-col-1 {
width: 25%;
}
.flex-col-1 .flex-row-1 {
height: 70%;
background: #ddd url(https://placeimg.com/400/600/arch) no-repeat center;
background-size: cover;
}
.flex-col-1 .flex-row-2 {
height: 30%;
background: #ddd url(https://placeimg.com/400/600/arch) no-repeat center;
background-size: cover;
}
.flex-col-2 {
width: 32.5%;
}
.flex-col-2 .flex-row-1 {
height: 80%;
background: #ddd url(https://placeimg.com/400/600/arch) no-repeat center;
background-size: cover;
}
.flex-col-2 .flex-row-2 {
height: 20%;
background: #ddd url(https://placeimg.com/400/600/arch) no-repeat center;
background-size: cover;
}
.flex-col-3 {
width: 42.5%;
}
.flex-col-3 .flex-row-1 {
height: 44%;
background: #ddd url(https://placeimg.com/400/600/arch) no-repeat center;
background-size: cover;
}
.flex-col-3 .flex-row-2 {
height: 56%;
}
.flex-col-3-inner {
flex: 1;
display: flex;
}
.flex-col-3-inner .flex-col-1-inner {
width: 40%;
margin-right: 5px;
background: #ddd url(https://placeimg.com/400/600/arch) no-repeat center;
background-size: cover;
}
.flex-col-3-inner .flex-col-2-inner {
width: 60%;
margin-left: 5px;
background: #ddd url(https://placeimg.com/400/600/arch) no-repeat center;
background-size: cover;
}
<div class="wrapper">
<div class="flex-container">
<div class="flex-col-1">
<div class="flex-row-1">
</div>
<div class="flex-row-2">
</div>
</div>
<div class="flex-col-2">
<div class="flex-row-1">
</div>
<div class="flex-row-2">
</div>
</div>
<div class="flex-col-3">
<div class="flex-row-1">
</div>
<div class="flex-row-2">
<div class="flex-col-3-inner">
<div class="flex-col-1-inner">
</div>
<div class="flex-col-2-inner">
</div>
</div>
</div>
</div>
</div>
</div>
2:nd示例,使用img
,我还添加了此规则
.flex-container img {
height: auto;
width: 100%;
}
正如您所看到的,使用这样的布局,img
元素需要更多的单独触摸才能使它们表现出来。
html, body {
margin: 0;
}
.wrapper {
position: relative;
}
.wrapper:after {
padding-top: 56.25%; /* 16:9 ratio */
display: block;
content: ' ';
}
.flex-container {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
padding: 5px;
display: flex;
}
.flex-container > div { /* .flex-col-1, .flex-col-2, .flex-col-3 */
height: 100%;
display: flex;
flex-direction: column;
}
.flex-container > div > div { /* .flex-row-1, .flex-row-2 */
margin: 5px;
display: flex;
}
.flex-container img {
height: auto;
width: 100%;
}
.flex-col-1 {
width: 25%;
}
.flex-col-1 .flex-row-1 {
height: 70%;
background: #ddd;
}
.flex-col-1 .flex-row-2 {
height: 30%;
background: #ddd;
}
.flex-col-2 {
width: 32.5%;
}
.flex-col-2 .flex-row-1 {
height: 80%;
background: #ddd;
}
.flex-col-2 .flex-row-2 {
height: 20%;
background: #ddd;
}
.flex-col-3 {
width: 42.5%;
}
.flex-col-3 .flex-row-1 {
height: 44%;
background: #ddd;
}
.flex-col-3 .flex-row-2 {
height: 56%;
}
.flex-col-3-inner {
flex: 1;
display: flex;
}
.flex-col-3-inner .flex-col-1-inner {
width: 40%;
margin-right: 5px;
background: #ddd;
}
.flex-col-3-inner .flex-col-2-inner {
width: 60%;
margin-left: 5px;
background: #ddd;
}
<div class="wrapper">
<div class="flex-container">
<div class="flex-col-1">
<div class="flex-row-1">
<img src="https://placeimg.com/400/600/arch" alt="">
</div>
<div class="flex-row-2">
<img src="https://placeimg.com/400/600/arch" alt="">
</div>
</div>
<div class="flex-col-2">
<div class="flex-row-1">
<img src="https://placeimg.com/400/600/arch" alt="">
</div>
<div class="flex-row-2">
<img src="https://placeimg.com/400/600/arch" alt="">
</div>
</div>
<div class="flex-col-3">
<div class="flex-row-1">
<img src="https://placeimg.com/400/600/arch" alt="">
</div>
<div class="flex-row-2">
<div class="flex-col-3-inner">
<div class="flex-col-1-inner">
<img src="https://placeimg.com/400/600/arch" alt="">
</div>
<div class="flex-col-2-inner">
<img src="https://placeimg.com/400/600/arch" alt="">
</div>
</div>
</div>
</div>
</div>
</div>
以下是另外两个变种,一个使用background-size: contain;
和另一个background-size: 100% 100%