``How can I create a navigation bar that remains fixed while the section below fills the entire height

Struggling with creating a fixed nav bar and full-height sections with content? Having issues with the second section after scrolling?

Here's the code I've tried:

*,
*::after,
*::before {
    margin: 0px;
    padding: 0px;
    box-sizing: inherit;
    -webkit-box-sizing: inherit;
}
body,
html {
    margin: 0px;
    padding: 0px;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
}
nav {
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    text-align: center;
    padding: 10px 0px;
    background: tomato;
}

{... more CSS code ...}

Could someone provide guidance and help me find a solution to this issue?

Answer №1

It is not recommended to have nested <section>. A better approach would be to replace your outer <section> with

<div class="wrapper">...</div>

Next, utilize the wrapper class to apply a margin-top: 54px, which corresponds to the height of the navigation.

.wrapper {
    margin-top: 54px;
    height: 100vh;
    background: #303030;
    padding: 0px;
}

To address the overlap issue, use the calc function to dynamically set the height like so: height: calc(100vh - 52px);

*,
*::after,
*::before {
    margin: 0px;
    padding: 0px;
    box-sizing: inherit;
    -webkit-box-sizing: inherit;
}
body,
html {
    margin: 0px;
    padding: 0px;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
}
nav {
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    text-align: center;
    padding: 10px 0px;
    background: tomato;
}
nav ul {
    margin: 0px;
    padding: 0px;
    list-style: none;
    text-align: center;
}
nav ul li {
    display: inline-block;
    margin: 3px;
}
nav ul li a {
    display: block;
    padding: 5px;
    background: #000;
    color: #ddd;
    text-decoration: none;
}
nav ul li a:hover {
    background: #ddd;
    color: #333;
}
section {
    height: 100vh;
    background: #303030;
    margin: 0px;
    padding: 0px;
}
.wrapper {
    margin-top: 54px;
      height: 100vh;
    background: #303030;
    padding: 0px;
}
section p {
    color: #fff;
}
section.green {
    background: green;
    height: calc(100vh - 52px);
    margin: 0px;
    padding: 0px;
}
section.green p {
    color: #ddd;
}
<div class="wrapper">
   <nav>
      <ul>
         <li><a href="javascript:void(0);">Home</a></li>
         <li><a href="javascript:void(0);">About</a></li>
         <li><a href="javascript:void(0);">Service</a></li>
         <li><a href="javascript:void(0);">Contact</a></li>
      </ul>
   </nav>
   <section>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
         tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
         quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
         consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
         cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
         proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      </p>
   </section>
   <section class="green">
      <p>
         Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
         tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
         quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
         consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
         cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
         proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      </p>
   </section>
</div>

Answer №2

Utilize the calc() function to determine the height of each section.


height: calc(100vh - 54px); /*54px represents the height of the navigation bar*/

This will set the height of each section to be equal to the screen height minus the height of the navigation bar.


To eliminate the gap between sections, you should remove the margin-top from the p tag and include padding-top:54px; in the container CSS. The value of 54px corresponds to the height of the navigation bar, allowing sections to align directly below the nav bar.

*,
*::after,
*::before {
  margin: 0px;
  padding: 0px;
  box-sizing: inherit;
  -webkit-box-sizing: inherit;
}

body,
html {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
}

.container {
  height: 100vh;
  padding-top:54px;
}

nav {
  position: fixed;
  top: 0px;
  left: 0px;
  width: 100%;
  text-align: center;
  padding: 10px 0px;
  background: tomato;
}

nav ul {
  margin: 0px;
  padding: 0px;
  list-style: none;
  text-align: center;
}

nav ul li {
  display: inline-block;
  margin: 3px;
}

nav ul li a {
  display: block;
  padding: 5px;
  background: #000;
  color: #ddd;
  text-decoration: none;
}

nav ul li a:hover {
  background: #ddd;
  color: #333;
}

section {
  height: calc(100vh - 54px);
  background: #303030;
  margin: 0px;
  padding: 0px;

}

section p {

  color: #fff;
}

section.green {
  background: green;
  margin: 0px;
  padding: 0px;
}

section.green p {
  color: #ddd;
}
<div class="container">
<nav>
  <ul>
    <li><a href="javascript:void(0);">Home</a></li>
    <li><a href="javascript:void(0);">About</a></li>
    <li><a href="javascript:void(0);">Service</a></li>
    <li><a href="javascript:void(0);">Contact</a></li>
  </ul>
</nav>
<section>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
    dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </p>
</section>
<section class="green">
  <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </p>
</section>
</div>

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

Securing your information in Node.js - Is it possible?

Looking to enhance security by encrypting user passwords and storing the encrypted version in the database. Any recommendations on which encryption algorithm would be most effective for this purpose? Also, curious about the optimal approach for utilizing ...

