如何在Bootstrap 5.3中将页眉置于顶部、页脚置于底部,主内容位于它们之间?

huangapple go评论81阅读模式
英文:

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>

从上面的代码生成以下输出:

如何在Bootstrap 5.3中将页眉置于顶部、页脚置于底部,主内容位于它们之间?

我想要以下输出:

如何在Bootstrap 5.3中将页眉置于顶部、页脚置于底部,主内容位于它们之间?

英文:

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
	&lt;meta charset=&quot;utf-8&quot;&gt;
	&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;

	&lt;title&gt;Test Webpage&lt;/title&gt;
	&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js&quot;&quot;&gt;&lt;/script&gt;
	&lt;style&gt;
	&lt;/style&gt;
	&lt;body style=&quot;min-height: 100vh; background-color: rgb(100,100,100);&quot;&gt;
	&lt;header style=&quot;background-color: rgb(210, 210, 241);&quot;&gt;
	Header
	&lt;/header&gt;
	&lt;main  style=&quot;background-color: rgb(185, 185, 215);&quot;&gt;
	Main content
	&lt;/main&gt;
	&lt;footer style=&quot;background-color: rgb(235, 235, 255);&quot;&gt;
	Footer
	&lt;/footer&gt;
	&lt;/body&gt;
&lt;/head&gt;

The following out is generated from above code

如何在Bootstrap 5.3中将页眉置于顶部、页脚置于底部,主内容位于它们之间?

I want output like below

如何在Bootstrap 5.3中将页眉置于顶部、页脚置于底部,主内容位于它们之间?

答案1

得分: 1

您的HTML由3个部分组成:

关键在于在内容上使用Bootstrap的flex-grow-1类,并在底部上使用flex-shrink-0类,将底部推至页面底部。另外,将Bootstrap的类d-flexflex-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 &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>
英文:

Your HTML consists of 3 parts:

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 &lt;body&gt;.

See the snippet below.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html,
body {
height: 100%;
}

<!-- language: lang-html -->

&lt;!doctype html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
&lt;title&gt;Bootstrap demo&lt;/title&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;/head&gt;
&lt;body class=&quot;d-flex flex-column&quot;&gt;
&lt;!-- Header --&gt;
&lt;nav class=&quot;navbar navbar-expand-lg bg-light&quot;&gt;
&lt;div class=&quot;container-fluid&quot;&gt;
&lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;Navbar&lt;/a&gt;
&lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#navbarSupportedContent&quot; aria-controls=&quot;navbarSupportedContent&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;
&lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
&lt;/button&gt;
&lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarSupportedContent&quot;&gt;
&lt;ul class=&quot;navbar-nav me-auto mb-2 mb-lg-0&quot;&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link active&quot; aria-current=&quot;page&quot; href=&quot;#&quot;&gt;Home&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Link&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item dropdown&quot;&gt;
&lt;a class=&quot;nav-link dropdown-toggle&quot; href=&quot;#&quot; role=&quot;button&quot; data-bs-toggle=&quot;dropdown&quot; aria-expanded=&quot;false&quot;&gt;
Dropdown
&lt;/a&gt;
&lt;ul class=&quot;dropdown-menu&quot;&gt;
&lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Action&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Another action&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;hr class=&quot;dropdown-divider&quot;&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Something else here&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link disabled&quot;&gt;Disabled&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;form class=&quot;d-flex&quot; role=&quot;search&quot;&gt;
&lt;input class=&quot;form-control me-2&quot; type=&quot;search&quot; placeholder=&quot;Search&quot; aria-label=&quot;Search&quot;&gt;
&lt;button class=&quot;btn btn-outline-success&quot; type=&quot;submit&quot;&gt;Search&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;!-- Page content --&gt;
&lt;div id=&quot;page-content&quot; class=&quot;flex-grow-1&quot;&gt;
&lt;div class=&quot;container text-center&quot;&gt;
&lt;div class=&quot;row justify-content-center&quot;&gt;
&lt;div class=&quot;col-md-7&quot;&gt;
&lt;h1 class=&quot;fw-light mt-4 text-white&quot;&gt;Sticky Footer using Flexbox&lt;/h1&gt;
&lt;p class=&quot;lead text-white-50&quot;&gt;Use just two Bootstrap utility classes and three custom CSS rules and you will have a flexbox enabled sticky footer for your website!&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!-- Footer --&gt;
&lt;footer id=&quot;sticky-footer&quot; class=&quot;flex-shrink-0 py-4 bg-dark text-white-50&quot;&gt;
&lt;div class=&quot;container text-center&quot;&gt;
&lt;small&gt;Copyright &amp;copy; Your Website&lt;/small&gt;
&lt;/div&gt;
&lt;/footer&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月19日 18:08:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定