How do you align left / right a div without using float?

后端 未结 8 749
猫巷女王i
猫巷女王i 2020-12-24 10:04

When doing something like this:

Left Div
Right Div
相关标签:
8条回答
  • 2020-12-24 11:03

    Another way to do something similar is with flexbox on a wrapper element, i.e.,

     .row {
            display: flex;
            justify-content: space-between;
        }
      <div class="row">
            <div>Left</div>
            <div>Right</div>
        </div>
    
       

    0 讨论(0)
  • 2020-12-24 11:04

    You could just use a margin-left with a percentage.

    HTML

    <div class="goleft">Left Div</div>
    <div class="goright">Right Div</div>
    

    CSS

    .goright{
        margin-left:20%;
    }
    .goleft{
        margin-right:20%;
    }
    

    (goleft would be the same as default, but can reverse if needed)

    text-align doesn't always work as intended for layout options, it's mainly just for text. (But is often used for form elements too).

    The end result of doing this will have a similar effect to a div with float:right; and width:80% set. Except, it won't clump together like a float will. (Saving the default display properties for the elements that come after).

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