自动将<div>的宽度指定为包含<img/>的<img src="https://i.stack.imgur.com/WNJUx.png" alt="enter image description here"/>,其中图像的宽度未知

时间:2016-09-13 19:58:53

标签: css css3

我不确定我的标题最佳,但这是对我的问题的简单解释。

我有一个像这样的HTML结构:

<div class="media-container" style="float: left">
  <img src="..." />
  <p class="caption">Here is a long caption that will extend beyond the width of the image.</p>
</div>

这就是它最终看起来像:

enter image description here

我希望字幕文本在达到图像宽度后换行,所以它看起来像这样:

{{0}}

我可以通过在容器div上设置固定来得到它我想要的方式,但我不知道提前宽度,所以没有办法。

对于这个问题我有什么解决方案可以在没有JavaScript的情况下在纯CSS中实现吗?

2 个答案:

答案 0 :(得分:1)

使用display: table;

.media-container {
  display: table;
}

.caption {
  display: table-caption;
  caption-side: bottom;
}
<div class="media-container" style="float: left">
  <img width="100" src="https://d1ra4hr810e003.cloudfront.net/media/27FB7F0C-9885-42A6-9E0C19C35242B5AC/0/D968A2D0-35B8-41C6-A94A0C5C5FCA0725/F0E9E3EC-8F99-4ED8-A40DADEAF7A011A5/dbe669e9-40be-51c9-a9a0-001b0e022be7/thul-IMG_2100.jpg" />
  <p class="caption">Here is a long caption that will extend beyond the width of the image.</p>
</div>

小提琴:https://jsfiddle.net/4pL4wm9h/

答案 1 :(得分:0)

将以下内容添加到父元素:

  width: -moz-min-content;
  width: -webkit-min-content;
  width: min-content;

可以找到min-content的详细信息here

PLUNKER

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>
  <style>
    figure {
      float: left;
      width: -moz-min-content;
      width: -webkit-min-content;
      width: min-content;
    }
  </style>
</head>

<body>
  <p>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by
    arches into stiff sections.</p>

  <p>The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. "What's happened to me? " he thought. It wasn't a dream.</p>


  <figure>
    <img src="http://i.imgur.com/sbxyLKX.png">
    <figcaption>Here is a long caption that will extend beyond the width of the image.</figcaption>
  </figure>


  <p>His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had
    recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then
    turned to look out the window at the dull weather. Drops</p>
  <p>A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine. I am so
    happy, my dear friend, so absorbed in the exquisite sense of mere tranquil existence, that I neglect my talents. I should be incapable of drawing a single stroke at the present moment; and yet I feel that I never was a greater artist than now. When,
    while the lovely valley teems with vapour around me, and the meridian sun strikes the upper surface of the impenetrable foliage of my trees, and but a few stray gleams steal into the inner sanctuary, I throw myself down among the tall grass by the
    trickling stream; and, as I lie close to the earth, a thousand unknown plants are noticed by me: when I hear the buzz of the little world among the stalks, and grow familiar with the countless indescribable forms of the insects and flies, then I feel
    the presence of the Almighty, who formed us in his own image, and the breath</p>
</body>

</html>
&#13;
&#13;
&#13;