正确对齐图像标题

发布于 2024-09-26 09:07:10 字数 576 浏览 7 评论 0原文

我怎样才能实现这样的布局?

现在我正在使用这个 HTML:

<div class="image">
  <img>
  <div class="caption">
    Caption Text
  </div>
</div>

和这个 CSS:

.image {
  background-color: #2A2A2A;
}

img {
  max-width: 590px;
}

但是 .image 框太大了(因为它扩展以适合其父级):

替代文字

How can I achieve a layout like this?

Right now I'm using this HTML:

<div class="image">
  <img>
  <div class="caption">
    Caption Text
  </div>
</div>

And this CSS:

.image {
  background-color: #2A2A2A;
}

img {
  max-width: 590px;
}

But the .image box is too big (since it expands to fit its parent):

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

遗心遗梦遗幸福 2024-10-03 09:07:10

关键是不要为 img 元素或父容器设置宽度。如果父级 .image 只是浮动或以任何其他方式进行调整,以便缩小到其内容的大小,那么这应该可行。

我使用 float 来实现收缩包装方面,但 position:absolute; 会做同样的事情,就像 display: inline-block;

JS Bin 有一个演示,它使用一些 jQuery 来交换图像,但它对任何元素的宽度。 CSS 复制如下:

  .image {
    float: left;    // for the shrink wrap
    padding: 1em;   // To achieve the bordered effect
    background-color: #666;  // just for contrast
    -moz-border-radius: 2em;  // for that web 2.0 goodness...
    -webkit-border-radius: 2em;
    border-radius: 2em;
  }
  .image img {
    -moz-border-radius: 2em;    // no width, anywhere. Presumably width: auto, would  
    -webkit-border-radius: 2em; // work, but that's redundant, since it's the default
    border-radius: 2em;
  }
  .image img + .caption {
    width: 100%;              // forcing the .caption to take up 100% of the width
    background-color: #ffa;   // of its parent, except for the padding, so that it's
  }                           // the same width as the image above.

The key is to not set a width for the img element, or the parent container. If the parent, .image is simply floated or in any other way adapted so that it shrinks to the size of its contents, this should work.

I used float to achieve the shrink-wrap aspect, but position: absolute; would do the same, as would display: inline-block;.

There's a demo over at JS Bin, which uses some jQuery to swap the images around, but it does nothing to the width of any elements. The CSS is reproduced below:

  .image {
    float: left;    // for the shrink wrap
    padding: 1em;   // To achieve the bordered effect
    background-color: #666;  // just for contrast
    -moz-border-radius: 2em;  // for that web 2.0 goodness...
    -webkit-border-radius: 2em;
    border-radius: 2em;
  }
  .image img {
    -moz-border-radius: 2em;    // no width, anywhere. Presumably width: auto, would  
    -webkit-border-radius: 2em; // work, but that's redundant, since it's the default
    border-radius: 2em;
  }
  .image img + .caption {
    width: 100%;              // forcing the .caption to take up 100% of the width
    background-color: #ffa;   // of its parent, except for the padding, so that it's
  }                           // the same width as the image above.
倾城泪 2024-10-03 09:07:10

正如@Kyle 所说,块元素会调整其宽度以适合其父元素。
不过,将块元素设置为内联并不是正确的方法:您需要做的是将 .image div 设置为浮动元素,从而实现类似的结果,同时保留块元素的功能。实现这一技巧的 css 应该是:

.image {
  float: left;
  display: inline; /* needed to fix the (IE <= 6) "3 pixels out of nowhere bug" */
  /* whatever code you may find appropriate in order to render the rounded border */
}
.image .caption {
  clear: left;
}

我将您可能认为需要的任何进一步的样式改进留给了您。

As @Kyle said, block elements adjust their width to fit their parent's.
Setting a block element as inline though, is not the correct approach: what you need to do, is to set the .image div as a floating element, thus achieving a similar result, while keeping the features of a block element. The css to do the trick should be:

.image {
  float: left;
  display: inline; /* needed to fix the (IE <= 6) "3 pixels out of nowhere bug" */
  /* whatever code you may find appropriate in order to render the rounded border */
}
.image .caption {
  clear: left;
}

I left to you any further style improvement you may feel needed.

天冷不及心凉 2024-10-03 09:07:10

如果将 .image 框的宽度设置为与图像相同的宽度,然后将 padding 应用于 .image 框,您将获得您正在寻找的边框因为当您指定宽度时,会添加填充。

