如何在 CSS 中实现绝对定位?

发布于 2024-10-01 23:03:34 字数 380 浏览 1 评论 0原文

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

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

发布评论

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

评论(6

放飞的风筝 2024-10-08 23:03:34

是您要找的吗?

只需在主 div 中设置 position:relative - 它将允许相对于主 div 定位内部 div,并在内部 div 中设置 bottom:0 将其定位在底部。带有 float:leftwidth: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 set bottom:0 in your inner div to position it on the bottom. Small hack with float:left and width:100%, without float width:100% doesn't seem to work properly.

肥爪爪 2024-10-08 23:03:34
<div style="position: relative; width: 200px;">
   <img src="" />
   <div style="position: absolute; bottom: 0;">
       <!-- caption text here -->
   </div>
</div>
<div style="position: relative; width: 200px;">
   <img src="" />
   <div style="position: absolute; bottom: 0;">
       <!-- caption text here -->
   </div>
</div>
柠北森屋 2024-10-08 23:03:34
<style>
    div#container {
        position: relative;
        float: left;
    }

    div#container div {
        position: absolute;
        bottom: 0;
        background: #ccc;
    }
</style>

<div id="container">
    ....
<style>
    div#container {
        position: relative;
        float: left;
    }

    div#container div {
        position: absolute;
        bottom: 0;
        background: #ccc;
    }
</style>

<div id="container">
    ....
[旋木] 2024-10-08 23:03:34
  1. 您需要在#container上设置position:relative。这将使绝对定位相对于该容器 div 的边缘。

  2. bottom: 0; 添加到 .trans_caption 以使文本的基线(不是确切的底部)与底部对齐图片。如果您想将文本向上移动,请增加该数字。

  3. width: 100% 添加到 .trans_caption,使其与容器一样宽。

  4. 如果您想让标题居中,请将 text-align: center; 添加到 .trans_caption

  5. 请注意,图像 height 属性的 auto 值无效。

最好将 CSS 与 HTML 标记分开,放在单独的文件中。我们现在拥有的是(尝试一下)

#container {
    float:left;
    position:relative;
}

.trans_caption {
    background-color:#cccccc;
    bottom:0;
    position:absolute;
    text-align:center;
    width:100%;
}
  1. You need to set position: relative on #container. That will make the absolute positioning relative to the edges of that container div.

  2. 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.

  3. Add width: 100% to .trans_caption to make it as wide as its container.

  4. If you want to center the caption, add text-align: center; to .trans_caption.

  5. Note that the auto value for an image's height 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):

#container {
    float:left;
    position:relative;
}

.trans_caption {
    background-color:#cccccc;
    bottom:0;
    position:absolute;
    text-align:center;
    width:100%;
}
青春如此纠结 2024-10-08 23:03:34

绝对定位意味着该元素将定位在最后一个父级上未使用默认位置 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:

  • The Left, Right, Top, and Bottom nudge the positioning from their normal "static" position, and
  • Absolutely positioned elements will be positioned inside it.

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 at bottom: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 with text-align: center;, it should look the way you expect.

小耗子 2024-10-08 23:03:34

我认为你想要做的是在容器上设置 css background 属性。

像这样的东西

<div id="container" style="float:left; width:200px; height:100px; background:url('{{ p.sizes.2.url }}') no-repeat; position:relative">
<span style="position:absolute;bottom:0px">Caption goes here</span>
</div>

I think what you want to do is set the css background property on the container.

Something like this

<div id="container" style="float:left; width:200px; height:100px; background:url('{{ p.sizes.2.url }}') no-repeat; position:relative">
<span style="position:absolute;bottom:0px">Caption goes here</span>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文