【发布时间】:2019-07-10 22:14:45
【问题描述】:
我想做的事:
块元素相对于绝对位置的元素调整其高度。
有问题:
如果价格太长,价格离开区块。
条件:
- 块元素必须是可点击的才能转到链接。
- 价格不得在参考链接中。
我一直在尝试做的事情:
在javascript中使价格元素可点击以转到链接并删除绝对位置,未选择此解决方案,因为您无法执行“CTRL + CLICK”以在新标签中打开。
效果不错的案例:
* {
box-sizing: border-box;
}
.block {
background: #3CAEA3;
display: inline-block;
position: relative;
}
.link {
display: inline-block;
padding: 20px 20px 40px 20px;
text-align: center;
text-decoration: none;
}
.title {
font-size: 20px;
}
.subtitle {
font-size: 16px;
}
.price {
height: 20px;
position: absolute;
bottom: 0;
left: 0;
right: 0;
text-align: center;
pointer-events: none;
}
<div class="block">
<a class="link" href="https://google.com">
<div class="title">Coffee table</div>
<div class="subtitle">Made of wood</div>
</a>
<div class="price">
The price is: 100€
</div>
</div>
不起作用的情况:
* {
box-sizing: border-box;
}
.block {
background: #3CAEA3;
display: inline-block;
position: relative;
}
.link {
display: inline-block;
padding: 20px 20px 40px 20px;
text-align: center;
text-decoration: none;
}
.title {
font-size: 20px;
}
.subtitle {
font-size: 16px;
}
.price {
height: 20px;
position: absolute;
bottom: 0;
left: 0;
right: 0;
text-align: center;
pointer-events: none;
}
<div class="block">
<a class="link" href="https://google.com">
<div class="title">Coffee table</div>
<div class="subtitle">Made of wood</div>
</a>
<div class="price">
The price is tooooo long: 100€
</div>
</div>
你对我的问题有什么解决办法吗?
谢谢。
【问题讨论】:
-
您尝试使用绝对值有什么原因吗? flexbox 是一种选择吗?
-
如果您不关心旧浏览器,请使用 flexbox。父 div 会自动调整大小
-
简单去掉固定高度(
height:20px) -
我真的不知道该怎么做。价格元素必须是可点击的,如果我删除绝对位置它将不再是这种情况。如果我删除20像素的高度,如果价格在3行就不好了。