CSS: Left, Center, & Right Align Text on Same Line

前端 未结 8 813
借酒劲吻你
借酒劲吻你 2020-12-04 18:03

I need to left, center, & right align text on the same line. I have the following text:

  • Left Align: 1/10
  • Center: 02:27
  • R
相关标签:
8条回答
  • 2020-12-04 18:29

    Improvement on Pod's answer:

    #textbox {
        display: flex;
    }
    .alignleft {
        flex: 1;
        text-align: left;
    }
    .aligncenter {
        flex: 1;
        text-align: center;
    }
    .alignright {
        flex: 1;
        text-align: right;
    }
    <div id="textbox">
    <p class="alignleft">1/10</p>
    <p class="aligncenter">02:27</p>
    <p class="alignright">100%</p>
    </div>
    
     

    This avoids the awkward box wrapping that occurs with floats on a small screen, as well as centering the middle text, in the middle of #textbox.

    JSFiddle:

    http://jsfiddle.net/secnayup/

    0 讨论(0)
  • 2020-12-04 18:33

    A variation of JCBiggar's great solution is to skip the width and text-align for .alignright, but simply to float it to the right. The advantage of this is that it removes the concern that the inner elements have set margins or paddings, making the 33.33% * 3 exceed the 100% width of the containing element. The desired placement of .alignright can be effected much more simply.

    The CSS would then be:

    .alignleft {
      float: left;
      width:33.33333%;
      text-align:left;
      padding-left: 10px;
    }
    .aligncenter {
      float: left;
      width:33.33333%;
      text-align:center;
    }
    .alignright {
      float: right;
      padding-right: 10px;
    }​
    

    This worked well for me when I had elements which required paddings of set amounts (and could not be represented as percentages) - as shown above in the CSS.

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