Skip to content

How to make cellHeight fill container height #2583

Closed

Description

@ptorrent

Hello there,

How to create a checkboarder with a fixed container height (without scroll). How to make rows working exactly like column ?

Example:

column : 3
row : 2
with 6 items

image

Column : 3
row : 2
with 3 items

image

colum : 3
row : 3
with 3 items

image

colum : 3
row : 3
with 2 items

image

Actually it's working well for column. If I set column = 3 and I've only 2 item, the size of items are correct. There is an empty space for the 3th item. But we don't have the same behavior for rows...

Container width and height always the same : 800x600

Activity

  1. ptorrent commented on Dec 18, 2023

    @ptorrent
    Author

    By setting grid.cellHeight = 600 / 4 (number of rows). It seems working... is there a way to do it automatically ? "auto" doesn't work..

    If I set row = 4, column = 4 even without item in the grid, the height of .grid-stack is set to 1302px ?

    My grid options :

    {
        "column": 4,
        "row": 4,
        "cellHeight": "auto",
        "margin": 25
    }
    

    The html result :

    <div style=" height: 400px;">
    <style type="text/css" gs-style-id="gridstack-style-13038"></style>
    <div class="grid-stack grid-stack-instance-7169 grid-stack-animate grid-stack-4" gs-current-row="4" style="height: 1302px;"></div>
    </div>
    

    Maybe the question is why the .grid-stack height is set to 1302px ?

  2. adumesny commented on Dec 19, 2023

    @adumesny
    Member

    cellHeight: 'auto' means squared cells, not dividing the page height by # of rows since # of rows is not something typically fixed (unless you set it). asking for a new feature like cellHeight:'fillPage' or something, or specify cellHeight:25%

    related to #787

  3. changed the title [-]How to make a checkerboard with fixed container height[/-] [+]How to make cellHeight fill container height[/+] on Mar 30, 2024
  4. masum-mollik-rocketml commented on May 3, 2024

    @masum-mollik-rocketml

    @ptorrent
    set cellHeight: '1em'

    find the parent element of the grid and set the font size dividing the height by the count of the rows available in the grid. Execute it after view initialization.

    setCardFontSizeBasedOnHeight(gridParentElement: Element|HTMLElement) {
        const height = item.clientHeight;
        item['style'].fontSize = `${height / gridRowCount}px`;
      }
    

    call the method each time the window size changes

  5. billyc commented on Dec 17, 2024

    @billyc

    I have the same need for my situation. This code worked for me: you need to know the total height of the full gridstack, which is always the largest y+height of all grid items.

    Call this when the window size or layout changes. Note the this.grid is saved from Gridstack.init()

    resizeToFillVertical(parentId: string) {
      const parentElement = document.getElementById('parentId')
      if (!parentElement) return
    
      const totalGridHeight = this.grid.getGridItems().reduce((a,b) => {
        return Math.max(a, b.gridstackNode.y + b.gridstackNode.h)
       }, 0)
      const height = parentElement.clientHeight
      const newCellHeight = Math.floor(height / totalGridHeight)
      this.grid.cellHeight(`${newCellHeight}px`)
    }
    
  6. adumesny commented on Dec 17, 2024

    @adumesny
    Member

    @billyc no need to find the row count yourself. just call grid.getRow() or check the gs-current-row attr also set, which both take minRow and row into acount (might be empty space) if set, otherwise grid.engine.getRow() does what you have above.

  7. billyc commented on Dec 17, 2024

    @billyc

    Ah! thanks @adumesny, that definitely makes my code a bit simpler. I don't see getRow() in the API docs... didn't know that method was present.

    Thank you so much for maintaining this library -- it does exactly what I need! 🙏

  8. adumesny commented on Dec 18, 2024

    @adumesny
    Member

    the doc is not auto generated from code - need to change that - so it can be out of date/missing sometimes...

    UPDATE: api doc is now auto-generated.

  9. added a commit that references this issue on Sep 20, 2026
    0a37462
  10. pinned this issue on Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions