文章中的内联图像和标题 - 使标题的宽度与图像的宽度一致
这是我的代码:
<div class="image">
<img src="image.jpg" alt="Image description" />
<p class="caption">This is the image caption</p>
</div>
这是我的 CSS:
.image {
position: relative;
float: right;
margin: 8px 0 10px 10px;
}
.caption {
font-weight: bold;
color: #444666;
}
如上所述,标题将符合 div.image 的宽度。我的问题通常是 img 的大小不同,从 100 像素宽度到 250 像素宽度。我想让标题宽度符合图像的相应宽度,无论大小如何。
虽然我也希望它更加语义化,但我不确定将 p.caption 与 img 切换起来有多容易。当然,我更喜欢这种方法,但我会一步一步地采取这种方法。
是否有一些 jquery 代码可以用来检测图像的宽度并将该宽度作为内联样式添加到标题标记中?
有更好的方法吗?我愿意接受建议。
Here's my code:
<div class="image">
<img src="image.jpg" alt="Image description" />
<p class="caption">This is the image caption</p>
</div>
Here's my CSS:
.image {
position: relative;
float: right;
margin: 8px 0 10px 10px;
}
.caption {
font-weight: bold;
color: #444666;
}
As is above, the caption will conform to the width of div.image. My problem is often the img varies in size, from 100px width to 250px width. I'd like to make the caption width conform to the corresponding width of the image, no matter the size.
While I'd love for this to be more semantic as well, I'm not sure how easy it would be to switch p.caption with img. Of course, I'd prefer that method but am taking this a step at a time.
Is there some jquery code that I can use to detect the width of the image and add that width as an inline style to the caption tag?
Is there a better way to do this? I'm open to suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下操作:
这还会将
移动到您要求的
之前。
为了进行测试,您可以在此处查看工作演示。
You can do something like this:
This also does the moving of
<p>
before the<img>
which you asked for.For testing, you can see a working demo here.