正确对齐图像标题
我怎样才能实现这样的布局?
现在我正在使用这个 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):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
关键是不要为
img
元素或父容器设置宽度。如果父级.image
只是浮动或以任何其他方式进行调整,以便缩小到其内容的大小,那么这应该可行。我使用
float
来实现收缩包装方面,但position:absolute;
会做同样的事情,就像display: inline-block;
。JS Bin 有一个演示,它使用一些 jQuery 来交换图像,但它对任何元素的宽度。 CSS 复制如下:
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, butposition: absolute;
would do the same, as woulddisplay: 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:
正如@Kyle 所说,块元素会调整其宽度以适合其父元素。
不过,将块元素设置为内联并不是正确的方法:您需要做的是将 .image div 设置为浮动元素,从而实现类似的结果,同时保留块元素的功能。实现这一技巧的 css 应该是:
我将您可能认为需要的任何进一步的样式改进留给了您。
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:
I left to you any further style improvement you may feel needed.
如果将
.image
框的宽度设置为与图像相同的宽度,然后将 padding 应用于.image
框,您将获得您正在寻找的边框因为当您指定宽度时,会添加填充。基本上,您需要以下 CSS:
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:
尝试以下操作:
现在我对标题 div 应用 3 个边框的原因是因为您不知道没有边框的图像的宽度,但您确实知道图像边框的宽度。对标题应用相同的边框将使标题具有相同的宽度。当然,您需要调整 .image 的宽度和 img 标签的高度(这可以通过 css 完成),但其余的将为您完成。此外,标题 div 也会调整大小以获得更大的标题。
此致,
理查德
PS 这段代码已经在 Chrome 中尝试和测试 - 它工作得很好。
Try the following:
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.
由于 div 是块级元素,因此它们会扩展以适合其父元素。
这可能不是最好的解决方案,但如果您提前不知道图像的大小,您可以执行以下操作:
上面的操作将导致 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:
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.我想出了另一个解决方案。我不相信大卫·托马斯的回答会让标题出现在图像中(如果我错了,请务必纠正我),所以尝试下面的代码(我使用了我的代码和大卫的组合)。
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).