我正在尝试使用 flexbox 显示 collection 网格,但我无法获得正确的布局
I am trying to show a collection grid using flexbox but i am not able to get the layout right
我正在尝试使用 flexbox 实现以下响应式布局,如下图所示..
我尝试了一些东西,但没有得到想要的结果。
此设计中的每个项目都有一个图像和顶部我需要显示博客标题、日期和类别,但我无法正确布局
目前我在没有 flexbox 的情况下做同样的事情,但是它有很多 css 和其他标签,虽然我可以使用 flex 用更少的代码来改变它。
我看了很多示例,但找不到 1 个与我的布局相似的示例
当前页面使用的是 bootstrap v3.3.6,我无法更改,因为它会影响网站的其他部分
.flex-container {
display: flex;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
.flex1 {
flex: 100%;
max-height:372px;
}
.flex2 {
flex:25%;
max-height:372px;
}
.flex3 {
flex:25%;
max-height:372px;
}
.flex4 {
flex:25%;
max-height:372px;
}
.flex5 {
flex:25%;
max-height:372px;
}
.flex-container > div > span {
position:absolute;
z-index:1;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<div class="container">
<div class="row row-margin">
<div class="flex-container">
<div class="flex1">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<span>1</span>
</div>
<div class="flex2">
<img src="https://dummyimage.com/480x360/3b063b/fff.jpg&text=Image" class="img-responsive">
<span>2</span>
</div>
<div class="flex3">
<img src="https://dummyimage.com/645x424/3b063b/fff.jpg&text=Image" class="img-responsive">
<span>3</span>
</div>
<div class="flex4">
<img src="https://dummyimage.com/480x360/3b063b/fff.jpg&text=Image" class="img-responsive">
<span>4</span>
</div>
<div class="flex5">
<img src="https://dummyimage.com/480x360/3b063b/fff.jpg&text=Image" class="img-responsive">
<span>5</span>
</div>
</div>
</div>
</div>
我建议你使用网格,有很多方法可以实现这一点,但如果你关心媒体查询,那么 flex 绝对不是处理这样的网格的最佳方法。尽管如此,一种方法是使用 positions 和 flex。但这是一个权衡。
.container {
display: flex;
justify-content: center;
align-items: center;
gap: 1px;
}
.item {
height: 4rem;
width: 4rem;
background-color: antiquewhite;
display: flex;
justify-content: center;
align-items: center;
}
.item-1 {
height: 8rem;
width: 8rem;
}
.item-2,
.item-3 {
margin-top: -4rem;
}
.row {
display: flex;
gap: 1px;
}
.item-4 {
position: absolute;
margin-top: 1px;
}
.item-5 {
position: absolute;
margin-top: 1px;
margin-left: 4.1rem;
}
<div class="container">
<div class="item item-1">1</div>
<div class="row">
<div class="item item-2">2</div>
<div class="item item-3">3</div>
<div class="item item-4">4</div>
<div class="item item-5">6</div>
</div>
</div>
我还意识到您可以将 2 项 2 和 4 包装成一个 div 并将其 flex-direction 设置为列,对 3 和 6 项执行相同的操作。
我已经设法使用网格来做到这一点,并且无需编写任何额外的媒体查询即可响应
基本工作代码示例https://codepen.io/KGuide/pen/xxPWxKj
<div class="container">
<div class="row">
<div class="column1 columnX grid-col" style="background-color:#f00;">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<h2>Column 1</h2>
<p>Some text..</p>
</div>
<div class=" column2 column grid-col" style="background-color:#f00;">
<div class="row margin-0">
<div class="column grid-col" style="background-color:#aaa;">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<h2>Column 2</h2>
<p>Some text..</p>
</div>
<div class="column grid-col" style="background-color:#bbb;">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<h2>Column 3</h2>
<p>Some text..</p>
</div>
</div>
<div class="row margin-0">
<div class="column grid-col" style="background-color:#ccc;">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<h2>Column 4</h2>
<p>Some text..</p>
</div>
<div class="column grid-col" style="background-color:#ddd;">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<h2>Column 5</h2>
<p>Some text..</p>
</div>
</div>
</div>
CSS
* {
box-sizing: border-box;
}
/* Create two equal columns that floats next to each other */
.column {
float: left;
width: 50%;
padding: 1px;
margin:0;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Style the buttons */
.btn {
border: none;
outline: none;
padding: 12px 16px;
background-color: #f1f1f1;
cursor: pointer;
}
.btn:hover {
background-color: #ddd;
}
.btn.active {
background-color: #666;
color: white;
}
.column1{float: left; width:50%; padding:0; margin:0;}
.column2{float: left; width:50%; padding:0; margin:0;}
.grid-col {position:relative;}
.grid-col > img {z-index:9;}
.grid-col > h2,p {position:absolute; z-index:10;top:30px;left:25px; }
.margin-0{margin:0px;}
我正在尝试使用 flexbox 实现以下响应式布局,如下图所示..
我尝试了一些东西,但没有得到想要的结果。
此设计中的每个项目都有一个图像和顶部我需要显示博客标题、日期和类别,但我无法正确布局
目前我在没有 flexbox 的情况下做同样的事情,但是它有很多 css 和其他标签,虽然我可以使用 flex 用更少的代码来改变它。
我看了很多示例,但找不到 1 个与我的布局相似的示例
当前页面使用的是 bootstrap v3.3.6,我无法更改,因为它会影响网站的其他部分
.flex-container {
display: flex;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
.flex1 {
flex: 100%;
max-height:372px;
}
.flex2 {
flex:25%;
max-height:372px;
}
.flex3 {
flex:25%;
max-height:372px;
}
.flex4 {
flex:25%;
max-height:372px;
}
.flex5 {
flex:25%;
max-height:372px;
}
.flex-container > div > span {
position:absolute;
z-index:1;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<div class="container">
<div class="row row-margin">
<div class="flex-container">
<div class="flex1">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<span>1</span>
</div>
<div class="flex2">
<img src="https://dummyimage.com/480x360/3b063b/fff.jpg&text=Image" class="img-responsive">
<span>2</span>
</div>
<div class="flex3">
<img src="https://dummyimage.com/645x424/3b063b/fff.jpg&text=Image" class="img-responsive">
<span>3</span>
</div>
<div class="flex4">
<img src="https://dummyimage.com/480x360/3b063b/fff.jpg&text=Image" class="img-responsive">
<span>4</span>
</div>
<div class="flex5">
<img src="https://dummyimage.com/480x360/3b063b/fff.jpg&text=Image" class="img-responsive">
<span>5</span>
</div>
</div>
</div>
</div>
我建议你使用网格,有很多方法可以实现这一点,但如果你关心媒体查询,那么 flex 绝对不是处理这样的网格的最佳方法。尽管如此,一种方法是使用 positions 和 flex。但这是一个权衡。
.container {
display: flex;
justify-content: center;
align-items: center;
gap: 1px;
}
.item {
height: 4rem;
width: 4rem;
background-color: antiquewhite;
display: flex;
justify-content: center;
align-items: center;
}
.item-1 {
height: 8rem;
width: 8rem;
}
.item-2,
.item-3 {
margin-top: -4rem;
}
.row {
display: flex;
gap: 1px;
}
.item-4 {
position: absolute;
margin-top: 1px;
}
.item-5 {
position: absolute;
margin-top: 1px;
margin-left: 4.1rem;
}
<div class="container">
<div class="item item-1">1</div>
<div class="row">
<div class="item item-2">2</div>
<div class="item item-3">3</div>
<div class="item item-4">4</div>
<div class="item item-5">6</div>
</div>
</div>
我还意识到您可以将 2 项 2 和 4 包装成一个 div 并将其 flex-direction 设置为列,对 3 和 6 项执行相同的操作。
我已经设法使用网格来做到这一点,并且无需编写任何额外的媒体查询即可响应
基本工作代码示例https://codepen.io/KGuide/pen/xxPWxKj
<div class="container">
<div class="row">
<div class="column1 columnX grid-col" style="background-color:#f00;">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<h2>Column 1</h2>
<p>Some text..</p>
</div>
<div class=" column2 column grid-col" style="background-color:#f00;">
<div class="row margin-0">
<div class="column grid-col" style="background-color:#aaa;">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<h2>Column 2</h2>
<p>Some text..</p>
</div>
<div class="column grid-col" style="background-color:#bbb;">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<h2>Column 3</h2>
<p>Some text..</p>
</div>
</div>
<div class="row margin-0">
<div class="column grid-col" style="background-color:#ccc;">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<h2>Column 4</h2>
<p>Some text..</p>
</div>
<div class="column grid-col" style="background-color:#ddd;">
<img src="https://dummyimage.com/645x424/753075/fff.jpg&text=Image" class="img-responsive">
<h2>Column 5</h2>
<p>Some text..</p>
</div>
</div>
</div>
CSS
* {
box-sizing: border-box;
}
/* Create two equal columns that floats next to each other */
.column {
float: left;
width: 50%;
padding: 1px;
margin:0;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Style the buttons */
.btn {
border: none;
outline: none;
padding: 12px 16px;
background-color: #f1f1f1;
cursor: pointer;
}
.btn:hover {
background-color: #ddd;
}
.btn.active {
background-color: #666;
color: white;
}
.column1{float: left; width:50%; padding:0; margin:0;}
.column2{float: left; width:50%; padding:0; margin:0;}
.grid-col {position:relative;}
.grid-col > img {z-index:9;}
.grid-col > h2,p {position:absolute; z-index:10;top:30px;left:25px; }
.margin-0{margin:0px;}