当有预定义高度时如何将文本放置在底部!
这应该是非常微不足道的事情,但事实并非如此。我有一个预定义的锚点高度,我想将文本放置在底部。
<li><a class="my-text">My Text</a></li>
我使用了以下 CSS,但不起作用。文本仍然显示在顶部。
a.my-text {
background-color:#cccc;
height:50px;
vertical-align:bottom;
width:100px;
}
我的想法是:我想将文本对齐到底部,但是如果有超过一行的文本,我希望溢出到顶部,而不是将其他所有内容推到下面......关于如何做到这一点的任何想法能够实现吗?
This should be incredibly trivial, but it's not. I have a predefined height for an anchor, and I would like to place the text at the bottom.
<li><a class="my-text">My Text</a></li>
I used the following CSS, which does not work. The text still appears at the top.
a.my-text {
background-color:#cccc;
height:50px;
vertical-align:bottom;
width:100px;
}
The idea is: I want to align text to the bottom, but if there is text that is longer than one line, I want the over flow to go to the top, not push everything else down... any ideas of how this could be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 css 和您提供的 html 无法完成此操作。如果你在锚点上加一个额外的跨度,可以做到:
This can't be done using css and the html you provide. If you put an extra span in the anchor, it can be done:
您可以在锚点中使用
bottom:0px
和position:absolute
。HTML
CSS
参见jsfiddle。
You can use
bottom:0px
withposition:absolute
in anchor.HTML
CSS
See in jsfiddle.
它肯定行不通,因为
锚点是内联标签,因此为它们分配高度和宽度是没有用的。
vertical-align
属性确定内联元素相对于其所在行的位置,而不是文本的垂直位置。 (请参阅http://reference.sitepoint.com/css/vertical-align)据我了解,您所要求的事情无法完成。然而,如上所述,还有其他替代方案可以实现类似的效果。It definitely would not work, because
<a>
anchors are inline tags, therefore assigning them heights and widths is useless. Thevertical-align
property determines the positioning of inline elements with respect to the line they're in, not the vertical position of the text. (See http://reference.sitepoint.com/css/vertical-align) As far as I understand what you are requesting cannot be done. However, there are alternatives, as suggested above, to achieve similar effects.您的代码的问题是锚点不会响应高度/宽度,因为它是内联元素。如果您将 {display: block} 添加到锚点,它现在是一个块元素,但是,据我记得,vertical-align 不适用于块元素。这是我能想到的使用 display: table-cell 的最简单方法。
The issue with your code is that the anchor won't respond to height/width because it is an inline element. If you you add a {display: block} to the anchor it's now a block element, but, as I recall, vertical-align doesn't work on the contents of block elements. This was the easiest way I could think of using display: table-cell.
听起来您只需要摆脱锚标记上的高度规则并在
li
上使用类似padding-top: 45px
的内容即可It sounds like you just need to get rid of the height rule on the anchor tag and use something like
padding-top: 45px
on theli