【问题标题】:Get height of flexbox row获取flexbox行的高度
【发布时间】:2016-09-14 11:27:48
【问题描述】:

我有一个 flexbox 容器,其中每一行的项目溢出,然后进入下一行。每个项目都与基线对齐;因此,行的大小可以不同,具体取决于哪些项目在哪一行以及它们如何与基线对齐。

下图中的项目以基线为中心:

我想知道是否有办法动态获取行高?或者获取从每个项目的基线到行的顶部/底部的距离?

我想这样做,以便我可以在项目周围设置悬停状态,允许悬停在项目上伸展,并使高度一直到每行的顶部,因此每个项目的悬停将如下所示:

我的尝试: codepen

/* on hover i want this to be a box around the item, and to have the top and the bottom of the border be touching the top/bottom of the row it is on */
.flex-item:hover {
  border: 1px solid darkred;
}

/* so item doesn't move when not hovering */
.flex-item {
  border: 1px solid lightblue;
}

.flex-container {
  overflow: hidden;    position: relative;
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: baseline;
  align-items: baseline;
  width: 400px;
  height: 350px;
  background-color: lightblue;
  flex-wrap: wrap;
  padding: 10px;
}

.inner-wrapper {
  display: inline-block;
  text-align: center;
  width: 100px;
  margin: 10px;
  background-color: cornflowerblue;
}

.flex-body {
  border-bottom: 1px solid #fff;
}
.flex-body-more {
  float: left;
  clear: left;
  width: 100%;
}
.flex-img {
  justify-content: center;
  align-items: center;
}
  <div class="flex-container">
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1 longer title</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/40x40/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/40x90/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text more text more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/40x50/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
  </div>

【问题讨论】:

    标签: javascript html css flexbox


    【解决方案1】:

    我能够通过更改使所有项目的悬停效果相同高度

    .flex-container {
        align-items: baseline
    } 
    

    .flex-container {
        align-items: strech
    }
    

    然后我添加了以下几行以使项目垂直对齐:

    .flex-item {
        display: flex;
        align-items: center;
    }
    

    但是,您的原始代码将这些项目对齐为“基线”而不是“中心”。我不知道该怎么做。

    希望这会有所帮助。

    codepen:https://codepen.io/bottlecap/pen/pEgbNx

    【讨论】:

      【解决方案2】:

      尝试将其插入 codepen。

      html

          <div class="flex-container">
        <div class="flex-item">
          <div class="inner-wrapper">
            <div class="flex-title">flex title1</div>
            <div class="flex-img">
              <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
            </div>
            <div class="flex-body">center here</div>
            <div class="flex-body-more">more text</div>
          </div>
        </div>
        <div class="flex-item">
          <div class="inner-wrapper">
            <div class="flex-title">flex title1 longer title</div>
            <div class="flex-img">
              <img src='http://dummyimage.com/40x40/000/ffffff&text=hi'>
            </div>
            <div class="flex-body">center here</div>
            <div class="flex-body-more">more text</div>
          </div>
        </div>
        <div class="flex-item">
          <div class="inner-wrapper">
            <div class="flex-title">flex title1</div>
            <div class="flex-img">
              <img src='http://dummyimage.com/40x90/000/ffffff&text=hi'>
            </div>
            <div class="flex-body">center here</div>
            <div class="flex-body-more">more text more text more text</div>
          </div>
        </div>
        <div class="flex-item">
          <div class="inner-wrapper">
            <div class="flex-title">flex title1</div>
            <div class="flex-img">
              <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
            </div>
            <div class="flex-body">center here</div>
            <div class="flex-body-more">more text</div>
          </div>
        </div>
        <div class="flex-item">
          <div class="inner-wrapper">
            <div class="flex-title">flex title1</div>
            <div class="flex-img">
              <img src='http://dummyimage.com/40x50/000/ffffff&text=hi'>
            </div>
            <div class="flex-body">center here</div>
            <div class="flex-body-more">more text</div>
          </div>
        </div>
      </div>
      

      css

          /* on hover i want this to be a box around the item, and to have the top and the bottom of the border be touching the top/bottom of the row it is on */
      .flex-item:hover {
        border: 1px solid darkred;
      }
      
      /* so item doesn't move when not hovering */
      .flex-item {
        border: 1px solid lightblue;
        outline:1px solid red;
        height:62vh;
      
      
      }
      
      .flex-container {
        overflow: hidden;    position: relative;
        display: -webkit-flex;
        display: flex;
        -webkit-align-items: baseline;
        align-items: baseline;
        width: 400px;
        height: 400px;
        background-color: lightblue;
        flex-wrap: wrap;
        padding: 10px;
        flex-direction:column;
      
      }
      
      .inner-wrapper {
        display: inline-block;
        text-align: center;
        width: 100px;
        margin: 10px;
        background-color: cornflowerblue;
      }
      
      .flex-body {
        border-bottom: 1px solid #fff;
      }
      .flex-body-more {
        float: left;
        clear: left;
        width: 100%;
      }
      .flex-img {
        justify-content: center;
        align-items: center;
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-29
        • 2016-08-01
        • 1970-01-01
        • 2014-10-16
        • 1970-01-01
        • 2023-02-22
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        相关资源
        最近更新 更多