Sticky

React Bootstrap 5 Sticky

Sticky is a component which allows elements to be locked in a particular area of the page. It is often used in navigation menus.

Note: Read the API tab to find all available options and advanced customization


Basic example

To start use sticky just create MDBSticky component

        
            
          import React from 'react';
          import { MDBSticky } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBSticky
                stickyBoundary
                tag='a'
                className='btn btn-outline-primary bg-white'
                href='https://mdbootstrap.com/docs/jquery/getting-started/download/'
              >
                Download MDB
              </MDBSticky>
            );
          }
          
        
    

Sticky bottom

You can pin element to bottom by adding stickyPosition="bottom"

        
            
          import React from 'react';
          import { MDBSticky } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBSticky
                stickyBoundary
                stickyPosition='bottom'
                stickyDirection='both'
                tag='img'
                src='https://mdbcdn.b-cdn.net/wp-content/uploads/2018/06/logo-mdb-jquery-small.webp'
                alt='MDB Logo'
              />
            );
          }
          
        
    

Boundary

Set stickyBoundary so that sticky only works inside the parent element. Remember to set the correct parent height.

        
            
          import React from 'react';
          import { MDBSticky } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBSticky
                stickyBoundary
                tag='img'
                src='https://mdbcdn.b-cdn.net/wp-content/uploads/2018/06/logo-mdb-jquery-small.webp'
                alt='MDB Logo'
              />
            );
          }
          
        
    

Outer element as a boundary

You can specify an element selector to be the sticky boundary.

Stop here
        
            
          import React from 'react';
          import { MDBSticky } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <>
                <div>
                  <MDBSticky
                    stickyBoundary='#sticky-stop'
                    stickyDirection='both'
                    tag='img'
                    src='https://mdbcdn.b-cdn.net/wp-content/uploads/2018/06/logo-mdb-jquery-small.webp'
                    alt='MDB Logo'
                  />
                </div>
                <div id='sticky-stop' className='mt-auto' style={{ height: '5rem' }}>
                  Stop here
                </div>
              </>
            );
          }
          
        
    

Direction

Default direction of sticky component is down. You can change it by setting stickyDirection="top" or stickyDirection="both"

        
            
          import React from 'react';
          import { MDBSticky } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <>
                <MDBSticky stickyBoundary tag='a' className='btn btn btn-primary' stickyDirection='up'>
                  Up
                </MDBSticky>
                <MDBSticky stickyBoundary tag='a' className='btn btn btn-primary' stickyDirection='down'>
                  Down
                </MDBSticky>
                <MDBSticky stickyBoundary tag='a' className='btn btn btn-primary' stickyDirection='both'>
                  Both
                </MDBSticky>
              </>
            );
          }
          
        
    

Animation

You can add an animation that will run when the sticky starts and when the sticky element is hidden. just specify the css class of the animation using the stickyAnimationSticky and stickyAnimationUnsticky properties.

Remember that not every animation will be appropriate. We suggest using the animations used in the example below.

        
            
          import React from 'react';
          import { MDBSticky } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBSticky
                id='MDB-logo'
                tag='img'
                src='https://mdbcdn.b-cdn.net/wp-content/uploads/2018/06/logo-mdb-jquery-small.webp'
                alt='MDB Logo'
                stickyAnimationSticky='slide-in-down'
                stickyAnimationUnsticky='slide-up'
                stickyBoundary
              />
            );
          }
          
        
    

Sticky with navbar

By default, sticky locks the element at the edge of the screen. If you have a navbar element on your website, it will hide behind it. You can prevent this by setting an offset and delay

        
            
          import React from 'react';
          import { MDBSticky } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <>
                <nav className='navbar sticky-top navbar-light bg-light mb-3' style={{ top: '100px' }}>
                  <div className='container-fluid'>
                    <a className='navbar-brand' href='#'>
                      Navbar
                    </a>
                  </div>
                </nav>
                <MDBSticky
                  id='MDB-logo'
                  tag='img'
                  src='https://mdbcdn.b-cdn.net/wp-content/uploads/2018/06/logo-mdb-jquery-small.webp'
                  alt='MDB Logo'
                  stickyBoundary
                  stickyDirection='both'
                  stickyOffset={175}
                  stickyDelay={175}
                />
              </>
            );
          }
          
        
    

Sticky - API


Import

        
            
          import { MDBSticky } from 'mdb-react-ui-kit';
        
        
    

Properties

MDBSticky

Name Type Default Description Example
className String '' Add custom class to MDBSticky <MDBSticky className="class" />
stickyAnimationSticky String '' Set sticky animation <MDBSticky stickyAnimationSticky="fade-in" />
stickyAnimationUnsticky String '' Set unsticky animation <MDBSticky stickyAnimationUnsticky="fade-out" />
stickyBoundary Boolean false set to true to stop sticky on the end of the parent <MDBSticky stickyBoundary />
stickyDelay Number 0 Set the number of pixels beyond which the item will be pinned <MDBSticky stickyDelay={100} />
stickyDirection String 'down' Set the scrolling direction for which the element is to be stikcky <MDBSticky stickyDirection='up' />
stickyOffset Number 0 Set the distance from the top edge of the element for which the element is sticky <MDBSticky stickyOffset={100} />
stickyPosition String 'top' Set the edge of the screen the item should stick to <MDBSticky stickyPosition="down" />