【问题标题】:How to remove the whitespace between two columns of a row in bootstrap 5 grid?如何删除引导程序 5 网格中一行的两列之间的空格?
【发布时间】:2022-01-31 18:08:29
【问题描述】:

我正在尝试实现下面给出的网格布局

.col_1{
    background-color: bisque !important;
    height: 500px;

  
}
.col_2 {
    width: 300px;
    height: 287px;
    background-position: center;
    background-image: url('https://images.unsplash.com/photo-1633113218833-f0b9912cfe1c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80');
}
.col_3{
    width: auto;
    height: 280px;
    background-color: blue;
    /* margin: 0 !important; */
}
.col_4{
    width: auto;
    height: 300px;
    background-color: yellowgreen;

}
.col_5{
    width: auto;
    height: 350px;
    background-color: black;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Practice 1</title>
     <link  href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</head>
<body>
    <div class="container mt-2">
<div class="row">  
    <div class="col-sm-4 col_1"> 
        <div class="col col-sm-3 col_1"></div>
    </div> 
    <div class="col-sm-8">
        <div class="container-fluid">
        <div class="row">
                    <div class="col col-md-6 col-md-2">
                            <div class="col col-sm-6 col_2"></div>
                            <div class="col col-sm-6 mt-2 col_3"></div>
           
                </div>
                    <div class="col col-md-6">
                            <div class="col col-sm-6 col_4"></div>
                            <div class="col col-sm-6 mt-2 col_5"></div>
                 
                </div>
            </div>
        </div>
        </div>
    </div>

</div>
    </body>
</html>
所以第一个 我面临的问题是,当我减小图像的宽度时,图像与其相邻列之间出现空白。 如果我将所有列的宽度保持为自动,则不会出现空格。
我检查了我的代码,发现列右侧的边距导致空白我还在下面发布了我检查过的网页

那么我该如何解决这个空白问题呢? 或者唯一的解决方案是使用相对位置和边距?

【问题讨论】:

  • 这是预期的行为。如果您不想要那个空白,您应该删除 width 属性或将其设置为 100%。

标签: javascript html css bootstrap-4 bootstrap-5


【解决方案1】:

尝试使用背景重复,背景大小

.col_2 {
    width: 300px;
    height: 287px;
    background-position: center;
    background-repeat: no-repeat;
    background-size:100%;
    background-image: url('https://images.unsplash.com/photo-1633113218833-f0b9912cfe1c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80');
}

【讨论】: