為什么我不能將這個div與bootstrap和flask對齊?
{% extends "_base.html" %}
{% block content %}
<h1 class="text-center">Welcome {{current_user.email}}!</h1>
<div class="input-group mb-3-center">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Options</label>
</div>
<select class="custom-select" id="inputGroupSelect01">
<option selected>Choose...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</div>
{% endblock %}
我也嘗試過文本中心,但只適用于選項文本。
謝謝,請放心,我是bootstrap新手。通常我只使用html標簽,但這當然不起作用。
我的base.html,里面有引導css的參考:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Flask User Management</title>
<!-- meta -->
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width,initial-scale=1">
<!-- styles -->
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<link rel="stylesheet" href="{{url_for('static', filename="styles.css")}}">
{% block css %}{% endblock %}
</head>
<body>
{% include "navigation.html" %}
<div class="container">
<br>
<!-- messages -->
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
{% for category, message in messages %}
<div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
{{message}}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
</div>
<div class="col-md-4"></div>
</div>
{% endif %}
{% endwith %}
<!-- child template -->
{% block content %}{% endblock %}
</div>
<!-- scripts -->
<script src="https://code.jquery.com/jquery-3.6.1.min.js" type="text/javascript"></script>
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
{% block js %}{% endblock %}
</body>
</html>
# # #解決方案1
要將下拉列表及其相應的標簽居中:
{% extends "_base.html" %}
{% block content %}
<h1 class="text-center">Welcome {{current_user.email}}!</h1>
<div class="input-group mb-3-center" style="display: flex;justify-content: center;">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Options</label>
</div>
<select class="custom-select" id="inputGroupSelect01">
<option selected>Choose...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</div>
</div>
{% endblock %}
利用Flexbox,我添加了style = & quot顯示器:flexjustify-content:居中;"到最外面的分區。查看此SO問題,了解關于在另一個元素中水平居中一個元素的更多選項。
解決方案2
第一種解決方案是將下拉列表和標簽居中,但是標簽將位于下拉列表的一側,而不是頂部。這是將兩個元素居中的解決方案,同時還將標簽放在下拉列表的頂部。
{% extends "_base.html" %}
{% block content %}
<h1 class="text-center">Welcome {{current_user.email}}!</h1>
<div class="input-group mb-3-center" style="display: flex;flex-direction: column;align-items: center;">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Options</label>
</div>
<select class="custom-select" id="inputGroupSelect01">
<option selected>Choose...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</div>
</div>
{% endblock %}
如你所見,我仍然使用flexbox,但是現在我將flex-direction設置為column,并將項目對齊到中心。