gpt4 book ai didi

javascript - 获取 flexbox 行的高度

转载 作者:行者123 更新时间:2023-11-28 03:50:26 25 4
gpt4 key购买 nike

我有一个 flexbox 容器,其中每一行的元素都会溢出,然后转到下一行。每个元素都与基线对齐;因此,行可以有不同的大小,这取决于哪些元素在哪一行以及它们如何与基线对齐。

下图中的元素以基线为中心:

enter image description here

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

我想这样做,这样我就可以在元素周围设置一个悬停状态,允许悬停在元素上延伸,并让高度一直延伸到每一行的顶部,因此每个元素的悬停都会如下所示:

enter image description here

我的尝试: 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>

最佳答案

我可以通过更改

让悬停效果在所有元素上具有相同的高度
.flex-container {
align-items: baseline
}

.flex-container {
align-items: strech
}

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

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

enter image description here

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

希望这对您有所帮助。

代码笔:https://codepen.io/bottlecap/pen/pEgbNx

关于javascript - 获取 flexbox 行的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39458865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com