How can I align text directly beneath an image?

前端 未结 5 1379
臣服心动
臣服心动 2020-12-01 05:05

I used to know how to put an image on top and then justify the text below the image so that it stays within the borders of the width of the image. However, now I have no id

相关标签:
5条回答
  • 2020-12-01 05:47

    You can use HTML5 <figcaption>:

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

    Working example.

    0 讨论(0)
  • 2020-12-01 05:51

    Your HTML:

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

    If you know the width of your image, your CSS:

    .img-with-text {
        text-align: justify;
        width: [width of img];
    }
    
    .img-with-text img {
        display: block;
        margin: 0 auto;
    }
    

    Otherwise your text below the image will free-flow. To prevent this, just set a width to your container.

    0 讨论(0)
  • 2020-12-01 05:51

    This centers the "A" below the image:

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

    That is ASP.Net and it would render the HTML as:

    <div style="text-align:center">
    <img id="Image1" src="Images/opentoselect.gif" style="border-width:0px;" />
    <br />
    A
    </div>
    
    0 讨论(0)
  • 2020-12-01 05:53

    In order to be able to justify the text, you need to know the width of the image. You can just use the normal width of the image, or use a different width, but IE 6 might get cranky at you and not scale.

    Here's what you need:

    <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>
    
    0 讨论(0)
  • 2020-12-01 05:55

    I am not an expert in HTML but here is what worked for me:

    <div class="img-with-text-below">
        <img src="your-image.jpg" alt="alt-text" />
        <p><center>Your text</center></p>
    </div>
    
    0 讨论(0)
提交回复
热议问题