How do I put text that is inside a P element next to a image?

前端 未结 4 1046
感情败类
感情败类 2021-01-19 23:58

Here is my code:


This is some example text that i want next to th

相关标签:
4条回答
  • 2021-01-20 00:18

    you can do it without changing the html structure

    As img is a inline element you can set the next sibling p to inline

    By default p is a block element so it is taking full width

    more about + selector

    img + p {
      display: inline;
      margin-left: 10px;
      vertical-align: top; /* so that p aligns to the top of img*/
    }
    <img id="mepic" src="http://placehold.it/70x60" width="70" height="60">
    <p>This is some example text that i want next to the image on the right hand side</p>

    0 讨论(0)
  • 2021-01-20 00:26

    As a side note, since you tagged this as HTML5, you would be better off using <figure> and <figcaption> to mark up this content.

    For example:

    <figure>
       <img id="mepic" src="images/example.jpg" alt="Text" />
       <figcaption>This is some example text that i want next to the image on the right hand side</figcaption>
    </figure>
    

    Then you could style it like:

    figure {
       margin: 0;
       width: 600px;
       height: auto;
       overflow: hidden;
    }
    figure img {
       width: 30%;
       float: left;
    }
    figure figcaption {
       width: 70%;
       float: right;
    }
    
    0 讨论(0)
  • 2021-01-20 00:28

    If you want to place text to right side of an image, here's code which will allow it:

       <p><img src="example.jpg" alt="img"> Image text here.</p>
    

    Running example: https://jsfiddle.net/apc5spcu/

    Alternately, you could use divs and make a nicer looking image form.

    HTML

    <div class="container">
        <div class="imgdiv">
            <img src="example.jpg" alt="img">
        </div>
        <div class="textdiv">
            <p> Image text here.</p>
            <p> Image text here.</p>
            <p> Image text here.</p>
            <p> Image text here.</p>
        </div>
    

    This code makes use of 'Float' which positions each division next to eachother on left and right sides.

    CSS

    .container {
        width:540px;
    }
    .imgdiv {
        float:left;
        width:140px;
    }
    .textdiv {
        float:right;
        width:300px;
    }
    

    Running example: https://jsfiddle.net/apc5spcu/1/

    0 讨论(0)
  • 2021-01-20 00:36

    NO CSS & JavaScript, PURE HTML

    <p><img src="image.png" alt="image"/>&nbsp;Text beside image</p>
    

    Place <img> inside a <p> can put the image surround by text. Read more here.

    NOTE: <img> tag has display: inline by default and &nbsp; is basic a non-breaking space, read more here.

    <p>This is some text.&nbsp;
        <img src="http://placehold.it/60x60" alt="Smiley face">&nbsp;This is some text.</p><br/>
    <p>
        <img src="http://placehold.it/60x60" alt="Smiley face" align="middle">&nbsp;This text is next to the image</p>

    0 讨论(0)
提交回复
热议问题