我有以下由Django动态填充的模板
<div class="center row">
<h3><span>The Core</span></h3>
{% for member in core %}
<a class="core_img " href="#">
<div class="img__overlay">
{{member.user.first_name}} {{member.user.last_name}}
<br>
{{member.role}}
</div>
<img src='media/{{member.user.avatar}}'>
</a>
{% endfor %}
</div>
这是随附的CSS
.core_img {
border-radius: 100%;
display: flex;
flex: 0 0 150px;
height: 150px;
justify-content: center;
overflow: hidden;
position: relative;
flex-direction: row;
width: 150px;
}
.core_img img {
height: 100%;
}
.img__overlay {
align-items: center;
flex-direction: row;
bottom: 0;
display: flex;
justify-content: center;
left: 0;
opacity: 0;
position: absolute;
right: 0;
top: 0;
transition: opacity 0.25s;
z-index: 1;
}
.img__overlay:hover {
opacity: 1;
}
.img__overlay {
background-color: rgba(26,35,126,0.8);
color: #fafafa;
font-size: 15px;
}
我希望将它一个个显示出来,怎么办?
答案 0 :(得分:3)
如果您在div
循环之前添加for
作为包装,如下所示:
<div class="center row">
<h3><span>The Core</span></h3>
<div class="test"> //////////////// This
{% for member in core %}
<a class="core_img " href="#">
<div class="img__overlay">
{{member.user.first_name}} {{member.user.last_name}}
<br>
{{member.role}}
</div>
<img src='media/{{member.user.avatar}}'>
</a>
{% endfor %}
</div>
</div>
并且.test
类应如下所示:
.test{
display: flex;
flex-direction: row;
}
应该可以解决问题。
这是代码段:
.test{
display: flex;
flex-direction: row;
}
.test a {
margin-right: 5px;
}
<div class="center row">
<h3><span>The Core</span></h3>
<div class="test">
<a class="core_img " href="#">
<div class="img__overlay">
Name
<br>
admin
</div>
<img src='https://via.placeholder.com/150'>
</a>
<a class="core_img " href="#">
<div class="img__overlay">
Name 2
<br>
admin
</div>
<img src='https://via.placeholder.com/150'>
</a>
</div>
</div>
答案 1 :(得分:2)
我在您的“中心行”(重命名为display: flex
)类中添加了center-row
。
Flexbox非常强大,因为您可以非常轻松地排列子元素。 Here's是我每天使用的一个不错的备忘单:)
img{
background-color: red;
width: 150px;
}
.core_img {
border-radius: 100%;
display: flex;
flex: 0 0 150px;
height: 150px;
justify-content: center;
overflow: hidden;
position: relative;
flex-direction: row;
width: 150px;
}
.core_img img {
height: 100%;
}
.img__overlay {
align-items: center;
flex-direction: row;
bottom: 0;
display: flex;
justify-content: center;
left: 0;
opacity: 0;
position: absolute;
right: 0;
top: 0;
transition: opacity 0.25s;
z-index: 1;
}
.img__overlay:hover {
opacity: 1;
}
.img__overlay {
background-color: rgba(26,35,126,0.8);
color: #fafafa;
font-size: 15px;
}
.center-row{
display: flex;
flex-direction: row;
justify-content: center;
}
<h3><span>The Core</span></h3>
<div class="center-row">
<a class="core_img " href="#">
<div class="img__overlay">
Test1 Test2
<br>
css advisor
</div>
<img src=''>
</a>
<a class="core_img " href="#">
<div class="img__overlay">
Test1 Test2
<br>
css advisor
</div>
<img src=''>
</a>
</div>
答案 2 :(得分:1)
只需将.core_img{display:flex}
更改为inlin-flex
,并删除我向父font-size:0
添加的.row
图像之间的空间
.row{
font-size:0;
}
.core_img {
border-radius: 100%;
display: inline-flex;
flex: 0 0 150px;
height: 150px;
justify-content: center;
overflow: hidden;
position: relative;
flex-direction: row;
width: 150px;
}
.core_img img {
height: 100%;
}
.img__overlay {
align-items: center;
flex-direction: row;
bottom: 0;
display: flex;
justify-content: center;
left: 0;
opacity: 0;
position: absolute;
right: 0;
top: 0;
transition: opacity 0.25s;
z-index: 1;
}
.img__overlay:hover {
opacity: 1;
}
.img__overlay {
background-color: rgba(26,35,126,0.8);
color: #fafafa;
font-size: 15px;
}
<div class="center row">
<h3><span>The Core</span></h3>
<a class="core_img " href="#">
<div class="img__overlay">
<br>
</div>
<img src='https://picsum.photos/800'>
</a> <a class="core_img " href="#">
<div class="img__overlay">
<br>
</div>
<img src='https://picsum.photos/800'>
</a>
</div>
答案 3 :(得分:0)
由于我没有任何动态数据,因此我需要重新编写脚本。您将能够对其进行重构以使其很容易地在循环中使用。
我要做的就是在您的.wrapper
下添加一个.row
,并给它display: flex; flex-direction: row;
。
您可以将这些规则应用于.row
类本身,但是您的标题将与您的图像保持一致,我想您不想这样做。
HTML:
<div class="center row">
<h3><span>The Core</span></h3>
<div class="wrapper">
<a class="core_img " href="#">
<div class="img__overlay">
Joe Bloggs
<br>
Developer
</div>
<img src='http://unsplash.it/100/100'>
</a>
<a class="core_img " href="#">
<div class="img__overlay">
Jake Bloggs
<br>
Task Master
</div>
<img src='http://unsplash.it/100/100'>
</a>
</wrapper>
</div>
CSS:
.row{
display: block;
margin: 1em auto;
width: 50vw; // adjust this if you want less of a gap between images
}
h3{
// you don't need this rule
text-align: center;
margin: 1em auto;
}
.wrapper{
display: flex;
align-items: center;
flex-direction: row;
align-content: space-evenly;
}
.core_img {
border-radius: 100%;
display: block;
height: 150px;
overflow: hidden;
position: relative;
flex-direction: row;
width: 150px;
margin: 1% auto; //comment this if you don't want the massive gap between images
}
.core_img img {
height: 100%;
}
.img__overlay {
align-items: center;
flex-direction: row;
bottom: 0;
display: flex;
justify-content: center;
left: 0;
opacity: 0;
position: absolute;
right: 0;
top: 0;
transition: opacity 0.25s;
z-index: 1;
}
.img__overlay:hover {
opacity: 1;
}
.img__overlay {
background-color: rgba(26,35,126,0.8);
color: #fafafa;
font-size: 15px;
}