css – 图形标签中图像上的figcaption标签的中心和对齐宽度

我花了两天时间试图解决无花果/无花果问题无济于事.

我有一个Django应用程序,用户可以在其中提交图像,我正在使用图形和figcaption标签显示带有附带标题的图像.主要问题是标题宽度超出图片宽度.

我试图找出一种方法让图像保持相同的大小和标题,以相应地排列宽度.我也在使用Twitter-Bootstrap.我对所有解决方案持开放态度.任何意见,经验或建议都非常感谢.

更新:这是实际的HTML模板代码和CSS:

<div class="span4 offset2">
                {% if story.pic %}
                    <h2>Image</h2> 
                    <figure width="{{story.pic.width_field}}">
                    <img class="image"src="{{ story.pic.url }}" width="{{story.pic.width_field}}" alt="some_image_alt_text"/>
                    {% if story.caption %}
                        <figcaption>
                                                {{story.caption}}
                        </figcaption>
                    {% endif %}
                    </figure>
                {% endif %}
        </div>


 image {height:auto;}

 figure {margin:0; display:table;} 

figcaption {display:table-row;
max-width: 30%;
font-weight: bold;}

解决方法

原始方案
figure .image {
    width: 100%;
}

figure {
    text-align: center;
    display: table;
    max-width: 30%; /* demo; set some amount (px or %) if you can */
    margin: 10px auto; /* not needed unless you want centered */
}

关键是如果可以的话,在图元素上为img设置某种最大宽度,那么它将保持它和文本的约束.

See an example fiddle.

如果您以编程方式读取宽度

首先,这< figure width =“{{story.pic.width_field}}”>应该是这个< figure style =“width:{{story.pic.width_field}};”>.

第二,只做这个css(img或figcaption不需要其他东西; see fiddle):

figure {
    text-align: center;
    margin: 10px auto; /* not needed unless you want centered */
}

具有长文本的真正小图像仍然会有问题,as this fiddle shows.为了使它至少看起来干净,你可能会检查img的一些最小尺寸,如果它太小(比如100px),那么不要设置宽度该图将min-width设置为img大小,并将最大宽度设置为100px的阈值,如this fiddle shows.

相关文章

Css3如何实现鼠标移上变长特效?(图文+视频)
css3怎么实现鼠标悬停图片时缓慢变大效果?(图文+视频)
jquery如何实现点击网页回到顶部效果?(图文+视频)
css3边框阴影效果怎么做?(图文+视频)
css怎么实现圆角边框和圆形效果?(图文+视频教程)
Css3如何实现旋转移动动画特效