英文:
How align header in top, footer in bottom of the page and main between them in bootstrap 5.3
问题
Iam新手使用Bootstrap。我在这个HTML页面中使用了Bootstrap 5.3。现在我想将页眉对齐到顶部,页脚对齐到底部,并调整它们之间的主要内容的大小。我尝试了以下HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test Webpage</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<style>
</style>
<body style="min-height: 100vh; background-color: rgb(100,100,100);">
<header style="background-color: rgb(210, 210, 241);">
Header
</header>
<main style="background-color: rgb(185, 185, 215);">
Main content
</main>
<footer style="background-color: rgb(235, 235, 255);">
Footer
</footer>
</body>
</head>
从上面的代码生成以下输出:
我想要以下输出:
英文:
Iam new to bootstrap. I used bootstrap 5.3 in this html page. Now i want to align header in the top, footer in the bottom and size of the main between them. I tried the following html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test Webpage</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js""></script>
<style>
</style>
<body style="min-height: 100vh; background-color: rgb(100,100,100);">
<header style="background-color: rgb(210, 210, 241);">
Header
</header>
<main style="background-color: rgb(185, 185, 215);">
Main content
</main>
<footer style="background-color: rgb(235, 235, 255);">
Footer
</footer>
</body>
</head>
The following out is generated from above code
I want output like below
答案1
得分: 1
您的HTML由3个部分组成:
- 头部(使用Bootstrap的导航栏)
- 内容
- 底部
关键在于在内容上使用Bootstrap的flex-grow-1
类,并在底部上使用flex-shrink-0
类,将底部推至页面底部。另外,将Bootstrap的类d-flex
和flex-column
添加到<body>
中。
请参阅下面的代码片段。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
</head>
<body class="d-flex flex-column">
<!-- Header -->
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<!-- Navigation items -->
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
<!-- Page content -->
<div id="page-content" class="flex-grow-1">
<div class="container text-center">
<div class="row justify-content-center">
<!-- Content -->
</div>
</div>
</div>
<!-- Footer -->
<footer id="sticky-footer" class="flex-shrink-0 py-4 bg-dark text-white-50">
<div class="container text-center">
<small>Copyright © Your Website</small>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"></script>
</body>
</html>
英文:
Your HTML consists of 3 parts:
- header (use Bootstrap's navbar)
- content
- footer
The key is to use Bootstrap's flex-grow-1
class on the content and flex-shrink-0
on the footer to push the footer all the way down to the bottom of the page. Also, add Bootstrap's classes d-flex
and flex-column
to the <body>
.
See the snippet below.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
html,
body {
height: 100%;
}
<!-- language: lang-html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
</head>
<body class="d-flex flex-column">
<!-- Header -->
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
<!-- Page content -->
<div id="page-content" class="flex-grow-1">
<div class="container text-center">
<div class="row justify-content-center">
<div class="col-md-7">
<h1 class="fw-light mt-4 text-white">Sticky Footer using Flexbox</h1>
<p class="lead text-white-50">Use just two Bootstrap utility classes and three custom CSS rules and you will have a flexbox enabled sticky footer for your website!</p>
</div>
</div>
</div>
</div>
<!-- Footer -->
<footer id="sticky-footer" class="flex-shrink-0 py-4 bg-dark text-white-50">
<div class="container text-center">
<small>Copyright &copy; Your Website</small>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"></script>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论