Prevent flex items from overflowing a container

前端 未结 7 1908
说谎
说谎 2021-01-30 05:13

How do I make my flex item (article in this example), which has flex-grow: 1; not to overflow it\'s flex parent/container (main<

7条回答
  •  野性不改
    2021-01-30 05:31

    Your flex items have

    flex: 0 0 200px; /* 

    That means:

    • The

    • The

      will start at the width given by the content.

      Then, if there is available space, it will grow to cover it.

      Otherwise it won't shrink.

    To prevent horizontal overflow, you can:

    • Use flex-basis: 0 and then let them grow with a positive flex-grow.
    • Use a positive flex-shrink to let them shrink if there isn't enough space.

    To prevent vertical overflow, you can

    • Use min-height instead of height to allow the flex items grow more if necessary
    • Use overflow different than visible on the flex items
    • Use overflow different than visible on the flex container

    For example,

    main, aside, article {
      margin: 10px;
      border: solid 1px #000;
      border-bottom: 0;
      min-height: 50px; /* min-height instead of height */
    }
    main {
      display: flex;
    }
    aside {
      flex: 0 1 200px; /* Positive flex-shrink */
    }
    article {
      flex: 1 1 auto; /* Positive flex-shrink */
    }
    don't let flex item overflow container.... y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y

提交回复
热议问题