Use justify-content: flex-end and to have vertical scrollbar

前端 未结 5 430
無奈伤痛
無奈伤痛 2020-12-16 09:32

I have chat and I need to scroll all content to bottom. I want to use justify-content: flex-end and to have vertical scrollbar.

5条回答
  •  渐次进展
    2020-12-16 10:10

    I just had to face this issue myself and, after concluding it is a bug, I came up with a workaround.

    In summary, don't use justify-content: flex-end but rather put a margin-top: auto on the first child. Unlike flex-end this doesn't break the scrollbar functionality, and it bottom-aligns the contents when they're not overflowing the container.

    Example based on @SrdjanDejanovic's fiddle is at https://jsfiddle.net/peter9477/4t5r0t5b/

    In case the example isn't available, here's the relevant CSS:

    #container {
        overflow-y: auto;
        display: flex;
        flex-flow: column nowrap;
        /* justify-content: flex-end; DO NOT USE: breaks scrolling */
    }
    #container > :first-child {
        margin-top: auto !important;
        /* use !important to prevent breakage from child margin settings */
    }
    

    An alternative workaround that I believe I've also used is to add an extra container for the scrollbar. Use the flex-end on the inner container and have the outer container handle the scrolling. I generally dislike workarounds that require adding dummy elements though, so I prefer my CSS-only solution above.

提交回复
热议问题