Place a "sticky" button directly below a second "sticky" navigation bar

Within my app file, I have customized components like an appbar and various page elements:

const styles = theme => ({
  appBar: {
          width:'100%',
  },
});

class App extends Component {

  render() {
    const {classes} = this.props;
    return (
      <React.Fragment>
      <CssBaseline />
      <AppBar position="sticky" className={classes.appBar} />
      <Page1 show={someCondition} />
      <Page2 show={someCondition} />
      .
      .
      <Page99 show={someCondition} />
     </React.Fragment>
    );
  }
}

The appbar is set to remain sticky always visible at the top. Every page component contains a button located at the top of that specific page:

const styles = theme => ({
      button: {
              width:'100%',
      },
    });

class Page99 extends Component {

  render() {
    const {classes} = this.props;
    return (
      <React.Fragment>
         <div>
            <Button variant="contained" className= {classes.button}>
              Action Button
            </Button>
         </div>
        {/* Some other stuff */>
     </React.Fragment>
    );
  }
}

I am now attempting to keep this button fixed directly under the appbar. As the user scrolls down, I want the button to stay in place just like the appbar does. I attempted to apply sticky positioning, hoping it would align below the appbar, but it did not work as expected. The dynamic nature of the appbar makes it challenging to predict its exact height, so fixed positioning won't suffice across different resolutions.

Answer №1

To position the page container, set it as relative and the button as absolute. This way, you can align it to the top right or any other desired location on the page.

Take a look at this fiddle to see if it meets your requirements:

.componentparent {
  position: relative;
  height:100px;
  max-height: 50px;
  overflow: auto;
}

.button {
  position: fixed;
  top: 30px;
}

.otherelements{
  top: 70px;
  position: relative;
}
<div id="parent-container">
  <div> your app bar </div>
  <div class='componentparent'>
    <button class='button'>my button</button>
    <div class='otherelements'>your component</div>
  </div> 
</div>

Answer №2

To seamlessly integrate a button within your appbar, adjust its position to absolute and include the top: 100% attribute to align it precisely at the bottom of the appbar.

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

What is the CSS syntax for styling children that are not immediate descendants?

My query is about whether it's appropriate to reference a child of an element that is not a direct child. Imagine having this HTML structure: <form class="formation"> <p> <span> <input class="phone input"> ...

There seems to be an issue with the functionality of React Native's new Date() method

Can you help me with this code I have for formatting a date? const postDate = new Date(date); I'm trying to use it like this: <Text style={styles.dateText}>{postDate}</Text> But I keep getting this error and I'm not sure h ...

A comprehensive guide on how to find keywords within an array and then proceed to make all text within the parent div tag stand

I am currently looking for a webpage that displays a list of products based on keywords from an array. Once it detects any word in the array, it highlights it with a red background - everything is working smoothly so far. However, I now wish for the script ...

The positioning of sub-menu items is incorrect

I would like the subitems in the menu to be positioned directly under the main menu items instead of being slightly offset to the right. CSS ul#menu { float:left; width:100%; margin:0; padding:0; list-style-type:none; background-c ...

Transform the code provided by bundleMDX into an HTML string specifically for RSS, all the while utilizing the mdx-bundler

I am currently working on developing an RSS reader and I need to convert the code returned by bundleMDX into a string. This is necessary so that I can utilize it with ReactDOMServer.renderToStaticMarkup(mdx) in my project. You can find a similar implement ...

Enhance User Experience with the Tooltip Feature and Customized

Here is the jQuery code I am using to style my tooltips: $(function() { // select all input fields within #tooltips and attach tooltips to them $("#tooltips :input").tooltip({ // place tooltip on the right edge position: "cen ...

Is it possible to eliminate the *:after effect for specific elements in my CSS?

I struggle with CSS, so I decided to use a library that includes the following CSS code: *, *:before, *:after { box-sizing: inherit ; } While this code works well for some views, it messes up others and is unnecessary for some. For instance, in this b ...

Integrate stylish checkbox design into a form in Rails

I discovered some beautiful css styles for checkboxes that I would like to use instead of the regular ones. You can find these styles here: . Could someone guide me on how to implement this change? This is the code snippet from the form: <div class=" ...

What are the typical architecture patterns used for Java/Kotlin Android apps in the Common App State

Which pattern is predominantly used for state management in Java or Kotlin Android applications? I am transitioning from React Native to Kotlin, where I solely relied on the React Redux architecture. What is the prevailing architecture being utilized tod ...

A pair of div containers with one set to adjust its height automatically and the other to

I have a container with a height set to 100%. Inside, there are two child divs. Is it achievable to achieve the following heights: First div equal to the height of its content Second div equal to the remaining free space in the container ...

Techniques for simulating functions in Jest

I have a pair of basic components that I'm currently creating tests for using jest. My goal is to verify that when I click on a currencyItem, the corresponding array gets added to the select state. To achieve this, I am passing the handleCurrencyToggl ...

Create a vibrant stroke and conclude with a circular marker beneath the content with CSS

Can you create a vibrant line with a dot underneath text using CSS, similar to the example shown in the image? The underline should begin with some spacing or padding. .underline { text-decoration: underline; text-underline-offset: 10px; displa ...

Is there a way to import Nested JSON into my DataSource in React Native?

Can someone assist me with incorporating Nested JSON into my DataSource in React Native? I am struggling with this task, could it be because I am a novice? { success: true, result: { types: [], information: { ...

What is the best way to eliminate unexplained spacing at the bottom of a webpage using CSS?

I am struggling to create a basic webpage using Bootstrap and D3, and I can't seem to figure out how to remove the excess whitespace at the bottom. Any tips on how to resolve this issue would be greatly appreciated. I attempted to set the min-height ...

Mastering the integration of Sass with NextJS

After successfully styling my NextJS app with SCSS modules, I encountered an unexpected error when I returned to work on it later: Syntax error: Invalid CSS after "...ound: variables": expected expression (e.g. 1px, bold), was ".$main-gradie ...

Assign the state to a new object by preserving matching values from the previous state

In my current state, I have an object structured like this: stateObject = {0: 400, 1: 500, 2: 600} Whenever my component rerenders on componentWillUpdate, an additional column is added carrying over the value at key index 0 (400). My goal is to update th ...

The masonry plugin overlays images on top of each other

I have gone through similar questions but haven't found anything that applies to my specific case. Following an ajax call, I am adding li elements to a ul $.ajax({ url: 'do_load_gallery.php?load=all' }).done(function(result) { ...

Discover the best practices for implementing the metadata API within Next.js 13 while effectively utilizing context

Having trouble displaying layout.tsx with the default theme because it requires a theme provider and involves useContext. This causes Next.js to raise server-side rendering errors. To work around this, I added the 'use client' field to the docume ...

Struggling to effectively use XPath to target LI elements that contain specific text

Below is the HTML code for the list item in question: <li class="disabled-result" data-option-array-index="1" style="">4" (0)</li> Here is my attempt at using JavaScript to hide this list item, but it's not working as expected: var xpat ...

The React table in TanStack is encountering an issue where it is unable to access the properties of undefined when trying to read the

After deciding to incorporate a React library into my project, I stumbled upon the tanstack v8 table. Following a basic table tutorial, I encountered some errors along the way. //Parent next component 'use client' import React, { useEffect, useS ...