图像标题宽度与图像相同

发布于 2024-10-17 12:04:28 字数 277 浏览 5 评论 0原文

如何使图像标题宽度与图像相同?现在我有以下代码:

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

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

发布评论

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

评论(7

清音悠歌 2024-10-24 12:04:28

所以问题是你不知道img会有多宽,并且img的标题可能会超过img的宽度,在这种情况下你想将标题的宽度限制为img的宽度。

就我而言,我在父元素上应用了 display:table ,并在父元素上应用了 display:table-captioncaption-side:bottom标题元素如下:

<div class="image" style="display:table;">
    <img src="foo.jpg" alt="" />
    <div style="display:table-caption;caption-side:bottom;">This is the caption.</div>
</div>

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 applied display:table-caption and caption-side:bottom on the caption element like this:

<div class="image" style="display:table;">
    <img src="foo.jpg" alt="" />
    <div style="display:table-caption;caption-side:bottom;">This is the caption.</div>
</div>
月寒剑心 2024-10-24 12:04:28

您可以应用 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 like figure 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:

缱倦旧时光 2024-10-24 12:04:28

如果问题是标题太长,您可以随时使用


div.image {
    width: 200px; /* the width you want */
    max-width: 200px; /* same as above */ 
}
div.image div {
    width: 100%;
}

并且标题将保持静态。另外,标签名称是img,而不是image。

或者,如果您想检测图像宽度,可以使用 jQuery:


$(document).ready(function() {
    var imgWidth = $('.image img').width();
    $('.image div').css({width: imgWidth});
});

这样,您就可以获取图像宽度,然后为其设置标题宽度。

If the problem is the caption being too long, you can always use


div.image {
    width: 200px; /* the width you want */
    max-width: 200px; /* same as above */ 
}
div.image div {
    width: 100%;
}

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:


$(document).ready(function() {
    var imgWidth = $('.image img').width();
    $('.image div').css({width: imgWidth});
});

That way, you're getting the image width, then you set the caption width to it.

梦归所梦 2024-10-24 12:04:28

正确添加标题的唯一方法是将图像和标题包含在由带有 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.

独留℉清风醉 2024-10-24 12:04:28

扩展上述内容,我使用此 jQuery 代码片段来检查代码中具有标题的所有图像,并将标题宽度设置为与前一个图像的宽度相匹配。所以如果 HTML 是这样的:

<img src="blah" /><figcaption>this is the first caption</figcaption>
<img src="blah2" /><figcaption>this is the second caption</figcaption>

那么使用这个 jQuery:

$("figcaption").each(function () { 
    $(this).css("width", $(this).prev("img").width()) 
});

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:

<img src="blah" /><figcaption>this is the first caption</figcaption>
<img src="blah2" /><figcaption>this is the second caption</figcaption>

then use this jQuery:

$("figcaption").each(function () { 
    $(this).css("width", $(this).prev("img").width()) 
});
初见终念 2024-10-24 12:04:28

您可以尝试设置图像 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 :)

乙白 2024-10-24 12:04:28

关键是将图像视为具有一定的宽度和长度。在下面的示例中,我检查了图像的宽度为 200 像素。然后将标题视为内联块。

HTML:

<div style="width:200px;">
   <img src="anImage.png" alt="Image description"/>
   <div class="caption">This can be as long as you want.</div>
</div>

CSS:

.caption {
   display: inline-block;
}

您还可以通过将上面的 css 替换为以下内容之一,将内联块替换为左对齐、右对齐或拉伸到精确图像宽度的文本:

.caption {
   /* text-align: left; */
   /* text-align: right; */
   text-align: justify;
}

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:

<div style="width:200px;">
   <img src="anImage.png" alt="Image description"/>
   <div class="caption">This can be as long as you want.</div>
</div>

CSS:

.caption {
   display: inline-block;
}

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:

.caption {
   /* text-align: left; */
   /* text-align: right; */
   text-align: justify;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文