使用TailwindCSS创建带有导航栏的全页面div。

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

Making a full page div with a navbar in TailwindCSS

问题

I'm using Tailwind CSS and I would like to achieve a layout which includes:

  • A sticky navbar
  • A full-page section with centered content
  • Other sections with variable height
  • A footer

Here's a quick representation:

+--------------------------+    -+
|  Navbar (sticky)         |     |
|--------------------------|     |
|                          |     |
|                          |     | --> this has viewport height
|    centered content      |     | 
|                          |     |
|                          |     |
|--------------------------|    -+
|                          |
| Section 1                |
|                          |
+--------------------------+              
| Section 2                |
+--------------------------+
| Footer                   |
+--------------------------+

I would like the centered content section to take up all the remaining space of the viewport after the navbar, but applying the h-screen class causes it to be as high as the viewport causing an overflow. Here's some reproducible code:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cdn.tailwindcss.com"></script>
  <style>
    html, body {
      height: 100%;
      width: 100%;
      margin: 0;
    }
  </style>
</head>
<body>
  <div class="h-20 text-5xl sticky top-0 text-center bg-slate-300/50">
      Menu
  </div>
  <div class="h-screen mx-auto bg-indigo-300 flex flex-col justify-center items-center text-5xl">
      content
  </div>
  <div class="h-80 mx-auto bg-green-300 flex flex-col justify-center items-center text-5xl">
      content
  </div>
  <div class="h-20 mx-auto bg-black text-white flex flex-col justify-center items-center text-5xl">
      footer 
  </div>
</body>
</html>

Can someone help me to find a way to make the "centered content" section take up all the remaining space while letting the other, following, sections have an arbitrary height.

英文:

I'm using Tailwind CSS and I would like to achieve a layout which includes:

  • A sticky navbar
  • A full-page section with centered content
  • Other sections with variable height
  • A footer

Here's a quick representation:

+--------------------------+    -+
|  Navbar (sticky)         |     |
|--------------------------|     |
|                          |     |
|                          |     | --&gt; this has viewport height
|    centered content      |     | 
|                          |     |
|                          |     |
|--------------------------|    -+
|                          |
| Section 1                |
|                          |
+--------------------------+              
| Section 2                |
+--------------------------+
| Footer                   |
+--------------------------+

I would like the centered content section to take up all the remaining space of the viewport after the navbar, but applying the h-screen class causes it to be as height as the viewport causing an overflow. Here's some reproducible code:

&lt;!doctype html&gt;
&lt;html&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.0&quot;&gt;
  &lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;
  &lt;style&gt;
    html, body {
      height: 100%;
      width: 100%;
      margin: 0;
    }
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div class=&quot;h-20 text-5xl sticky top-0 text-center bg-slate-300/50&quot;&gt;
      Menu
  &lt;/div&gt;
  &lt;div class=&quot;h-screen mx-auto bg-indigo-300 flex flex-col justify-center items-center text-5xl&quot;&gt;
      content
  &lt;/div&gt;
  &lt;div class=&quot;h-80 mx-auto bg-green-300 flex flex-col justify-center items-center text-5xl&quot;&gt;
      content
  &lt;/div&gt;
  &lt;div class=&quot;h-20 mx-auto bg-black text-white flex flex-col justify-center items-center text-5xl&quot;&gt;
      footer 
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

Can someone help me to find a way to make the "centered content" section take up all the remaining space while letting the other, following, sections have an arbitrary height.

Thank you.

答案1

得分: 0

有两种解决方案可供调整您的布局。

您可以向粘性导航栏添加负的底部边距 -mb-20

&lt;div class=&quot;h-20 text-5xl sticky top-0 text-center bg-slate-300/50 -mb-20&quot;&gt;
  菜单
&lt;/div&gt;

或者您可以将导航栏的位置更改为 fixed

&lt;div class=&quot;h-20 text-5xl fixed top-0 inset-x-0 text-center bg-slate-300/50&quot;&gt;
  菜单
英文:

There is 2 solutions for your layout.

You can add negative margin bottom to sticky navbar -mb-20

&lt;div class=&quot;h-20 text-5xl sticky top-0 text-center bg-slate-300/50 -mb-20&quot;&gt;
  Menu
&lt;/div&gt;

or you can change you navbar position to fixed

&lt;div class=&quot;h-20 text-5xl fixed top-0 inset-x-0 text-center bg-slate-300/50&quot;&gt;
  Menu

Hope this helps.

huangapple
  • 本文由 发表于 2023年2月16日 02:22:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463968.html
匿名

发表评论

匿名网友

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

确定