Search code examples
cssflexboxcss-multicolumn-layout

box-shadow cut off when using column-count


I need to order my items from top-to-bottom, then left-to-right, e.g.:

1 5
2 6
3 7
4 8

However, box shadow is being cutoff. Reference the snippet: item 3's box shadow is cut off at bottom and item 4 is cut off at the top (in chrome).

There are similar questions to this, however the answers don't apply in this situation. I can't use flex on the container with flex-direction: column as this requires an explicit height and my item count is dynamic. I also can't set the items to display: inline-block as other answers suggest, since I need to control this content with flex.

.container {
  column-count: 2;
  column-gap: 16px;
  width: 500px;
}

.item {
  box-shadow: 0px 3px 1px -2px rgba(0, 0, 0, 0.2), 0px 2px 2px 0px rgba(0, 0, 0, 0.14), 0px 1px 5px 0px rgba(0, 0, 0, 0.12);
  border-radius: 3px;
  margin-bottom: 16px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  break-inside: avoid-column;
}
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item">5</div>
</div>

Two other things I've tried from other similar SO questions that did not work: setting overflow: visible, adding a wrapper around the items with transparent border. Thanks for any suggestions.


Solution

  • Add display: inline-flex and width: 100% properties. break-inside: avoid-column property is invalid.

    .container {
      column-count: 2;
      column-gap: 16px;
      width: 500px;
      margin-top: -2px;
      margin-bottom: -14px;
    }
    
    .item {
      box-shadow: 0px 3px 1px -2px rgba(0, 0, 0, 0.2), 0px 2px 2px 0px rgba(0, 0, 0, 0.14), 0px 1px 5px 0px rgba(0, 0, 0, 0.12);
      border-radius: 3px;
      margin-top: 2px;
      margin-bottom: 14px;
      height: 64px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      width: 100%;
    }
    <div class="container">
      <div class="item">1</div>
      <div class="item">2</div>
      <div class="item">3</div>
      <div class="item">4</div>
      <div class="item">5</div>
    </div>