HTML/CSS: how to put text both right and left aligned in a paragraph

后端 未结 6 1675
情歌与酒
情歌与酒 2020-12-28 18:54

What would be the best code to have two bits of text in a single paragraph, one left aligned, the other right aligned, that also is:

  • the least code as possible
相关标签:
6条回答
  • 2020-12-28 19:39

    Least amount of markup possible (you only need one span):

    <p>This text is left. <span>This text is right.</span></p>
    

    How you want to achieve the left/right styles is up to you, but I would recommend an external style on an ID or a class.

    The full HTML:

    <p class="split-para">This text is left. <span>This text is right.</span></p>
    

    And the CSS:

    .split-para      { display:block;margin:10px;}
    .split-para span { display:block;float:right;width:50%;margin-left:10px;}
    
    0 讨论(0)
  • 2020-12-28 19:41

    I have used this in the past:

    html

    January<span class="right">2014</span>
    

    Css

    .right {
        margin-left:100%;
    }
    

    Demonstration

    0 讨论(0)
  • 2020-12-28 19:41

    Ok what you probably want will be provide to you by result of:

    1. in CSS:

      div { column-count: 2; }

    2. in html:

      <div> some text, bla bla bla </div>

    In CSS you make div to split your paragraph on to column, you can make them 3, 4...

    If you want to have many differend paragraf like that, then put id or class in your div:

    0 讨论(0)
  • 2020-12-28 19:45

    I wouldn't put it in the same <p>, since IMHO the two infos are semantically too different. If you must, I'd suggest this:

    <p style="text-align:right">
     <span style="float:left">I'll be on the left</span>
     I'll be on the right
    </p>
    
    0 讨论(0)
  • 2020-12-28 19:48

    The only half-way proper way to do this is

    <p>
      <span style="float: right">Text on the right</span>
      <span style="float: left">Text on the left</span>
    </p> 
    

    however, this will get you into trouble if the text overflows. If you can, use divs (block level elements) and give them a fixed width.

    A table (or a number of divs with the according display: table / table-row / table-cell properties) would in fact be the safest solution for this - it will be impossible to break, even if you have lots of difficult content.

    0 讨论(0)
  • 2020-12-28 19:53

    enter image description here

    If the texts has different sizes and they must be underlined this is the solution:

    <table>
      <tr>
        <td class='left'>January</td>
        <td class='right'>2014</td>
      </tr>
    </table>
    

    css:

    table{
        width: 100%;
        border-bottom: 2px solid black;
        /*this is the size of the small text's baseline over part  (≈25px*3/4)*/
        line-height: 19.5px; 
    }
    table td{
        vertical-align: baseline;
    }
    .left{
        font-family: Arial;
        font-size: 40px;
        text-align: left;
    
    }
    .right{
        font-size: 25px;
        text-align: right;
    }
    

    demo here

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