Creating a sleek full-page container with a navigation bar using TailwindCSS

I am currently utilizing Tailwind CSS and in need of creating a layout that consists of:

  • A sticky navigation bar
  • A full-page section with content centered
  • Other sections with varying heights
  • A footer

Below is a simple representation:

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

I want the centered content section to occupy all available space after navbar, but using the h-screen class causes it to overflow due to being equal to viewport height. Below is some code for reference:

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

If anyone has a solution on how I can make the "centered content" section fill up the remaining space while allowing other sections to have different heights, please share your input.

Thank you.

Answer №1

There are a couple of solutions available for adjusting your layout.

One option is to apply a negative margin bottom to the sticky navbar using -mb-20

<div class="h-20 text-5xl sticky top-0 text-center bg-slate-300/50 -mb-20">
  Menu
</div>

Alternatively, you can switch the positioning of your navbar to fixed

<div class="h-20 text-5xl fixed top-0 inset-x-0 text-center bg-slate-300/50">
  Menu

I hope this information proves helpful.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating responsive CSS layouts for various device sizes

My webpage features a form with a prompt for the input box, followed by the input box itself. I want to implement two different layouts based on the device accessing the page. For cell phones, I want everything stacked vertically. However, for computers, ...

Creating a stylish grid design with Tailwind CSS: A step-by-step guide

Can anyone tell me if this grid is created using SVG? Also, how can I replicate a similar grid design using Tailwind CSS? I have inspected the element but couldn't find any SVG code. Thank you. https://i.sstatic.net/HwBK8.png Check out the link: htt ...

A triangular-shaped div positioned at the bottom with a captivating background image

Is there a way to create a div with a unique twist - a triangle shape at the bottom? I've been attempting to add a background image within the triangle using a pseudo element (:after), but so far, it hasn't been successful. #homebg:after{ co ...

How can I use HTML code to style the header cell values of a table?

Can someone please assist me with creating a table header style that looks like the one in the image? Additionally, how can I turn the name and picture area into a block and add borders to them? I'm also having trouble with my list icons showing up a ...

When the text exceeds the designated block, it will be fragmented

I have always struggled with text elements not breaking when they exceed the boundaries of their parent element. I understand that setting a max-width of 100px will solve the issue, but it's not an ideal solution for me. I want the text to break only ...

Flexbox causing issues with relative positioning at the bottom of the screen in various browsers

My flex component looks like this: <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" ... width="100%" height="100%" creationComplete="init()"> ....... <components:Naviga ...

Modify table cell content based on boolean value - HTML

Currently, I am working with a table that displays properties from an object. Is there a simple method to change the text displayed in a cell instead of the true/false values it is currently set to? For example, one of the columns indicates whether the ob ...

Achieving the extraction of a particular string from an HTML element using JavaScript

<input id="WD01B3" ct="CB" lsdata="{2:'WD01B4',4:'Any',20:'\x7b\x22WDA_TYPE\x22\x3a\x22DROPDOWN_BY_KEY\x22,\x22WDA_ID\x22\x3a\x22ABCA950297D2C0C432BAB9BB ...

Issue with the Styled Components Color Picker display

For the past 6 months, I have been using VSCode with React and Styled Components without any issues. However, recently I encountered a problem where the color picker would not show up when using CSS properties related to color. Usually, a quick reload or r ...

What are alternative ways to divide CSS without relying on CSS-in-JS and CSS modules?

Recently, I transitioned to next.js and I'm eager to develop an application with CSS styles without resorting to css-in-js or inline styling. In my exploration, I've come across two potential options: using CSS modules or importing global styles ...

What is the process of linking a PHP file to an HTML form?

Having trouble sending emails with form data since the mailto function isn't working properly. For some reason, the php file is not connecting to the html form. When I try running index.html locally and hit submit, the browser displays php code inste ...

Issue with Jquery function not executing on second attempt

/** Creates a table of iTunes data in HTML format **/ var appendValues = function(songArray) { songArray.each(function() { var title = $(this).find("im\\:name").eq(0).text(); var songImage = $(this).find("im\\:image").eq(0).text(); var ...

Unlimited icon carousel crafted with CSS

Currently, I have a set of 10 icons that are scrolling horizontally from right to left. However, I am encountering an issue where at the end of the icon loop, there is a space causing the icons to jump back to the beginning. I am looking for a solution to ...

Extracting Data: Unlocking Information from Websites

My friend and I are working on a school project that involves creating a small application. The main goal of this app is to find pharmacies in our area. I am looking to extract information from two specific websites. gun = day/date lc = id of town ...

Aligning form fields in a HTML form using Bootstrap

In my extensive form, several fields within the setup encounter the same issue. Specifically, a checkbox below 'contact telephone number' causes the surrounding content to become misaligned. https://i.sstatic.net/8qI8Z.png My attempts to resolv ...

Choosing the name of an HTML element tag using Selenium with Python

Struggling with writing a Python3 function using Selenium to click a link matching specific text. The HTML code is: <a class="name-link" href="/colors/thisOne" </a> Yellow I know how to select the class name "find_element_by_class_name("name-li ...

margin around the masterpage

Currently, I am utilizing a master page in ASP.NET (using Visual Studio Express 2015) and overall everything is functioning properly. However, there seems to be an unsightly space around the edges when viewing it in a browser. While not a critical issue, ...

Attempting to squeeze a lengthy word into a small span

Apologies for my poor English. I tried searching for an answer online but couldn't find a solution. Maybe I'm just not able to figure it out myself. All I need is to make those long words fit inside a button. This is how it currently looks. My ...

Placeholder fails to appear

After implementing some jQuery validation, I wanted to display a text as a placeholder when the user skipped out of an input field without entering anything. This is the code I wrote: $('input[type="text"]').on('blur', function() { ...

Is it possible to transfer the style object from PaperProps to makeStyles in Material UI?

I am working with a Material UI Menu component and attempting to customize its border. Currently, I am only able to achieve this customization by using PaperProps inline on the Menu element. However, I already have a makeStyles object defined. Is there a ...