在Bootstrap中设计2列具有不同行的列

时间:2019-01-27 05:29:03

标签: css html5 twitter-bootstrap twitter-bootstrap-3 bootstrap-4

我刚刚开始学习Bootstrap。现在,我正在尝试复制使用表格制作的旧设计。在我想要得到的设计之下

Looks I want

但是不幸的是,我的Bootstrap技能使我丧命

I hate this

我想要的D行[D1,D2]未对齐。在我使用的代码下面[取自Bootstrap的示例]

    <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Jekyll v3.8.5">
<title>Jumbotron Template ยท Bootstrap</title>

<!-- Bootstrap core CSS -->
<link href="https://getbootstrap.com/docs/4.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<style>
  .bd-placeholder-img {
    font-size: 1.125rem;
    text-anchor: middle;
  }

  @media (min-width: 768px) {
    .bd-placeholder-img-lg {
      font-size: 3.5rem;
    }
  }
</style>
<!-- Custom styles for this template -->
<link href="https://getbootstrap.com/docs/4.2/examples/jumbotron/jumbotron.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/4.0/examples/sticky-footer-navbar/sticky-footer-navbar.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
  <li class="nav-item active">
    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled" href="#">Disabled</a>
  </li>
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
    <div class="dropdown-menu" aria-labelledby="dropdown01">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <a class="dropdown-item" href="#">Something else here</a>
    </div>
  </li>

  <li class="nav-item">
  <!-- Example single danger button -->
<div class="btn-group">
<button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Action
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Separated link</a>
</div>
</div>
</li>

</ul>
<div class="alert alert-success">
<strong>A</strong>
</div>
<form class="form-inline my-2 my-lg-0">
  <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
  <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>

<main role="main">

<!-- Main jumbotron for a primary marketing message or call to action -->
<div class="jumbotron">
<div class="container">
  <h1 class="display-3">B</h1>
  <p>This is a template </p>
  <p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more &raquo;</a></p>
</div>
</div>

<div class="row mb-2">
<div class="col-md-4"> <div class="alert alert-success">  C</div> 
</div>
    <div class="col-md-8">
      <div class="alert alert-warning">  D1</div> 
      </div>
      <div class="col-md-8">
      <div class="alert alert-warning">  D2</div> 
      </div>
</div>

</main>

<footer class="footer">
  <div class="container">
    <span class="text-muted">E</span>
  </div>
</footer>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script>window.jQuery || document.write('<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"><\/script>')</script><script src="https://getbootstrap.com/docs/4.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-zDnhMsjVZfS3hiP7oCBRmfjkQC4fzxVxFhBx8Hkz2aZX8gEvA/jsP3eXRCvzTofP" crossorigin="anonymous"></script></body>
</html>

1 个答案:

答案 0 :(得分:2)

这个...

<div class="row mb-2">
  <div class="col-md-4">
    <div class="alert alert-success"> C</div>
  </div>
  <div class="col-md-8">
    <div class="alert alert-warning"> D1</div>
  </div>
  <div class="col-md-8">
    <div class="alert alert-warning"> D2</div>
  </div>
</div>

...必须是:

<div class="row mb-2">
  <div class="col-md-4">
    <div class="alert alert-success"> C</div>
  </div>
  <div class="col-md-8">
    <div class="row mb-2">
      <div class="col-12">
        <div class="alert alert-warning"> D1</div>
      </div>
    </div>
    <div class="row mb-2">
      <div class="col-12">
        <div class="alert alert-warning"> D2</div>
      </div>
    </div>
    <div class="row mb-2">
      <div class="col-12">
        <div class="alert alert-warning"> etc...</div>
      </div>
    </div>
  </div>
</div>

正确的布局是:

<html>
<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
</head>

<body>
  <header>
    <nav></nav>
  </header>
  <main>
    <section class="jumbotron">
      <div class="container">
        jumbotron
      </div>
    </section>
    <div class="container">
      <div class="row">
        <div class="col-md-4">
          left column
        </div>
        <div class="col-md-8">
          <div class="row">
            <div class="col-12">content 1</div>
            <div class="col-12">content 2</div>
          </div>
          <div class="row">
            <div class="col-6">content 3.1</div>
            <div class="col-6">content 3.2</div>
          </div>
          <div class="row">
            <div class="col-md-4">content 4.1</div>
            <div class="col-md-8">content 4.2 (5 below md)</div>
          </div>
        </div>
      </div>
    </div>
  </main>
  <footer>
    <div class="container">
      <div class="row">
        <div class="col-12">
          footer content...
        </div>
      </div>
    </div>
  </footer>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
</body>

</html>