Fixed navbar stays at the top with body occupying 100% of the screen

In the process of developing a react app, I encountered an issue with a sticky top navbar from Bootstrap. After setting the body to height: 100%, the navbar no longer sticks as intended. I cannot use vh for one of my pages due to mobile content display issues, so I prefer sticking to percentages. Is there any alternative solution for maintaining the sticky behavior while keeping the body height at 100%? Below is the code snippet where I set the body height.

html,
body,
#root {
  height: 100%;
  max-height: 100%;
}

Answer №1

To achieve the desired layout, you can utilize the position CSS property (https://developer.mozilla.org/en-US/docs/Web/CSS/position).

Additionally, for further control over the stacking order of elements, you may also make use of the z-index property (https://developer.mozilla.org/en-US/docs/Web/CSS/z-index)

Check out this example:

HTML

<div id="container">
  <header>
    <div>content</div>
    <div>content</div>
    <!-- ... -->
    <div>content</div>
    <div>content</div>
  </header>
  <main>
    <!-- ... -->
  </main>
</div>

CSS

#container {
    /* to maintain document flow */
    position: relative;
    height: 100vh;
}

header {
    /* to stick at the top while scrolling */
    position: sticky; 
    top: 0;
    left: 0;
    right: 0;

    /* additional styling */
    background-color: lightblue;
    height: 2rem;
    display: flex;
    justify-content: space-around;
}

main {
  /* to show overflowing content with a scrollbar */
  overflow: auto hidden;

  /* additional styling */
  height: 200%;
  background-color: lightpink;
}

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

Leveraging Flexbox and flexGrow in conjunction with Textfield

I am looking to use Flexbox to separate my components on the screen in a way that positions them correctly. I have 3 rows with textfields that need specific ratios related to the full width: row 1 : 2, 1, 1 row 2 : 1, 1 row 3 : 1, 1, 2 I attempted to ach ...

What is the maximum size allowed for a background image in CSS?

Within a 70 by 50 px box, I have various SVG images with different aspect ratios - some portrait and others landscape. Setting background-size: 70px auto; works for landscape images but distorts the portraits by stretching them vertically. Is there a way t ...

Tips for keeping the mobile menu active and responsive

Struggling to implement this menu on my website, I encountered a problem: how do I keep the menu active? I envision the menu bar changing when users navigate to the Home page, appearing like this: https://i.sstatic.net/z2fbX.png I attempted to use the cl ...

``Need help setting up a fallback source for the Image component in Next.JS? Here's

A new component called Avatar has been developed to encapsulate the usage of the Image component from Next.JS. The parent component of Avatar relies on the src prop, which is a string representing the image source (typically remote). In cases where the src ...

Utilizing React portals for displaying a modal component upon clicking a table row

As I familiarize myself with React, the concept of portals in the developer documentation caught my attention. However, I am struggling to grasp how this portal component renders on-demand and how data can be passed to it to populate a modal. My current s ...

Having an issue with HTML and JavaScript where the button won't open when pressed. Any help is appreciated

https://jsbin.com/haluhifuqe/edit?html,js,output I'm facing an issue with my HTML & JavaScript code - when I click the button, it doesn't open. Can anyone help me out? <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...

How to create a dynamic background color animation in jQuery similar to a progress bar animation

I have a container with text content (for example: My Name) and it is displayed in a RED background color along with a button. What I am looking for : When the button is clicked, I want to change the background color of the container from RED to BLUE, si ...

What sets apart compressing test.min.css from compressing test.css?

Recently, I have transitioned to a new job role with a different employer. In my previous workplace, we utilized LESS and compiled it into a .css file before compressing it further into a .min.css file. However, in my current position, we also work with L ...

React password eye icon feature designed for numerous input fields

I have a total of three password fields, each equipped with an eye icon that allows users to toggle the visibility of their passwords. At present, I am facing an issue where toggling the visibility of one field affects all other fields as well when using ...

The Bootstrap button is not appearing on my website as expected

Why isn't my button appearing on the website? I've reviewed the code numerous times and it appears to be correct, but the button remains invisible. Any suggestions as to what might be causing this problem? I'm using ReactJS to construct my ...

jQuery UI smooths out the transitions, making the effect more gradual and fluid

Is there a way to make my toggle sidebar scroll smoothly using jQuery UI? Currently, it has a jerky behavior and I would like it to have a smoother left/right effect. I want the outer shell to slide smoothly just like the inner container does, instead of ...

Is it possible to achieve the same functionality as :first-child by employing :nth-of-type without a selector?

Currently tackling a console warning related to MUI5/emotion (I believe) I have noticed that the pseudo class ":first-child" might pose potential risks during server-side rendering. It may be worth considering replacing it with ":first-of-type". I wonder ...

HTML text not lining up with SVG text

Why is the positioning of SVG Text slightly off when compared to CSS text with the same settings? The alignment seems awkwardly offset. Any help in understanding this would be much appreciated! Even with x: 50% and the relevant text-anchor property set to ...

What is the best way to center and adjust the size of an image in a jumbotron?

I need some assistance with centering and resizing an image above my navigation bar. I want the image to be positioned right above the navigation bar, similar to how it appears on other websites. Can anyone help me achieve this? .jumbotron { text-alig ...

Position the image slider uniformly at the bottom using CSS (utilizing the Slick Carousel library)

I am utilizing the slick carousel library to create a unique and elegant slider. However, when using images with varying heights, I am encountering issues in aligning the images at the same bottom: https://i.stack.imgur.com/ab5s3.png Here is my code snip ...

Switching Present Tab in Rails

Within my application, I have a set of tabs displayed at the top thanks to a general layout in application.html.erb. Here's how they are structured: <li class="current"><%= link_to "Home", provider_path(current_user.id), :method=> "GET"%& ...

What is the best way to handle the parsing of a JSON object in React using lifecycle methods?

Please assist me in parsing this data using lifecycle methods: {"blocks":[{ "key":"33du7", "text":"Hello there!", "type":"unstyled", "depth":0, "inlineStyleRanges":[], "entityRanges":[], "data":{}}], "entityMap":{} } I am trying to dis ...

Utilize Bootstrap columns to maximize vertical space efficiency

I recently came across a bootstrap layout like this: https://i.sstatic.net/VXBCF.png In this setup, there is a single .row that contains 8 .col-lg-3 divs. However, I noticed that there is excessive vertical whitespace present, which not only looks undesi ...

Developing alternatives for CSS Grid

Currently, I am faced with an intriguing challenge involving a layout that includes a container containing 9 div elements. Each of these divs is assigned one of three classes: column-1, column-2, or column-3. <div class="grid cols-3"> <div cl ...

Efficient React search feature with pagination that avoids redundant setState calls

Currently in the process of incorporating search functionality with pagination in React. After reviewing numerous examples, it appears they all involve using setState() twice, both before and after making an AJAX call to the backend. Here is a snippet of m ...