繁体   English   中英

连续显示弹性项目

[英]Display flex items in a row

我有以下由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;
}

截至目前,图像正以一个显示在另一个显示之下 在此处输入图片说明

我希望将它一个接一个地显示,该如何处理?

如果在for循环之前将div作为包装器添加,如下所示:

<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> 

我添加了一个display: flex到您的“ center row”(重命名为center-row )类。

Flexbox非常强大,因为您可以非常轻松地排列子元素。 是我每天使用一个不错的备忘单:)

 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> 

只需将.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> 

由于我没有任何动态数据,因此我需要重新编写脚本。 您将能够对其进行重构以使其很容易地在循环中使用。

我所做的就是在.wrapper下方添加.rowdisplay: flex; flex-direction: 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM