我曾经知道如何将图片放在顶部,然后将文本对齐到图片下方,以使其始终保持在图像的宽度边界内。然而,现在我不知道如何做到这一点。请问应该如何实现?
你的 HTML:
<div class="img-with-text">
<img src="yourimage.jpg" alt="sometext" />
<p>Some text</p>
</div>
如果您知道图片的宽度,您可以使用CSS:
如果您知道图像的宽度,则可使用CSS:
.img-with-text {
text-align: justify;
width: [width of img];
}
.img-with-text img {
display: block;
margin: 0 auto;
}
否则,图像下方的文本将自由流动。为了防止这种情况,只需为容器设置一个宽度即可。div
本身浮动到左侧或右侧,则无需设置宽度,内容仍然居中。Html/css应根据最大组件自动居中,而不是根据全宽居中。 - djmjposition:absolute
以便将图片放在我想要的位置时,文本就会隐藏在图片后面。 - Nickposition:absolute
将图像从文档流中移出,这意味着文本无法理解图像。最好使用边距、填充甚至position:relative
来定位图像。 - Jason<figcaption>
标签:<figure>
<img src="img.jpg" alt="my img"/>
<figcaption> Your text </figcaption>
</figure>
工作范例。
这将使"A"在图像下方居中:
<div style="text-align:center">
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/opentoselect.gif" />
<br />
A
</div>
<div style="text-align:center">
<img id="Image1" src="Images/opentoselect.gif" style="border-width:0px;" />
<br />
A
</div>
<style type="text/css">
#container { width: 100px; //whatever width you want }
#image {width: 100%; //fill up whole div }
#text { text-align: justify; }
</style>
<div id="container">
<img src="" id="image" />
<p id="text">oooh look! text!</p>
</div>
我不是HTML专家,但这是对我有效的方法:
<div class="img-with-text-below">
<img src="your-image.jpg" alt="alt-text" />
<p><center>Your text</center></p>
</div>