Is there a way to extract the HTMLElement[] type from the DOM?

In my TypeScript project, I am trying to gather all the top-level elements of a page using the code snippet below: const getHTMLElement() : HTMLElement[] { const doc = document.body.children; const list : HTMLElement[] = []; for (let c of Array.f ...

Having trouble centering the CSS image gallery properly

I recently created a website using HTML and CSS, which you can view here. It seems to be functioning fine overall, but there are some spacing issues within the image gallery section. Specifically, the first space appears larger compared to the others. The ...

<p> appears below the <img> element without the need for a <br> tag

<img src="http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/d1/d1a6cabea45b1f34f639b4f3dba8dd7af91072d4.jpg"><p>text 1</p>&nbsp;&nbsp;&nbsp;<p>text 2</p> Instead of using the <br> tag, I ...

How can I ensure that accented characters display correctly in my HTML?

I encountered some HTML code that features a mix of English and Spanish text: <p><em><span class="consolas">In </span><span class="english">English</span><span class="consolas"> and </span><span><st ...

Modify the div's visibility based on selected option in the dropdown menu

In my PHP file, I have the following codes: <section id="placeOrder"> <h2>Place order</h2> Your details Customer Type: <select name="customerType"> <option value="">Customer Type?</option> <option value="ret ...

Typical procedure for organizing information in HTML tables

I am encountering an issue with a basic table on my HTML page. The format is quite simple: +++++++++++++++++++++++++++++++++++++++++ +Col1 + Col2 + Col3 + Col4 + Col5 + Col6+ +++++++++++++++++++++++++++++++++++++++++ + + + + + ...

Using jQuery, create a smooth sliding effect for the example

My code is nearly functional and can be viewed on this jsFiddle. The current behavior of the code is that it slides down and up a green rectangle. However, I am facing several issues that need to be resolved. The first issue I want to address is achieving ...

How to make a div slide up using jQuery without obstructing text

A scenario is presented where HTML contains two div elements, one with text and the other hidden initially. The goal is to make the second div slide up from below the first div to create a new background color effect. While sliding up works successfully, ...

Creating an HTML Form that Sends an Email

I'm completely stumped as to where the issue lies! The mail() function is working perfectly fine (I tested it with a simple PHP file), but for some reason, my form isn't sending any emails! HTML <section id="contact"> <div id="go ...

When a new item is added to the JSON file, it does not appear on the browser page

Initially, I included 3 elements on the page and they displayed correctly. However, when I added a few more elements, they did not show up on the browser page without any errors. import Image from "next/image" import getUnits from "../libs/g ...

Using Jquery and CSS to create a sleek animation for opening a div box container

Designed an animation to reveal a div container box, but it did not meet the desired criteria. Expectations: Initiate animation horizontally from 0% to 100%. Then expand vertically to 100% height. The horizontal animation from 0% to 100% is not visible ...

A guide on implementing Flex Direction in React

For my latest project, I decided to create a web page using Cards in React. To achieve this, I utilized React Bootstrap for the card components. However, I encountered an issue with setting the flex property. Below is a snippet of my component: import &ap ...

The rounded corners border radius is not displaying when printing

After reviewing a helpful article on the topic from this link, I am still struggling to understand why my border-radius is not showing up when printing. The border-radius properties display correctly in the browser, but fail to print as expected. Here is ...

Spree Deluxe - Customizing color scheme variables

We're struggling to modify the color variables in Spree Fancy. The documentation suggests creating a variables_override.css.scss file. But where exactly should this file be placed? We've tried adding it under the stylesheets folder in assets and ...

What is the best method to adjust the scrollbar to the correct position on the table?

Currently facing an issue with adding a scrollbar on the right side of my table. I have tried using a jQuery plugin called table_scroll.js but encountering difficulties. The problem can be seen in this image: https://i.sstatic.net/XqLBD.png. Any assistan ...

Adjusting the positioning of my webpage's content towards the header?

As a beginner in the world of HTML and CSS, I am currently working on customizing a Tumblr page. The template provided by the platform has been modified to include additional elements. However, I have encountered an issue where there seems to be a gap bet ...

Arranging Columns in Bootstrap

I'm looking to align two columns in a specific way: When they are on the same line (if the screen size allows) I want them to be vertically aligned. The shorter column should be centered within the taller one. If the screen shrinks and the columns s ...

"Encountering a problem with a function showing an undefined error

Trying to incorporate a slider feature into my application led me to reference this w3schools sample. However, upon loading the page, the images briefly appear before disappearing. Additionally, clicking on the arrow or dots triggers an error stating: U ...

Remove any text in the textarea beyond the fifth line

I am looking for a way to implement a functionality where every character after the 5th line in a textarea will be automatically deleted, and the 5th line will only contain a maximum of 15 characters. Currently, I have managed to achieve this to some exte ...