How to scroll the page when a modal dialog is longer than the screen?

前端 未结 11 528
时光取名叫无心
时光取名叫无心 2020-12-22 20:42

I have a modal dialog in my app which can get quite long in the y direction. This introduces a problem whereby some of the content of the dialog is hidden off the bottom of

11条回答
  •  情深已故
    2020-12-22 21:05

    I wanted to add my pure CSS answer to this problem of modals with dynamic width and height. The following code also works with the following requirements:

    1. Place modal in center of screen
    2. If modal is higher than viewport, scroll dimmer (not modal content)

    HTML:

    
    

    CSS/LESS:

    .modal {
        position: fixed;
        display: flex;
        align-items: center;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        padding: @qquad;
        overflow-y: auto;
        background: rgba(0, 0, 0, 0.7);
        z-index: @zindex-modal;
    
        &__content {
            width: 900px;
            margin: auto;
            max-width: 90%;
            padding: @quad;
            background: white;
            box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.75);
        }
    }
    

    This way the modal is always within the viewport. The width and height of the modal are as flexible as you like. I removed my close icon from this for simplicity.

提交回复
热议问题