微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/css/215974.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。