How to prevent column break within an element?

后端 未结 18 2316
刺人心
刺人心 2020-11-22 16:09

Consider the following HTML:

  • Number one
  • Number two
  • &
18条回答
  •  感情败类
    2020-11-22 16:41

    The correct way to do this is with the break-inside CSS property:

    .x li {
        break-inside: avoid-column;
    }
    

    Unfortunately, as of October 2019, this is not supported in Firefox but it is supported by every other major browser. With Chrome, I was able to use the above code, but I couldn't make anything work for Firefox (See Bug 549114).

    The workaround you can do for Firefox if necessary is to wrap your non-breaking content in a table but that is a really, really terrible solution if you can avoid it.

    UPDATE

    According to the bug report mentioned above, Firefox 20+ supports page-break-inside: avoid as a mechanism for avoiding column breaks inside an element but the below code snippet demonstrates it still not working with lists:

    .x {
        column-count: 3;
        width: 30em;
    }
    
    .x ul {
        margin: 0;
    }
    
    .x li {
        -webkit-column-break-inside: avoid;
        -moz-column-break-inside:avoid;
        -moz-page-break-inside:avoid;
        page-break-inside: avoid;
        break-inside: avoid-column;
    }
    • Number one, one, one, one, one
    • Number two, two, two, two, two, two, two, two, two, two, two, two
    • Number three

    As others mention, you can do overflow: hidden or display: inline-block but this removes the bullets shown in the original question. Your solution will vary based on what your goals are.

    UPDATE 2 Since Firefox does prevent breaking on display:table and display:inline-block a reliable but non-semantic solution would be to wrap each list item in its own list and apply the style rule there:

    .x {
        -moz-column-count: 3;
        -webkit-column-count: 3;
        column-count: 3;
        width: 30em;
    }
    
    .x ul {
        margin: 0;
        -webkit-column-break-inside: avoid; /* Chrome, Safari */
        page-break-inside: avoid;           /* Theoretically FF 20+ */
        break-inside: avoid-column;         /* IE 11 */
        display:table;                      /* Actually FF 20+ */
    }
    • Number one, one, one, one, one
    • Number two, two, two, two, two, two, two, two, two, two, two, two
    • Number three

提交回复
热议问题