【问题标题】:Wrapping divs with gaps between them用它们之间的间隙包装 div
【发布时间】:2017-08-17 19:51:29
【问题描述】:

当浏览器窗口变小时,我想要换行到下一行的 div。我还希望将边距放在 div 之间,以便它们之间存在间隙。我遇到的问题是,如果浏览器设置为特定大小,中心 div 的边距会导致 div 错误地换行。在一定尺寸下,一个 div 下有 2 个 div。以我下面的截图为例,这个小提琴:http://jsfiddle.net/uhh2jwe2/(改变窗口的宽度)

这确实需要是动态的,因为它将是用于布局不同大小的 div 的框架解决方案。父 div 将与示例类似。任何帮助都会很棒

#outer {
  width: 90%;
  height: 90%;
  margin: 5%;
  overflow: auto;
  background-color: red;
}

.inner1 {
  float: left;
  width: 150px;
  height: 150px;
  margin-right: 20px;
  background-color: blue;
}

.inner2 {
  float: left;
  width: 150px;
  height: 150px;
  margin-right: 20px;
  background-color: blue;
}

.inner3 {
  float: left;
  width: 150px;
  height: 150px;
  background-color: blue;
}
<div id="outer">
  <div class="inner1">1</div>
  <div class="inner2">2</div>
  <div class="inner3">3</div>
</div>

【问题讨论】:

  • "我有内联块 div" 我在提供的代码中看不到任何内容。
  • 我取出了 inline-block 部分,因为我也把它从示例中取出了。这无关紧要。您应该能够看到代码和 div 示例
  • 很明显...当outer 宽度小于 340px 时,它不能水平放置 两个 170px div(150px 宽度 + 20px 边距)。如果您也将右边距添加到第三个 div,您将获得可预测的结果。无论如何,您的预期输出是多少?

标签: html css


【解决方案1】:

您可以使用媒体查询来更改较小屏幕上的 css。

#outer {
  width: 90%;
  height: 90%;
  margin: 5%;
  overflow: auto;
  background-color: red;
}

.inner1 {
  float: left;
  width: 150px;
  height: 150px;
  margin-right: 20px;
  background-color: blue;
}

.inner2 {
  float: left;
  width: 150px;
  height: 150px;
  margin-right: 20px;
  background-color: blue;
}

.inner3 {
  float: left;
  width: 150px;
  height: 150px;
  background-color: blue;
}

@media (max-width: 435px) {
  #outer > div {
      margin-right:auto;
      margin-left:auto;
      margin-bottom:15px;
      float:none;
  }
}
<div id="outer">
  <div class="inner1">1</div>
  <div class="inner2">2</div>
  <div class="inner3">3</div>
</div>

