【发布时间】:2014-02-19 05:45:41
【问题描述】:
假设我有一个可以是可变宽度的图像(最小:100px,最大:100% [760px])。我还有一个<p> 元素,将显示在图像的正下方。我希望<p> 的宽度与<img> 相同,但我似乎找不到答案。
这是jsfiddle这种场景所涉及的代码:
html:
<div id='page'>
<figure>
<img src='http://www.myimage.com/img.jpg'/>
<p>Hi there. I am some text. I would like to start where the image starts :(</p>
</figure>
</div>
css:
#page {
width:760px; /* arbitrary */
}
figure img {
display: block;
border: 1px solid #333;
max-width: 100%;
min-width: 100px;
margin: 0 auto;
}
figure p {
/* ??? */
}
有什么想法吗?
【问题讨论】:
-
你喜欢这个jsfiddle.net/a9rEh/1??
-
您需要为
figure定义一个宽度,即图像的宽度。 -
@davidpauljunior 如果我不知道图像的宽度怎么办?你是说我必须使用 javascript 来读取图像大小,然后根据它改变 css?
-
@Beginner 不幸的是,没有。正如我所提到的,文本将直接显示在图像下方。
-
你这样做的目的是什么?您要制作字幕吗?