html - 绝对定位到 div 底部的文本需要向下而不是向上增长/扩展

标签 html css css-position

我有一个三列布局,使用绝对定位将号召性用语定位在底部,容器的高度为 100%:

-----------------------------    -----------------------------     -----------------------------  
| Col 1                     |    | Col 2                     |     | Col 3                     |
|                           |    |                           |     |                           |
| Copy                      |    | Copy                      |     | Copy                      |
|                           |    |                           |     |                           |
|                           |    |                           |     | Call to action 3 is       |
| Call to action 1          |    | Call to action 2          |     | Longer text               |
-----------------------------    -----------------------------     -----------------------------  

但我想要的是段落中的第一句话与其他句子保持一致,即使其余部分溢出到容器之外:

-----------------------------    -----------------------------     -----------------------------  
| Col 1                     |    | Col 2                     |     | Col 3                     |
|                           |    |                           |     |                           |
| Copy                      |    | Copy                      |     | Copy                      |
|                           |    |                           |     |                           |
|                           |    |                           |     |                           |
| Call to action 1          |    | Call to action 2          |     | Call to action 3 is       |
-----------------------------    -----------------------------     -----------------------------  
                                                                     Longer text

我能够通过在 CTA 上使用 top:300px 而不是 bottom:10px 来实现这一点,但这并不理想,因为如果不调整 top:300px 值,就无法更改副本中的字符数。感谢您的帮助。

最佳答案

您可以通过将 CTA 完全推出卡片然后使用等于您的行高的转换值将其拉回来来管理此问题。

.container {
  display: flex;
}

.card {
  flex: 1;
  padding: 1em;
  padding-bottom: 2.2em;
  border: 1px solid green;
  margin: 1em;
  position: relative;
}

.cta {
  position: absolute;
  top: 100%;
  border: 1px solid red;
  padding: 0 1em;
  left: 0;
  right: 0;
  transform: translateY(-1.2em);
  /* line-height */
}
<div class="container">
  <div class="card">
    <h2>Heading 1</h2>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Natus, voluptatem.</p>
    <div class="cta">CTA</div>
  </div>
  <div class="card">
    <h2>Heading 2</h2>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptate repellendus in magni?</p>
    <div class="cta">CTA Lorem ipsum dolor sit amet</div>
  </div>
  <div class="card">
    <h2>Heading 3</h2>
    <p>Lorem ipsum dolor sit amet.</p>
    <div class="cta">CTA Lorem ipsum dolor sit amet, consectetur adipisicing elit. </div>
  </div>
</div>

关于html - 绝对定位到 div 底部的文本需要向下而不是向上增长/扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58962655/

相关文章:

javascript - firefox/edge 中的 HTML 自定义元素

html - 增加浏览器大小后如何显示div?

html - 在 android 浏览器/cordova 上滚动太远时摆脱阴影

javascript - 使用 jQuery 从本地存储中的数组中删除元素

css - 覆盖谷歌地图在 jqm 中放置搜索框样式

javascript - 隐藏页脚 javascript 中的固定元素

html - 输入占位符在 Firefox 中未垂直对齐

javascript - 无法使用 v-if 获取元素的 id

twitter-bootstrap - Bootstrap 在容器外添加框

html - Bootstrap4 将登录表单移至中心