基本上,您需要以下 CSS:

.image {
    padding: 10px;
    width: 300px; /* assuming the picture is 300px */
}

If you set the width of the .image box to the same width as the image, then apply padding to the .image box, you will get the border you are looking for because when you specify width, padding gets added to it.

So basically, you would need the following CSS:

.image {
    padding: 10px;
    width: 300px; /* assuming the picture is 300px */
}
少女的英雄梦 2024-10-03 09:07:10

尝试以下操作:

.image {
    position: relative;
    width: 250px;
}
img {
    border: 15px solid #777777;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
    width: 100%;
}
.caption {
    border-left: 15px solid #777777;
    border-right: 15px solid #777777;
    border-bottom: 15px solid #777777;
    position: absolute;
    width: 100%;
    bottom: 0px;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
}

<div class="image">
    <img src="yourImage" height="150px" />
    <div class="caption">
        Caption TextCaption TextCaption TextCaption TextCaption Text
    </div>
</div>

现在我对标题 div 应用 3 个边框的原因是因为您不知道没有边框的图像的宽度,但您确实知道图像边框的宽度。对标题应用相同的边框将使标题具有相同的宽度。当然,您需要调整 .image 的宽度和 img 标签的高度(这可以通过 css 完成),但其余的将为您完成。此外,标题 div 也会调整大小以获得更大的标题。

此致,

理查德

PS 这段代码已经在 Chrome 中尝试和测试 - 它工作得很好。

Try the following:

.image {
    position: relative;
    width: 250px;
}
img {
    border: 15px solid #777777;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
    width: 100%;
}
.caption {
    border-left: 15px solid #777777;
    border-right: 15px solid #777777;
    border-bottom: 15px solid #777777;
    position: absolute;
    width: 100%;
    bottom: 0px;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
}

<div class="image">
    <img src="yourImage" height="150px" />
    <div class="caption">
        Caption TextCaption TextCaption TextCaption TextCaption Text
    </div>
</div>

Now the reason I have applied 3 borders to the caption div is because you do not know the width of the image without the border, but you do know the width of the border for the image. Applying the same border to the caption will give the caption the same width. Of course you will need to adjust the width of .image and the height of the img tag (this can be done through css), but the rest will be done for you. Also the caption div will resize for larger captions.

Regards,

Richard

PS this code has been tried and tested in Chrome - it works fine.

娇女薄笑 2024-10-03 09:07:10

由于 div 是块级元素,因此它们会扩展以适合其父元素。

这可能不是最好的解决方案,但如果您提前不知道图像的大小,您可以执行以下操作:

.image
{  
    padding: 10px;
    max-width: 590px;  
    disply: inline;  
}

.caption
{  
    background-color: #2A2A2A;
    disply: inline;  
}

上面的操作将导致 img div 被渲染为内联元素,这将缩小它以适应内容而不是其父级,并且 padding 将添加边框。

Since divs are block-level elements, they expand to fit their parent.

It may not be the best solution, but if you don't know the size of the image ahead of time, you could do the below:

.image
{  
    padding: 10px;
    max-width: 590px;  
    disply: inline;  
}

.caption
{  
    background-color: #2A2A2A;
    disply: inline;  
}

The above will cause the img div to be rendered as an inline element which will shrink it to fit the content rather than its parent, and the padding will add the border.

顾北清歌寒 2024-10-03 09:07:10

我想出了另一个解决方案。我不相信大卫·托马斯的回答会让标题出现在图像中(如果我错了,请务必纠正我),所以尝试下面的代码(我使用了我的代码和大卫的组合)。

.image {
    position: relative;
    float: left;
    border: 15px solid #777777;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
}
.caption {
    position: absolute;
    bottom: 5px;
    left: 5px;
}
.image-container {
    position: relative;
}

<div class="image">
    <img src="/Images/header1.png" />
    <div class="caption">
        Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text
    </div>
</div>

I have come up with another solution. I dont believe David Thomas' answer makes the caption appear within the image (by all means correct me if I am wrong), so try the code below (I have used a combination of my code and Davids).

.image {
    position: relative;
    float: left;
    border: 15px solid #777777;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
}
.caption {
    position: absolute;
    bottom: 5px;
    left: 5px;
}
.image-container {
    position: relative;
}

<div class="image">
    <img src="/Images/header1.png" />
    <div class="caption">
        Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text
    </div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文