& with floating image, figcaption wraps, and article text wraps around image/caption

问题描述 投票:4回答:3

在网上搜索了几个星期但找不到满足我所有需求的答案(只是部分),非常欢迎帮助。

我想要和已经完成的事情:

  • 简单的HTML5兼容和CSS
  • 在文章中显示图像
  • 图片必须有标题
  • 标题必须在图像下方
  • 标题必须限制为图像的水平尺寸
  • 标题可能长于一行,文本应换行到下一行(仍在图像大小内)
  • 图像和标题必须作为一组向左或向右浮动(想想使用<figure class="left">
  • 文章的文字必须包裹图像和标题
  • 图像大小不同(第一个图像例如是200像素,第二个图像在文本中的其他位置是320像素等)

现在我想添加这些要求:

  • 我不喜欢像<figure class="left" style="width:200px">那样添加图像的原始宽度,但前提是没有其他方法。
  • 响应式设计:当图像宽度占据显示宽度的50%以上时,必须将其限制为显示宽度的50%。
  • 当显示宽度为320px或更低时,图像不能浮动但必须是块级元素,因此图像周围没有文章的文字换行。

我离开的地方:

figure {
  display: inline
}

figcaption {
  display: block
}

figure.left {
  float: left
}

figure.right {
  float: right
}
<p>This is a part of the text of the article and at this point a image is inserted at the left side
  <figure class="left" style="width:250px">
    <img src="image.png" alt="img txt">
    <figcaption>image caption and a lot of text</figcaption>
  </figure>
  and the article text goes on and on so that it will wrap around the image</p>

(我省略了填充/边距等,以使其看起来更好。)

css html5 figure
3个回答
2
投票

尝试以下css并在调整浏览器大小时查看图像上的文本换行:

.left {
  float: left;
  border: 5px solid #BDBDBD;
  background: #E0E0E0;
  padding: 5px;
  margin: 0px;
}

figcaption {
  text-align: center;
}

1
投票

也许这对你来说太难了,但我找到了这个答案:

http://www.sitepoint.com/forums/showthread.php?1049396-How-to-force-this-figcaption-element-to-respect-its-parent-s-width-boundaries

figure {
  display: table;
}
figcaption {
  display: table-caption;
  caption-side: bottom;
}

我不认为这是HTML5或CSS3禁止的,它似乎对我有用。

暂且没有响应式设计要求 - 我觉得这是一个单独的问题,我没有一个很好的CSS唯一的答案。


0
投票

这可能有助于某人解决它。我也在寻找一种方法来显示图像右侧的标题,其高度与图像相同。

figure, .figure {
  
     display: inline-table;
}

figcaption {
    display: table-caption;
     caption-side: bottom;
      background-color: red;
  
}
     
img {
     width: 100%;
}
     
.image {
     width: auto;
}
     
     figure div {
          display: inline-table;
          background-color: aqua;
     }
     
.caption {
    display: table-caption;
     caption-side: top;
      background-color: red;
     margin: 0;
}
<figure>
    <h2 class="caption">caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption </h2>
    
        <img src="http://mintnet.net/images/thumbs/small-mossy.jpg"> 
        <figcaption>caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption </figcaption>
</figure>

<figure>
    <figcaption>caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption </figcaption>
    
        <img src="http://mintnet.net/images/thumbs/small-mossy.jpg">
       
</figure>

<figure>
    <div class="caption">caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption caption </div>
    
       <div><img  class="image" src="http://mintnet.net/images/thumbs/small-mossy.jpg">
            </div> 
       
</figure>
© www.soinside.com 2019 - 2024. All rights reserved.