【问题标题】:Can i use display flex but only with images?我可以使用 display flex 但只能使用图像吗?
【发布时间】:2021-08-17 14:05:03
【问题描述】:

这就是我想要的(带着包)https://i.stack.imgur.com/iUh9O.png

这就是我现在拥有的https://i.stack.imgur.com/f1GhW.png

这是css:

 <!--listing-->
.listing{}
    
.listing{
    display:inline-block;
    margin-top:150px;
    margin-left:60px;
    width:35%;
}

.listing p {
    font-size:16px;
    line-height:30px;
    color:#0000;
}

.price {
    color: #f76f46;
    font-size:20px;
    font-weight:100px;
}

和html:

<!--main section-->
<div class="listing">
    <img src="pinkbag.png" class="center">
    <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span class="price center">$76.00</span></p>
    
    <img src="pinkbag.png" class="center">
    <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span class="price center">$76.00</span></p>
    
    <img src="pinkbag.png" class="center">
    <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span class="price center">$76.00</span></p>
</div>

如果我使用 display:inline-flex;而不是 css 中的 inline-block ,而是这样结束 https://i.stack.imgur.com/O65l2.png

有没有办法让文字留在每张图片下方?

【问题讨论】:

  • 您需要为 img + p 添加一个包装器,这样您就可以将 flex-direction 更改为它们的列,并且文本将位于图像下方

标签: html css flexbox


【解决方案1】:

请找到flex 实现。

.listing {
  display: flex;
  margin-top: 150px;
  margin-left: 60px;
  flex-wrap: wrap;
}

.image-container {
  width: 35%;
  display: flex;
  flex-direction: column;
}

.listing p {
  font-size: 16px;
  line-height: 30px;
  color: #0000;
}

.price {
  color: #f76f46;
  font-size: 20px;
  font-weight: 100px;
}
<div class="listing">
  <div class="image-container">
    <img src="https://via.placeholder.com/150" class="center">
    <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span
        class="price center">$76.00</span></p>
  </div>
  <div class="image-container">
    <img src="https://via.placeholder.com/150" class="center">
    <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span
        class="price center">$76.00</span></p>
  </div>

  <div class="image-container">
    <img src="https://via.placeholder.com/150" class="center">
    <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span
        class="price center">$76.00</span></p>
  </div>
</div>

【讨论】:

    【解决方案2】:

    像这样将每个 img 和 p 标签包装在一个 div 中......

    <div class="listing">
       <div class="col1">
           <img src="pinkbag.png" class="center">
           <p style="color:#3d5791;" class="center">
              <b>Extra Large Madison Handbag</b><br>
              <span class="price center">$76.00</span>
           </p>
       </div>
    
       <div class="col1">
           <img src="pinkbag.png" class="center">
           <p style="color:#3d5791;" class="center">
              <b>Extra Large Madison Handbag</b><br>
              <span class="price center">$76.00</span>
           </p>
       </div>
    
       <div class="col1">
           <img src="pinkbag.png" class="center">
           <p style="color:#3d5791;" class="center">
              <b>Extra Large Madison Handbag</b><br>
              <span class="price center">$76.00</span>
           </p>
       </div>
    </div>
    

    并使用 css flex 布局将它们并排对齐:

    .listing {
       display: flex;
       align-items: center;
       justify-content: space-around;
    }
    
        
    

    【讨论】:

    • 我不得不添加一个 .listing img{} 标签来调整我的图片的大小,因为它们缩小了,但是非常感谢!
    【解决方案3】:

    在每对图像/文本周围放置一个 flex 容器:

    <!--main section-->
    <div class="listing">
    
      <div class="myFlexContainer">
        <img src="pinkbag.png" class="center">
        <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span class="price center">$76.00</span></p>
      </div>
    
    </div>
    
    
    .myFlexContainer {
      display: flex;
      flex-direction: column;
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用inline-grid and grid-template-columns

      输入display: inline-grid;grid-template-columns: auto auto auto; 进入父容器。

      别忘了将每个列表放在div

      调整网格列数改变autogrid-template-columns的数量

      更多内容请关注here

      <!--listing-->
      .listing{}
          
      .listing{
      
          display: inline-grid;
          grid-template-columns:  auto auto auto;
          margin-top:150px;
          margin-left:60px;
          width:100%;
      }
      
      .listing p {
          font-size:16px;
          line-height:30px;
          color:#0000;
      }
      
      .price {
          color: #f76f46;
          font-size:20px;
          font-weight:100px;
      }
      <!--main section-->
      <div class="listing">
          <div>
            <img src="https://image.shutterstock.com/image-photo/red-elegant-female-bag-two-260nw-1032419707.jpg" class="center">
          <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span class="price center">$76.00</span></p>
          </div>
          
          <div>
            <img src="https://image.shutterstock.com/image-photo/red-elegant-female-bag-two-260nw-1032419707.jpg" class="center">
          <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span class="price center">$76.00</span></p>
          </div>
          
          <div>
            <img src="https://image.shutterstock.com/image-photo/red-elegant-female-bag-two-260nw-1032419707.jpg" class="center">
          <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span class="price center">$76.00</span></p>
          </div>
          
          <div>
            <img src="https://image.shutterstock.com/image-photo/red-elegant-female-bag-two-260nw-1032419707.jpg" class="center">
          <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span class="price center">$76.00</span></p>
          </div>
          
          <div>
            <img src="https://image.shutterstock.com/image-photo/red-elegant-female-bag-two-260nw-1032419707.jpg" class="center">
          <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span class="price center">$76.00</span></p>
          </div>
          
          <div>
            <img src="https://image.shutterstock.com/image-photo/red-elegant-female-bag-two-260nw-1032419707.jpg" class="center">
          <p style="color:#3d5791;" class="center"><b>Extra Large Madison Handbag</b><br><span class="price center">$76.00</span></p>
          </div>
          
      
      </div>

      【讨论】:

      • 我得到了棋盘格图案? i.stack.imgur.com/Serpt.png
      • 我搞错了。现在已经修复了
      • 那是因为我忘了把img+p放在一个容器里
      猜你喜欢
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      相关资源
      最近更新 更多