【讨论】:

    【解决方案2】:

    像这样使用媒体查询:

    #outer div:last-child {
        margin-bottom: 0;
    }
    
    @media screen and (max-width:570px) {
        .inner1, .inner2, .inner3 {
            margin-bottom: 5px;
        }
    }
    
    @media screen and (max-width:411px) {
        .inner1, .inner2, .inner3 {
            float: none;
            margin: auto;
            margin-bottom: 5px;
        }
    }
    

    #outer {
        width: 90%;
        height: 90%;
        margin: 5%;
        overflow: auto;
        background-color: red;
    }
    
    .inner1 {
        float: left;
        width: 150px;
        height: 150px;
        margin-right: 20px;
        background-color: blue;
    }
    .inner2 {
        float: left;
        width: 150px;
        height: 150px;
        margin-right: 20px;
        background-color: blue;
    }
    .inner3 {
        float: left;
        width: 150px;
        height: 150px;
        background-color: blue;
    }
    
    #outer div:last-child {
        margin-bottom: 0;
    }
    
    @media screen and (max-width:570px) {
        .inner1, .inner2, .inner3 {
            margin-bottom: 5px;
        }
    }
    
    @media screen and (max-width:411px) {
        .inner1, .inner2, .inner3 {
            float: none;
            margin: auto;
            margin-bottom: 5px;
        }
    }
    <div id="outer">
        <div class="inner1">1</div>
        <div class="inner2">2</div>
        <div class="inner3">3</div>
    </div>

    【讨论】:

      【解决方案3】:

      我会推荐一种从内容元素中提取网格元素的解决方案。因此,您可以更好地控制布局,并且可以更灵活地处理要放入其中的内容。

      • .inner 元素用作网格元素并将其中的内容包装到.inner-content
      • 将所有内层排成一排,以去除外层排水沟
      • .inner 元素一个 percentage-width 和一个 px-max-width。所以元素可以占用可用宽度的 33.33%,但永远不会超过 150 像素。
      • 我为小屏幕添加了一些调整,因此.inner 元素相互重叠,占据了.outer 容器宽度的 33.33% 以上。
      • 查看代码:http://jsfiddle.net/uhh2jwe2/5/

      * {
        box-sizing: border-box;
      }
      
      /* flexible outer container */
      .outer {
        width: 90%;
        height: 90%;
        margin: 5%;
        overflow: hidden;
        background-color: red;
      }
      
      /* remove outer gutter */
      .row {
        margin: 0 -10px;
      }
      
      /* .inner will take care of the width */
      .inner {
        width: 33.33%;
        max-width: 150px;
        float: left;
        padding: 0 10px;
      }
      
      /* .inner-content take care of the height */
      .inner-content {
        height: 150px;
        color: #fff;
        background: blue;
      }
      
      @media (max-width: 435px) {
        /* this wraps .inner elements below each other and extends width */
        .outer .inner {
            padding: 10px 0;
            width: 100%;
            max-width: 100%;
            float:none;
        }  
      }
      <div class="outer">
        <div class="row">
          <div class="inner">
            <div class="inner-content">1</div>
          </div>
          <div class="inner">
            <div class="inner-content">2</div>
          </div>
          <div class="inner">
            <div class="inner-content">3</div>
          </div>
        </div>
      </div>

      【讨论】:

        【解决方案4】:

        我建议为此使用引导程序的技术。在内部元素的两侧都有填充,并在容器上用负边距取反。

        这将需要更严格的标记。虽然 .row 和 .container 可以在同一个元素上合并,但由于负边距,背景颜色会向左溢出。

        .container {
          background-color: green;
          width: 510px;
        }
        
        .row {
          font-size: 0;
          margin: 0 -15px;
        }
        
        .block {
          font-size: 12px;
          padding: 0 15px;
          display: inline-block;
        }
        
        .content {
          width: 150px;
          height: 150px;
          background-color: red;
        }
        <div class="container">
          <div class="row">
            <div class="block">
              <div class="content">
        
              </div>
            </div>
            <div class="block">
              <div class="content">
        
              </div>
            </div>
            <div class="block">
              <div class="content">
        
              </div>
            </div>
            <div class="block">
              <div class="content">
        
              </div>
            </div>
            <div class="block">
              <div class="content">
        
              </div>
            </div>
          </div>
        </div>

        【讨论】:

          【解决方案5】:

          在您的示例中,前两个 div 的宽度为 170px (150+20),第三个为 150px 宽,因为它没有边距,就是这样问题。

          例如,如果您希望 @media 具有完全响应性并且不从每行 4 项跳到每行 1 项,请避免使用 @media。

          你可以通过简单地在你的最后一个元素上添加一个 margin-right:20 来解决你的问题,但最好是这样

          .inner1, .inner2, .inner3{
              float: left;
              width: 150px;
              height: 150px;
              margin: 2px 10px; //left & right sides to half of 20px
              background-color: blue;
          }
          

          因为它会将边距向两侧分开,使其更加对称。


          用于布局不同大小的 div。

          如果你所有的 div 都可以改变大小但保持相等,它会起作用,但如果第一个 div 是 70,第二个和第三个是 50,那么在某个点上总会有两个 div。

          【讨论】:

            【解决方案6】:

            我想我已经找到了最简单的解决方案,无需使用媒体查询。我只是将右边距添加到包括最后一个字段在内的所有字段,而不是将其添加到除最后一个字段之外的每个字段。

            然后,我将所有字段包装在另一个 div 中并添加一个负边距(与间隙大小相同),以便字段在碰到容器侧面时会换行。这里有一个解决方案:http://jsfiddle.net/rahg1ky3/

            #outer {
                width: 90%;
                height: 90%;
                margin: 5%;
                overflow: auto;
                background-color: red;
            }
            #inner {
                margin-right: -20px;
            } 
            .cont {
                float: left;
                width: 150px;
                height: 150px;
                margin-right: 20px;
                background-color: blue;
            }
            
            <div id="outer">
              <div id = "inner">
                <div class="cont">1</div>
                <div class="cont">2</div>
                <div class="cont">3</div>
              </div>
            </div>
            

            【讨论】: