Set space between divs

后端 未结 3 1014
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 15:23

I have two divs like this:

相关标签:
3条回答
  • 2020-12-28 15:43

    For folks searching for solution to set spacing between N divs, here is another approach using pseudo selectors:

    div:not(:last-child) {
      margin-right: 40px;
    }
    

    You can also combine child pseudo selectors:

    div:not(:first-child):not(:last-child) {
      margin-left: 20px;
      margin-right: 20px;
    }
    
    0 讨论(0)
  • 2020-12-28 15:43

    You need a gutter between two div gutter can be made as following

    margin(gutter) = width - gutter size E.g margin = calc(70% - 2em)

    <body bgcolor="gray">
    <section id="main">
            <div id="left">
                Something here     
            </div>
            <div id="right">
                    Someone there
            </div>
    </section>
    </body>
    <style>
    body{
        font-size: 10px;
    }
    
    #main div{
        float: left;
        background-color:#ffffff;
        width: calc(50% - 1.5em);
        margin-left: 1.5em;
    }
    </style>
    
    0 讨论(0)
  • 2020-12-28 15:50

    Float them both the same way and add the margin of 40px. If you have 2 elements floating opposite ways you will have much less control and the containing element will determine how far apart they are.

    #left{
        float: left;
        margin-right: 40px;
    }
    #right{
       float: left;
    }
    
    0 讨论(0)
提交回复
热议问题