HTML, align two elements on both sides of the same line

前端 未结 5 833
名媛妹妹
名媛妹妹 2021-01-14 06:03

Without using table, how can I align two elements (one at left, another at right) in the same line?

相关标签:
5条回答
  • 2021-01-14 06:32

    HTML content :
    <div class='container'><div class="align-left">left</div><div class="align-right">right</div></div>

    Style as shown below

    .container{ width:100%; }
    .align-left{ float: left;width:50%; } .align-right{ float: right;width:50%; }

    0 讨论(0)
  • 2021-01-14 06:38

    I have made an example for you to understand the elements a bit better. With using a float you can 'move'(float) elements to a specific side (left or right).

    Depending on what you want, you can float everything to the left, which makes every element 'stick' to the other element as long as the width's of the elements do not exceed the parent element width. Otherwise they will 'fall' under the element to fit the width. For instance this code where the container has a width of 600px. Each class 'test' has a width of 250px. This will result in 2 elements next to each other, but the other one will fall under it.

    <div class='container green'>
      <div class='test big blue'>a</div>
      <div class='test big red'>b</div>
      <div class='test big yellow'>c</div>
    </div>
    

    http://jsfiddle.net/qBR9M/4/

    The best way to play with this is the give your elements a background-color, so you can see how the respond.

    0 讨论(0)
  • 2021-01-14 06:39

    Using float: right; and float:left;

    0 讨论(0)
  • 2021-01-14 06:47

    If you want them on the same line, you can use INLINE or INLINE-BLOCK.

    #element1 {
      display: inline-block;
      margin-right: 10px;
      width: 200px;
      background-color: red;
    }
    #element2 {
      display: inline-block;
      width: 200px;
      background-color: red;
    }
    <div id="element1">element 1 markup</div>
    <div id="element2">element 2 markup</div>

    0 讨论(0)
  • 2021-01-14 06:49

    try this:

    <div class="align-left">
      left
    </div>
    <div class="align-right">
      right
    </div>
    

    and the css:

    .align-left{
       float: left;
     }
    
    .align-right{
       float: right;
     }
    

    see example here

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