Home > database >  Why does position=sticky not work for an element next to AgGrid?
Why does position=sticky not work for an element next to AgGrid?

Time:07-07

I am trying to stick a button to the top of its parent element.

I have followed this tutorial which works fine. Unfortunately, sticky will no longer work, when the elements sibling contains an AgGrid instead of just a simple/plain paragraph/div, see this example

Is there any way to make this button sticky to the top without changing the AgGrids css?

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Ag Grid App</title>
    <!-- Include the JS for AG Grid -->
    <script src="https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.noStyle.js"></script>
    <!-- Include the core CSS, this is needed by the grid -->
    <link
      rel="stylesheet"
      href="https://unpkg.com/ag-grid-community/dist/styles/ag-grid.css"
    />
    <!-- Include the theme CSS, only need to import the theme you are going to use -->
    <link
      rel="stylesheet"
      href="https://unpkg.com/ag-grid-community/dist/styles/ag-theme-alpine.css"
    />
    <style>
      div.sticky {
        position: -webkit-sticky;
        position: sticky;
        top: 0;
        background-color: #666;
        padding: 40px;
        font-size: 25px;
      }
    </style>
  </head>
  <body>
    <div >
      <button>Deselect Rows</button>
    </div>
    <div id="myGrid"  style="height: 1500px"></div>
    <script type="text/javascript">
      const eGridDiv = document.getElementById("myGrid");
      new agGrid.Grid(eGridDiv, {});
    </script>
  </body>
</html>

CodePudding user response:

You can try adding a z-index: 1; to your style to make the element come to to the front, see the snippet below:

const eGridDiv = document.getElementById("myGrid");
new agGrid.Grid(eGridDiv, {});
div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  z-index: 1;       
  background-color: #666;
  padding: 40px;
  font-size: 25px;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Ag Grid App</title>
    <!-- Include the JS for AG Grid -->
    <script src="https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.noStyle.js"></script>
    <!-- Include the core CSS, this is needed by the grid -->
    <link
      rel="stylesheet"
      href="https://unpkg.com/ag-grid-community/dist/styles/ag-grid.css"
    />
    <!-- Include the theme CSS, only need to import the theme you are going to use -->
    <link rel="stylesheet" href="https://unpkg.com/ag-grid-community/dist/styles/ag-theme alpine.css"
    />
  </head>
  <body>
    <div >
      <button>Deselect Rows</button>
    </div>
    <div id="myGrid"  style="height: 1500px"></div>
  </body>
</html>

Hope this helps :)

  • Related