图像标题宽度与图像相同
如何使图像标题宽度与图像相同?现在我有以下代码:
<div class="image">
<img src="foo.jpg" alt="" />
<div>This is the caption.</div>
</div>
我已经尝试了很多东西(浮动、绝对定位等),但是如果标题很长,它总是使 div 变宽而不是多行。问题是我不知道图像的宽度(或标题的长度)。解决这个问题的唯一方法是使用表吗?
How can I make image caption width same as image? Now I have the following code:
<div class="image">
<img src="foo.jpg" alt="" />
<div>This is the caption.</div>
</div>
I've tried a lot of things (floating, absolute positioning etc), but if the caption is long, it always makes the div wide instead of going on many lines. The problem is that I don't know the width of image (or the length of caption). Is the only way to solve this use tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
所以问题是你不知道img会有多宽,并且img的标题可能会超过img的宽度,在这种情况下你想将标题的宽度限制为img的宽度。
就我而言,我在父元素上应用了
display:table
,并在父元素上应用了display:table-caption
和caption-side:bottom
标题元素如下:So the problem is that you don't know how wide the img will be, and the caption for the img may exceed the width of the img, in which case you want to restrict the width of the caption to the width of the img.
In my case, I applied
display:table
on the parent element, and applieddisplay:table-caption
andcaption-side:bottom
on the caption element like this:您可以应用
display:table;
和任意初始宽度,例如。width:7px;
到像figure
这样的父块,一切都会按预期工作!这是一个现场演示:
http://jsfiddle.net/blaker/8snwd/
此解决方案的限制是 IE 7 及更早版本不支持
display:table;
并且 IE 8 要求您的文档类型为!DOCTYPE
。资料来源:
You can apply
display:table;
and an arbitrary initial width, eg.width:7px;
to the parent block likefigure
and everything will work as expected!Here is a live demo:
http://jsfiddle.net/blaker/8snwd/
This solution's limitation is that IE 7 and earlier do not support
display:table;
and IE 8 requires your doctype to be!DOCTYPE
.Sources:
如果问题是标题太长,您可以随时使用
并且标题将保持静态。另外,标签名称是img,而不是image。
或者,如果您想检测图像宽度,可以使用 jQuery:
这样,您就可以获取图像宽度,然后为其设置标题宽度。
If the problem is the caption being too long, you can always use
And the caption will stay static. Also, the tag name is img, not image.
Or, if you want to detect your image width, you can use jQuery:
That way, you're getting the image width, then you set the caption width to it.
正确添加标题的唯一方法是将图像和标题包含在由带有 table、table-row 和 table-cell css 属性的 span 元素构建的表格中。
任何其他方法(包括 HTML5 图形标签)要么会导致宽度不匹配,要么会导致不必要的换行。
如果您的方法必须在非 CSS3 浏览器中运行,那么表格可能是最好的选择。
The only way to do captioning properly is to enclose the image and caption in a table constructed from span elements with table, table-row and table-cell css attributes.
Any other method (including HTML5 figure tags) either gives width mismatches or causes unwanted line breaks.
If your method must work in a non-css3 browser, then a table is probably the best bet.
扩展上述内容,我使用此 jQuery 代码片段来检查代码中具有标题的所有图像,并将标题宽度设置为与前一个图像的宽度相匹配。所以如果 HTML 是这样的:
那么使用这个 jQuery:
Expanding on the above, I use this jQuery snippet to examine all images in my code that have captions, and set the caption width to match the width of the previous image. So if the HTML is like:
then use this jQuery:
您可以尝试设置图像 div 包装
display:inline-block
http://jsfiddle .net/steweb/98Xvr/
如果标题很长,唯一的解决办法就是设置固定宽度,或者通过js设置.image div宽度。我正在尝试考虑一个纯 css 解决方案,但我认为这是一个艰巨的挑战:)
You could try to set the image div wrapper
display:inline-block
http://jsfiddle.net/steweb/98Xvr/
If the caption is long, the only solution will be to set a fixed width, or set the .image div width by js. I'm trying to think about a pure css solution, but I think it's an hard challenge :)
关键是将图像视为具有一定的宽度和长度。在下面的示例中,我检查了图像的宽度为 200 像素。然后将标题视为内联块。
HTML:
CSS:
您还可以通过将上面的 css 替换为以下内容之一,将内联块替换为左对齐、右对齐或拉伸到精确图像宽度的文本:
The key is to treat the image as having a certain width some length. In the example below I checked and my image was 200px wide. And then treat the caption as an inline-block.
HTML:
CSS:
You can also replace the inline-block with text that is left justified, right, or stretched over the exact image width by replacing above css with one of the following: