如何在图像下方直接对齐文本?

时间:2009-08-03 23:39:09

标签: html css text justify

我曾经知道如何将图像放在顶部,然后对齐图像下方的文本,使其保持在图像宽度的边框内。但是,现在我不知道该怎么做。这是如何完成的?

5 个答案:

答案 0 :(得分:55)

您的HTML:

<div class="img-with-text">
    <img src="yourimage.jpg" alt="sometext" />
    <p>Some text</p>
</div>

如果您知道图片的宽度,那么您的CSS:

.img-with-text {
    text-align: justify;
    width: [width of img];
}

.img-with-text img {
    display: block;
    margin: 0 auto;
}

否则图像下方的文字将自由流动。要防止这种情况,只需为容器设置宽度。

答案 1 :(得分:34)

您可以使用HTML5 <figcaption>

<figure>
  <img src="img.jpg" alt="my img"/>
  <figcaption> Your text </figcaption>
</figure>

Working example

答案 2 :(得分:4)

为了能够证明文本的合理性,您需要知道图像的宽度。您可以使用图像的正常宽度,或使用不同的宽度,但IE 6可能会让你胡思乱想,而不是缩放。

以下是您的需求:

<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>

答案 3 :(得分:0)

这使图像下方的“A”居中:

<div style="text-align:center">
  <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/opentoselect.gif" />
  <br />
  A
</div>

这就是ASP.Net,它会将HTML呈现为:

<div style="text-align:center">
<img id="Image1" src="Images/opentoselect.gif" style="border-width:0px;" />
<br />
A
</div>

答案 4 :(得分:0)

我不是HTML方面的专家,但这对我有用:

<div class="img-with-text-below">
    <img src="your-image.jpg" alt="alt-text" />
    <p><center>Your text</center></p>
</div>