如何在 CSS 中实现绝对定位?
<div id="container" style="float:left;">
<img src="{{ p.sizes.2.url }}" width="200" height="auto">
<div class="trans_caption" style="position:absolute;background-color:#cccccc;">
Picture Caption
</div>
</div>
如何将标题覆盖在图片顶部...但与底部对齐?另外,我希望标题宽度与容器相同。我尝试做宽度:100%,但它不起作用......
<div id="container" style="float:left;">
<img src="{{ p.sizes.2.url }}" width="200" height="auto">
<div class="trans_caption" style="position:absolute;background-color:#cccccc;">
Picture Caption
</div>
</div>
How do I overlay the caption on top of the picture...but aligned on the bottom? Also, I want the caption width to be the same as the container. I tried to do width:100%, but it didn't work...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是您要找的吗?
只需在主 div 中设置
position:relative
- 它将允许相对于主 div 定位内部 div,并在内部 div 中设置bottom:0
将其定位在底部。带有float:left
和width:100%
的小黑客,没有float
width:100%
似乎并不存在才能正常工作。Is that what you are looking for?
Just set
position:relative
in your main div - it will allow to position inner div relatively to the main div, and setbottom:0
in your inner div to position it on the bottom. Small hack withfloat:left
andwidth:100%
, withoutfloat
width:100%
doesn't seem to work properly.您需要在
#container
上设置position:relative
。这将使绝对定位相对于该容器 div 的边缘。将
bottom: 0;
添加到.trans_caption
以使文本的基线(不是确切的底部)与底部对齐图片。如果您想将文本向上移动,请增加该数字。将
width: 100%
添加到.trans_caption
,使其与容器一样宽。如果您想让标题居中,请将
text-align: center;
添加到.trans_caption
。请注意,图像
height
属性的auto
值无效。最好将 CSS 与 HTML 标记分开,放在单独的文件中。我们现在拥有的是(尝试一下):
You need to set
position: relative
on#container
. That will make the absolute positioning relative to the edges of that container div.Add
bottom: 0;
to.trans_caption
to make the baseline (not the exact bottom) of the text aligned with the bottom of the picture. Increase that number if you want to move the text higher up.Add
width: 100%
to.trans_caption
to make it as wide as its container.If you want to center the caption, add
text-align: center;
to.trans_caption
.Note that the
auto
value for an image'sheight
attribute is not valid.It's best to keep the CSS separate from the HTML markup, in a separate file. What we have now would be (try it out):
绝对定位意味着该元素将定位在最后一个父级上未使用默认位置
position: static
定位的特定位置。相对定位与静态相同,不同之处在于:
所有这一切都意味着,如果您将容器定位为相对位置,则 trans_caption 的绝对定位将相对于您的容器产生影响,而现在它是相对于更高级别的容器定位的。
此外,绝对定位会将您的元素放置在
top: 0;左:0;
除非另有说明。您需要将标题放置在bottom:0;
处,以将其强制放置在容器的底部。您的 trans_caption 通常默认为 100% 宽度,因为
是块显示元素,因此它对您提供的示例没有执行任何操作是有道理的。然而,绝对定位的项目并非如此,因此请保留该行。如果您随后使用
text-align: center;
设置样式,使文本在该中居中,那么它应该看起来像您期望的那样。
Absolute positioning means that the element will be positioned at a specific spot on the last parent that is not positioned with the default,
position: static
.Relative positioning is the same as static, except:
All of that is to say that if you position your container as relative, the absolute positioning of the trans_caption will be in affect relative to your container, where now it is positioned relative to a more higher level container.
Also, absolute positioning will place your element at
top: 0; left: 0;
unless otherwise specified. You need to position your caption atbottom:0;
to force it to the bottom of your container.Your trans_caption will normally default to 100% width because
<div>
is a block-displayed element, so it makes sense that it didn't do anything with the example you've provided. This isn't the case with absolutely positioned items, however, so keep that line. If you then center the text within that<div>
by styling it withtext-align: center;
, it should look the way you expect.我认为你想要做的是在容器上设置 css
background
属性。像这样的东西
I think what you want to do is set the css
background
property on the container.Something like this