【问题标题】:Align multiple Images (vertically aligned to the middle) with captions aligned along the baseline将多个图像(垂直对齐到中间)与沿基线对齐的标题对齐
【发布时间】:2017-10-13 22:44:58
【问题描述】:

我一直在尝试这样做一段时间,但无法找到适合我的解决方案。我从不同高度的数据库中获取图像,我需要将它们垂直对齐到中间,标题水平对齐在每个图像的末尾。

请注意,所有图像都以不同的方式剪切,我无法更改,因此它们看起来不对齐并不重要。

更新:图形标题(跨度)也可能有不同的高度。

Here's a Pencode

Here's what i need to accomplish

非常感谢一些帮助。

<section class="new-products container">
<div class="image-row">
        <!--PRODUCT BLOCK-->
          <div class="product-block col-lg-4 col-md-4 col-xs-12">
            <figure>
              <img src="https://s21.postimg.org/wr1wb9z0n/test2.jpg" class="img-responsive" alt="Foto Producto">
              <figcaption>
                <span class="category">category</span>
                <span class="product-name">Product Name</span>
                <span span="" class="price">price 6€</span>
              </figcaption>
            </figure>  
          </div>  
        <!--PRODUCT BLOCK END-->
        <!--PRODUCT BLOCK-->
          <div class="product-block col-lg-4 col-md-4 col-xs-12">
            <figure>
              <img src="https://s21.postimg.org/z9nlbykqv/test1.jpg" class="img-responsive" alt="Foto Producto">  
              <figcaption>
                <span class="category">category</span>
                <span class="product-name">Product Name</span>
                <span span="" class="price">price 6€</span>
              </figcaption>
            </figure>  
          </div>  
        <!--PRODUCT BLOCK END-->
        <!--PRODUCT BLOCK-->
          <div class="product-block col-lg-4 col-md-4 col-xs-12">
            <figure>
              <img src="https://s21.postimg.org/h84ge5qpz/test3.jpg" class="img-responsive" alt="Foto Producto">  
              <figcaption>
                <span class="category">category</span>
                <span class="product-name">Product Name</span>
                <span span="" class="price">price 6€</span>
              </figcaption>
            </figure>  
          </div>  
        <!--PRODUCT BLOCK END-->
        </div>

.image-row {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-align: baseline;
-webkit-align-items: baseline;
align-items: baseline;
-webkit-box-align: baseline;
-moz-box-align: baseline;
}

.new-products {
    text-align: center;
}

.product-block .category, .product-block .category-special {
    font-size: .75em;
    font-weight: 600;
}

.product-block {
    margin: 0 0 2em;
}

.product-block span {
    display: block;
}

.product-block .category, .product-block .category-special {
    font-size: 1em;
    font-weight: 600;
    letter-spacing: .063em;
    text-transform: uppercase;
}

.product-block .category {
    color: #b10832;
}

