【问题标题】:How do you remove the gap between inner divs & container div?如何消除内部 div 和容器 div 之间的差距?
【发布时间】:2019-12-30 02:09:41
【问题描述】:

我似乎无法弄清楚为什么我的房间和在线用户 div 的宽度不一样(均匀分布)。 此外,由于某种原因,它们中的每一个与容器 div 之间存在间隙。 例如,在“Rooms” div 的左侧有空白区域,然后是 div 容器边框。我正在努力做到这一点,以便那里没有空白区域。 “在线用户” div 也会发生同样的事情。

我正在使用 Bootstrap 4。当我检查页面元素时,那个空白显示为 div.container。

Codepen.

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/styles.css" />
    <link rel="stylesheet" href="./css/all.min.css" />
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
      integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
      crossorigin="anonymous"
    />
  </head>
  <body>
    <div class="container">
      <!-- Content here -->
      <div class="room"><h4>Rooms</h4></div>
      <div class="chat">
        <div class="chat-box">
          <div class="message"></div>
          <div class="submit">
            <button type="button" class="btn btn-dark">Send</button>
          </div>
        </div>
      </div>
      <div class="online"><h4>Online Users</h4></div>
    </div>

    <script
      src="https://kit.fontawesome.com/02926adb38.js"
      crossorigin="anonymous"
    ></script>

    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script
      src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
      integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
      integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
      integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
      crossorigin="anonymous"
    ></script>
  </body>
</html>
body {
  height: 100%;
}

.container {
  border: 1px solid #000000;
  height: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
}

.chat {
  display: flex;
  flex: 1;
  position: relative;
  flex-direction: column;
}

.room {
  border: 1px solid #000000;
}

.online {
  border: 1px solid #000000;
}

.container .chat .chat-box {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  position: absolute;
  bottom: 0;
  height: 50px;
  width: 100%;
}

.container .chat .message {
  height: 100%;
  display: flex;
  flex: 1;
  align-items: center;
  padding-left: 10px;
  border: 1px solid #000000;
}

.container .chat .submit {
  height: 100%;
  display: flex;
  align-items: center;
  padding-left: 1rem;
  padding-right: 1rem;
}

【问题讨论】:

    标签: html css bootstrap-4


    【解决方案1】:

    你必须给以下类等宽:

    .room {
      border: 1px solid #000000;
      width: 100px;
    }
    
    .online {
      border: 1px solid #000000;
      width: 100px;
    }
    

    最外层的&lt;div&gt;填充为0px

    <div class="container" style="padding: 0px;">
    

    html,
    body {
      height: 100%;
    }
    
    .container {
      
      border: 1px solid #000000;
      height: 100%;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    }
    
    .chat {
      display: flex;
      flex: 1;
      position: relative;
      flex-direction: column;
    }
    
    .room {
      border: 1px solid #000000;
      width: 100px;
    }
    
    .online {
      border: 1px solid #000000;
      width: 100px;
    }
    
    .container .chat .chat-box {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      position: absolute;
      bottom: 0;
      height: 50px;
      width: 100%;
    }
    
    .container .chat .message {
      height: 100%;
      display: flex;
      flex: 1;
      align-items: center;
      padding-left: 10px;
      border: 1px solid #000000;
    }
    
    .container .chat .submit {
      height: 100%;
      display: flex;
      align-items: center;
      padding-left: 1rem;
      padding-right: 1rem;
    }
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Document</title>
        <link rel="stylesheet" href="./css/styles.css" />
        <link rel="stylesheet" href="./css/all.min.css" />
        <link
          rel="stylesheet"
          href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
          integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
          crossorigin="anonymous"
        />
      </head>
      <body>
        <div class="container" style="padding: 0px;">
          <!-- Content here -->
          <div class="room"><h4>Rooms</h4></div>
          <div class="chat">
            <div class="chat-box">
              <div class="message"></div>
              <div class="submit">
                <button type="button" class="btn btn-dark">Send</button>
              </div>
            </div>
          </div>
          <div class="online"><h4>Online Users</h4></div>
        </div>
    
        <script
          src="https://kit.fontawesome.com/02926adb38.js"
          crossorigin="anonymous"
        ></script>
    
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script
          src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
          integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
          crossorigin="anonymous"
        ></script>
        <script
          src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
          integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
          crossorigin="anonymous"
        ></script>
        <script
          src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
          integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
          crossorigin="anonymous"
        ></script>
      </body>
    </html>

    【讨论】:

      【解决方案2】:

      Container 来自引导程序的类提供填充。所以给填充零。 我已经将25%.chat.online &lt;div&gt; 并计算了.chat 的宽度。尝试实现这一点,看看这是否适合你。

      .container {
        border: 1px solid #000000;
        height: 100%;
        display: flex;
        flex-direction: row;
        justify-content: space-around;
        padding : 0 !important;
      }
      
      .chat {
        display: flex;
        flex: 1;
        position: relative;
        flex-direction: column;
        width : calc(100% - 50%);
        padding : 0 !important;
      }
      
      .room {
        border: 1px solid #000000;
        width : 25%;
      }
      
      .online {
        border: 1px solid #000000;
        width : 25%;
      }
      
      

      【讨论】:

      • 非常感谢。你知道如何让房间和在线用户的宽度相同吗?我希望中间容器(聊天)保持相同的宽度。