关键是要不是设置为img
元素或父容器的宽度。如果父母.image
只是简单地浮动或以任何其他方式进行调整以使其缩小到其内容的大小,则应该起作用。
我用float
来实现收缩包装的方面,但position: absolute;
会做同样的事情,display: inline-block;
。
在JS Bin上有一个演示,它使用一些jQuery来交换图像,但它对任何元素的宽度没有影响。这个CSS被复制如下:
.image {
float: left; // for the shrink wrap
padding: 1em; // To achieve the bordered effect
background-color: #666; // just for contrast
-moz-border-radius: 2em; // for that web 2.0 goodness...
-webkit-border-radius: 2em;
border-radius: 2em;
}
.image img {
-moz-border-radius: 2em; // no width, anywhere. Presumably width: auto, would
-webkit-border-radius: 2em; // work, but that's redundant, since it's the default
border-radius: 2em;
}
.image img + .caption {
width: 100%; // forcing the .caption to take up 100% of the width
background-color: #ffa; // of its parent, except for the padding, so that it's
} // the same width as the image above.
你可以显示标记和相应的CSS规则吗? – Oded 2010-10-08 18:29:45
如果您可以使用JavaScript,请参阅http://stackoverflow.com/questions/2839248/inline-image-and-caption-in-article-conform-captions-width-to-images-width。 – ClarkeyBoy 2010-10-08 19:23:31