【问题讨论】:

    标签: html image css flexbox


    【解决方案1】:

    所以我对你的代码做了以下操作:

    1. 删除baseline 对齐image-row

    2. figures 设为flexbox 列并应用这些样式:

      .product-block figure {
        display: flex;
        flex-direction: column;
        height: 100%;
      }
      .product-block figure img {
        border: 1px solid red;
        margin-top:auto;
      }
      .product-block figure figcaption {
        margin-top:auto;
      }
      

    还在图像周围添加了边框以进行说明。让我知道您对此的反馈,谢谢!

    下面的演示:

    .image-row {
      display: -webkit-box;
      display: -moz-box;
      display: -ms-flexbox;
      display: -webkit-flex;
      display: flex;
      /*
      -ms-flex-align: baseline;
      -webkit-align-items: baseline;
      align-items: baseline;
      -webkit-box-align: baseline;
      -moz-box-align: baseline;
      */
    }
    .new-products {
      text-align: center;
    }
    .product-block .category,
    .product-block .category-special {
      font-size: .75em;
      font-weight: 600;
    }
    .product-block {
      margin: 0 0 2em;
    }
    .product-block span {
      display: block;
    }
    .product-block .category,
    .product-block .category-special {
      font-size: 1em;
      font-weight: 600;
      letter-spacing: .063em;
      text-transform: uppercase;
    }
    .product-block .category {
      color: #b10832;
    }
    .product-block figure {
      display: flex;
      flex-direction: column;
      height: 100%;
    }
    .product-block figure img {
      border: 1px solid red;
      margin-top:auto;
    }
    .product-block figure figcaption {
      margin-top:auto;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <section class="new-products container">
      <div class="image-row">
        <!--PRODUCT BLOCK-->
        <div class="product-block col-lg-4 col-md-4 col-xs-12">
          <figure>
            <img src="https://s21.postimg.org/wr1wb9z0n/test2.jpg" class="img-responsive" alt="Foto Producto">
            <figcaption>
              <span class="category">category</span>
              <span class="product-name">Product Name</span>
              <span span="" class="price">price 6€</span>
            </figcaption>
          </figure>
        </div>
        <!--PRODUCT BLOCK END-->
        <!--PRODUCT BLOCK-->
        <div class="product-block col-lg-4 col-md-4 col-xs-12">
          <figure>
            <img src="https://s21.postimg.org/z9nlbykqv/test1.jpg" class="img-responsive" alt="Foto Producto">
            <figcaption>
              <span class="category">category</span>
              <span class="product-name">Product Name</span>
              <span span="" class="price">price 6€</span>
            </figcaption>
          </figure>
        </div>
        <!--PRODUCT BLOCK END-->
        <!--PRODUCT BLOCK-->
        <div class="product-block col-lg-4 col-md-4 col-xs-12">
          <figure>
            <img src="https://s21.postimg.org/h84ge5qpz/test3.jpg" class="img-responsive" alt="Foto Producto">
            <figcaption>
              <span class="category">category</span>
              <span class="product-name">Product Name</span>
              <span span="" class="price">price 6€</span>
            </figcaption>
          </figure>
        </div>
        <!--PRODUCT BLOCK END-->
      </div>
      </div>

    【讨论】:

    • 嘿@kukkuz 非常感谢!这对我来说就像一个魅力,我会检查你的答案作为解决方案,因为它更干净,更容易使其响应。任何 cmets 对此进行交叉浏览?我的意思是我知道在这方面使用 flexbox 的含义,但是有什么额外的反馈吗?非常感谢!
    • 只有一件事@kukkuz,如果任何跨度增长(更高的高度),那么整个 figcaption 将不会与它旁边的对齐。
    • @Lowtrux 如果跨度是多行的,那么这个解决方案可能不起作用...
    • 是的,不幸的是跨度可以是多行,有什么想法吗?谢谢!
    【解决方案2】:

    将每个图像包装在一个 div 中,即,

    <div class="image-holder">
    <img src="https://s21.postimg.org/wr1wb9z0n/test2.jpg" class="img-responsive" alt="Foto Producto">
    </div> 
    

    并使用匹配高度 js 插件 (http://brm.io/jquery-match-height/) 对这些 div 应用相等的高度。然后

    .image-holder {
        float: left;
        width: 100%;
        vertical-align: middle;
    }
    
    .image-holder img {
        vertical-align: bottom;
        display: inline-block;
        width: auto;
        max-width: 100%;
        height: auto;
    }
    
    .image-holder:before {
        content: '';
        display: inline-block;
        height: 100%;
        vertical-align: middle;
        margin-right: -.25em;
    }
    

    【讨论】:

    • 感谢@Praveen,您的解决方案对我不起作用,图像标题与基线对齐,但图片仍然没有在中间垂直对齐(js 插件工作正常)。我已经尝试过在容器上使用和不使用 display flex,因为你没有提到这是否仍然需要但仍然不起作用。
    • .image-holder{ float:left;宽度:100%;垂直对齐:底部;高度:700 像素; } .image-holder img{ 边框:2px 纯红色;垂直对齐:底部;显示:内联块;宽度:自动;最大宽度:100%;高度:自动; } .image-holder:before{ 内容:'';显示:内联块;高度:100%;垂直对齐:中间;右边距:-.25em; }
    • image-holder height 仅用于演示目的。如果您使用的是匹配高度插件,只需将其删除。
    • 如果标题跨度是多行的,您的答案是否有效?谢谢!
    • 是的,我想。你能试试吗
    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 2010-12-21
    • 2010-12-03
    • 2022-12-23
    相关资源
    最近更新 更多