0

Need to display each item width 50% with overflow-x: scroll.

But it is displaying 3 items instead 2 items.

Here is my desired output-

enter image description here

But my output is like this-

enter image description here

.products {
  flex-flow: row nowrap;
  overflow-x: scroll;
  white-space: nowrap;
  display: flex;
}

.products li {
  width: calc(100% / 2);
}
<ul class="products">
  <li class="temashop-product">
    <a href="#">
      <img src="assets/images/product-placeholder.png" alt="Product">
      <h2>Product heading</h2>
      <p class="short-description">Product description</p>
      <span class="price">$35.00</span>
    </a>
  </li>
  <li class="temashop-product">
    <a href="#">
      <img src="assets/images/product-placeholder.png" alt="Product">
      <h2>Product heading</h2>
      <p class="short-description">Product description</p>
      <span class="price">$35.00</span>
    </a>
  </li>
  <li class="temashop-product">
    <a href="#">
      <img src="assets/images/product-placeholder.png" alt="Product">
      <h2>Product heading</h2>
      <p class="short-description">Product description</p>
      <span class="price">$35.00</span>
    </a>
  </li>
  <li class="temashop-product">
    <a href="#">
      <img src="assets/images/product-placeholder.png" alt="Product">
      <h2>Product heading</h2>
      <p class="short-description">Product description</p>
      <span class="price">$35.00</span>
    </a>
  </li>
</ul>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Hakik Zaman
  • 175
  • 15

2 Answers2

1

Don't use width on the li use flex properties like so:

.products {
  flex-flow: row nowrap;
  overflow-x: scroll;
  white-space: nowrap;
  display: flex;
}

.products li {
  flex: 0 0 50%;
}
<ul class="products">
  <li class="temashop-product">
    <a href="#">
      <img src="assets/images/product-placeholder.png" alt="Product">
      <h2>Product heading</h2>
      <p class="short-description">Product description</p>
      <span class="price">$35.00</span>
    </a>
  </li>
  <li class="temashop-product">
    <a href="#">
      <img src="assets/images/product-placeholder.png" alt="Product">
      <h2>Product heading</h2>
      <p class="short-description">Product description</p>
      <span class="price">$35.00</span>
    </a>
  </li>
  <li class="temashop-product">
    <a href="#">
      <img src="assets/images/product-placeholder.png" alt="Product">
      <h2>Product heading</h2>
      <p class="short-description">Product description</p>
      <span class="price">$35.00</span>
    </a>
  </li>
  <li class="temashop-product">
    <a href="#">
      <img src="assets/images/product-placeholder.png" alt="Product">
      <h2>Product heading</h2>
      <p class="short-description">Product description</p>
      <span class="price">$35.00</span>
    </a>
  </li>
</ul>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
0

The problem is the flex-shrink property.

In a flex container, flex-shrink defaults to 1, meaning that flex items are allowed to shrink to avoid overflowing the container.

You need to disable this feature, so that flex items always keep their defined width.

Add this to your code:

.products li {
   width: calc(100% / 2);
   flex-shrink: 0; /* new */
}

Or, more efficiently, just use this:

.products li {
   flex: 0 0 50%; /* flex-grow, flex-shrink, flex-basis */
 }

More details @MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/flex-shrink

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701