Why is content overflowing in flexbox?

人盡茶涼 提交于 2019-12-04 12:55:25

By default, flex items are set to flex-shrink: 1 (source).

This means they can shrink to avoid overflowing the container.

Disable this feature. Make your flex items flex-shrink: 0.


UPDATE (based on comments):

  • It doesn't work in Firefox.

    Actually, the solution above was tested in Chrome, Firefox and Edge. It works (fiddle demo).

  • It still works in Firefox when flex-shrink: 0 is removed.

    This is because in Firefox another default setting is in play: min-height: auto. This means that flex items can't shrink below the height of their content. Chrome does an "intervention" on this, which overrides the spec setting.

    See this post for a full explanation: Why doesn't flex item shrink past content size?

    The flex-shrink: 0 approach is a solid cross-browser solution.

  • The margin on the last item collapses.

    This likely happens because the containing block is "over-constrained".

    See this post for a full explanation and solutions: Last margin / padding collapsing in flexbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!