当有预定义高度时如何将文本放置在底部!

发布于 2024-09-14 12:06:02 字数 400 浏览 1 评论 0原文

这应该是非常微不足道的事情,但事实并非如此。我有一个预定义的锚点高度,我想将文本放置在底部。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如梦 2024-09-21 12:06:02

使用 css 和您提供的 html 无法完成此操作。如果你在锚点上加一个额外的跨度,可以做到:

a.my-text {
  height: 50px;
  display: block;
}
a.my-text span {
  position: absolute;
  bottom: 0;
}

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:

a.my-text {
  height: 50px;
  display: block;
}
a.my-text span {
  position: absolute;
  bottom: 0;
}
就此别过 2024-09-21 12:06:02

您可以在锚点中使用 bottom:0pxposition:absolute

HTML

<li><a class="my-text">My Text</a></li>

CSS

li {
    position: relative;
    height:200px;
    border: 1px solid red;
}

a.my-text {
    bottom: 0px;
    border: 1px solid blue;
    position: absolute;
    background-color:#cccc;
    width:100px;
    height:50px;
}

参见jsfiddle

You can use bottom:0px with position:absolute in anchor.

HTML

<li><a class="my-text">My Text</a></li>

CSS

li {
    position: relative;
    height:200px;
    border: 1px solid red;
}

a.my-text {
    bottom: 0px;
    border: 1px solid blue;
    position: absolute;
    background-color:#cccc;
    width:100px;
    height:50px;
}

See in jsfiddle.

旧伤慢歌 2024-09-21 12:06:02

It definitely would not work, because <a> anchors are inline tags, therefore assigning them heights and widths is useless. The vertical-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.

離人涙 2024-09-21 12:06:02

您的代码的问题是锚点不会响应高度/宽度,因为它是内联元素。如果您将 {display: block} 添加到锚点,它现在是一个块元素,但是,据我记得,vertical-align 不适用于块元素。这是我能想到的使用 display: table-cell 的最简单方法。

a.my-text {
  background-color: #ccc;
  height: 200px; width: 100px;
  vertical-align: bottom;
  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.

a.my-text {
  background-color: #ccc;
  height: 200px; width: 100px;
  vertical-align: bottom;
  display: table-cell;
}
花开半夏魅人心 2024-09-21 12:06:02

听起来您只需要摆脱锚标记上的高度规则并在 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 the li

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文