卡与网格系统堆叠在一起

时间:2019-07-10 16:11:19

标签: html css bootstrap-4

我有4张使用引导程序4的卡片。但是,当我按比例缩小时,这4张卡片彼此重叠,而不是降为2,然后在移动设备视图中降为1。

我的代码:https://codepen.io/jehc10/pen/LKqRRQ

.navbar-nav {
  background: #4287f5;
}

.navbar-brand {
  background: #4287f5;
}

.navbar-brand.custom {
  color: #FFFFFF;
  font-size: 25px
  /* border-right:2px solid black; */
}

.navbar {
  background: #4287f5;
}

body {
  font-family: 'Poppins', sans-serif;
  background-image: url("attractive-backdrop-background-988872.jpg");
  /* font-weight:700; */
}

.navbar-nav li a {
  color: #FFFFFF;
  font-size: 20px;
}

.card {
  margin-left: 35px;
  margin-top: 20px;
  background: #f2f2f2;
}

form {
  margin-left: 50px;
  margin-top: 20px;
  background: grey;
  border: 2px solid black;
}
<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">


    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <!-- My stylesheet -->
    <link rel="stylesheet" href="website.css">

    <!-- Google fonts -->
    <link href="https://fonts.googleapis.com/css?family=Poppins:400,700&display=swap" rel="stylesheet">

    <title>Jesse</title>
</head>

<body>

    <!-- NAV bar -->

    <nav class="navbar navbar-expand-lg">
        <a class="navbar-brand custom" href="#">Jesse</a>
        <button class="navbar-toggler navbar-dark" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item active">
                    <a class="nav-link" href="#">About me <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Portfolio</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Pricing</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Contact me</a>
                </li>
            </ul>
        </div>
    </nav>

    <!-- Portfolio cards -->
    <div class=row>
        <div class=col-sm-3>
            <div class="card" style="width: 18rem;">
                <img src="cat.png" class="card-img-top" alt="...">
                <div class="card-body">
                    <h5 class="card-title">Card 1</h5>
                    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                    <a href="http://www.google.ca" class="btn btn-primary">Go somewhere</a>
                </div>
            </div>
        </div>

        <div class=col-sm-3>
            <div class="card" style="width: 18rem;">
                <img src="cat.png" class="card-img-top" alt="...">
                <div class="card-body">
                    <h5 class="card-title">Card 2</h5>
                    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                    <a href="http://www.google.ca" class="btn btn-primary">Go somewhere</a>
                </div>
            </div>
        </div>

        <div class=col-sm-3>
            <div class="card" style="width: 18rem;">
                <img src="cat.png" class="card-img-top" alt="...">
                <div class="card-body">
                    <h5 class="card-title">Card 3</h5>
                    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                    <a href="http://www.google.ca" class="btn btn-primary">Go somewhere</a>
                </div>
            </div>
        </div>

        <div class=col-sm-3>
            <div class="card" style="width: 18rem;">
                <img src="cat.png" class="card-img-top" alt="...">
                <div class="card-body">
                    <h5 class="card-title">Card 4</h5>
                    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                    <a href="http://www.google.ca" class="btn btn-primary">Go somewhere</a>
                </div>
            </div>
        </div>
    </div>
    <!-- Contact form -->
    <div class=row>
        <div class=col-lg-6>
            <form>
                <div class="form-group">
                    <label for="exampleInputEmail1">Email address</label>
                    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
                    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                </div>
                <div class="form-group">
                    <label for="exampleInputPassword1">Password</label>
                    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
                </div>
                <div class="form-group form-check">
                    <input type="checkbox" class="form-check-input" id="exampleCheck1">
                    <label class="form-check-label" for="exampleCheck1">Check me out</label>
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
        </div>
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

4 个答案:

答案 0 :(得分:0)

删除内联固定宽度以解决此问题。

<div class="card" style="width: 18rem;">

答案 1 :(得分:0)

只需在卡中添加一些类别,即可解决您的问题。

    <div class="col-12 col-sm-3 mb-3">
      <div class="card w-100">
        <img src="cat.png" class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card 1</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="http://www.google.ca" class="btn btn-primary">Go somewhere</a>
        </div>
      </div>
    </div>

应该可以解决问题了。

我在卡片包装纸中添加了 col-12 ,在卡片中添加了 w-100 以实现全宽。 从卡片上删除了内嵌样式宽度。

答案 2 :(得分:0)

首先不要将宽度设置为style="width: 18rem;"内联或在CSS文件中。宽度应由Bootstrap的col-x类确定。这就是整个想法。

如果您想将它们放在彼此下面,可以这样做col col-sm-6 col-lg-3。这意味着一张卡在XS上将占据全角,从SM到MD彼此相邻,在LG和XL上占据4位,

<div class="row">

    <div class="col col-md-6 col-lg-3">
        <div class="card">

        </div>
    </div>

    <div class="col col-md-6 col-lg-3">
        <div class="card">

        </div>
    </div>

答案 3 :(得分:-1)

如果您从家长中删除col-sm-3,则应该可以正常工作。 发生的情况是,您迫使父母的div排成一行。但是在它们内部,您有一个带有style="width: 18rem;"固定值的div。因此内部div不适合当前div。这就是为什么它们重叠(并且flex-wrap属性无法正常工作的原因)。