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

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

When doing something like this:

Left Div
Right Div
8条回答
  •  轮回少年
    2020-12-24 11:04

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

    HTML

    Left Div
    Right 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).

提交回复
热议问题