Bootstrap和Flask-如何使卡片彼此相邻堆叠

时间:2019-02-14 03:25:39

标签: python html twitter-bootstrap flask

我正在制作一个航班预订Web应用程序,并在HTML页面中显示用户预订的航班,该页面在这样的for循环中。

{% extends 'layout.html' %}
{% block content %}
    <div class="container">
        <div class="row welcome text-center">
            <div class="col-12">
                <h1 class="display-4">My Flights</h1>
            </div>
        </div>
    </div>
    {% for flight in flights %}
        <div class="container">
            <div class="row">
                <div class="col-sm">
                    <div class="card">
                        <div class="card-body">
                            <center><h5 class="card-title">Flight Number - {{ flight.flight_number }}</h5></center>
                            <center><h5 class="card-title">Fly Date - {{ flight.date }}</h5></center>
                        </div>
                        <ul class="list-group list-group-flush">
                            <center>
                                <li class="list-group-item">Fly Too - {{ flight.destination }}</li>
                            </center>
                            <center>
                                <li class="list-group-item">Fly From - {{ flight.fly_from }}</li>
                            </center>
                            <center>
                                <li class="list-group-item">Departure Time - {{ flight.time }}</li>
                            </center>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    {% endfor %}
{% endblock %} 

正在从数据库中获取信息。

现在我在网页设计方面还不太好,所以谢谢您的帮助,这也是我用作每个页面基础的layout.html。

    <!DOCTYPE html>
<html>
<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">

    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}">

    {% if title %}
        <title>Sunny-Flights - {{ title }}</title>
    {% else %}
        <title>Sunny-Flights</title>
    {% endif %}
</head>
<body>
<header class="site-header">
    <nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
        <div class="container-fluid">
            <a class="navbar-brand mr-4" href="/">SunnyFlights</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle"
                    aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarToggle">
                <div class="navbar-nav mr-auto">
                    <a class="nav-item nav-link" href="{{ url_for('home') }}">Home</a>
                    <a class="nav-item nav-link" href="{{ url_for('book') }}">Book</a>
                    <a class="nav-link nav-link" href="{{ url_for('my_flights') }}">My Flights</a>
                </div>
                <!-- Navbar Right Side -->
                <div class="navbar-nav">
                    {% if current_user.is_authenticated %}
                        <a class="nav-item nav-link" href="{{ url_for('logout') }}">Logout</a>
                        <a class="nav-item nav-link" href="{{ url_for('account') }}">Account</a>
                        <a class="nav-item nav-link" href="#">£{{ current_user.funds }}</a>
                    {% else %}
                        <a class="nav-item nav-link" href="{{ url_for('login') }}">Login</a>
                        <a class="nav-item nav-link" href="{{ url_for('register') }}">Register</a>
                    {% endif %}
                </div>
            </div>
        </div>
    </nav>
</header>
<main role="main" class="container">
    <div class="row">
        <div class="col-md-8">
            {% with messages = get_flashed_messages(with_categories=true) %}
                {% if messages %}
                    {% for category, message in messages %}
                        <div class="alert alert-{{ category }}">
                            {{ message }}
                        </div>
                    {% endfor %}
                {% endif %}
            {% endwith %}
            {% block content %}{% endblock %}
        </div>
    </div>
</main>


<!-- 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>

2 个答案:

答案 0 :(得分:0)

根据引导程序文档。您可以将卡包装在卡组中,以将它们彼此堆叠:

bootstrap card-group documentation

<div class="card-group">
  <div class="card">
    <img class="card-img-top" src="..." alt="Card image cap">
    <div class="card-body">
      <h5 class="card-title">Card title</h5>
      <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
      <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
    </div>
  </div>
  <div class="card">
    <img class="card-img-top" src="..." alt="Card image cap">
    <div class="card-body">
      <h5 class="card-title">Card title</h5>
      <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
      <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
    </div>
  </div>
  <div class="card">
    <img class="card-img-top" src="..." alt="Card image cap">
    <div class="card-body">
      <h5 class="card-title">Card title</h5>
      <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p>
      <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
    </div>
  </div>
</div>

答案 1 :(得分:0)

如果您正在使用 for 循环。有一些方法可以将卡片彼此相邻放置。 这是我为示例所做的代码:

<div>
    <div class="card-deck">
        {% for post in posts %}
        <div class="card" style="min-width: 20rem; max-width: 22rem; margin-top: 5px;">
            <div class="card-body">
                <a href="{{ url_for('post', post_id=post.id) }}">
                    {{ post.details|safe }}
            </div>
        </div>
        </a>
        {% endfor %}
    </div>
</div>

style="min-width: 20rem; max-width: 22rem; margin-top: 5px;"> 用这个你可以设置最小和最大宽度,然后我会自动设置它的位置。