CSS Flexbox не работает на экранах устройств меньшего размера

#css #flexbox

#css #flexbox

Вопрос:

Я попробовал макет flexbox на этих блоках, и он работает не так, как предполагалось. Я попробовал flex-direction: row и flex-direction: column , чтобы заставить их аккуратно разбиться на 3 вертикальных блока, но, должно быть, я делаю что-то не так.

Нужно ли мне добавлять к этому медиа-запросы, чтобы они соответствовали?

Вот мой код:

 .product-grid {
  display: flex;
  flex-wrap: wrap;
  padding: 0 2px;
  margin: 0;
}

.cell {
  width: 33.333%;
  word-wrap: break-word;
  box-sizing: border-box;
  border: 1px solid #ccc;
}

.product-grid .item {
  background: #ffffff;
  position: relative;
}
.product-grid .cell .item:nth-child(even) {
  background: #eeebe7;
}
.product-grid .item .thumb {
  width: 100%;
  text-align: left;
  padding: 0px;
}
.product-grid .item .con {
  position: absolute;
  right: 20px;
  top: 40px;
  text-align: right;
}
.product-grid .item .con2 {
  position: absolute;
  right: 20px;
  top: 40px;
  text-align: right;
}
.product-grid .item h1 {
  color: #000000;
  font-size: 32px;
  margin: 0;
}
.product-grid .item h2 {
  color: #9fa163;
  font-size: 22px;
  margin: 0;
}
.product-grid .item .btn-details {
  position: absolute;
  right: 50px;
  bottom: 30px;
}
.product-grid .item h2 {
  color: #a46a6a;
}
.product-grid .item .btn-details {
  background-image: url(images/plus2.png);
}

.btn-details {
  background: url(images/plus1.png) right center no-repeat;
  padding: 5px 44px 5px 0;
  font-size: 20px;
  text-transform: uppercase;
  font-weight: 400;
  font-family: "Oswald", sans-serif;
  color: #000000;
  display: inline-block;
}  
 <div class="product-grid">
  <div class="cell">
    <div class="item">
      <div class="thumb"><a href="https://ripelifewines.com/product/un-oaked-chardonnay/"><img class="alignnone" src="http://ripelifewines.com/wp-content/uploads/2016/12/Un-Oaked-Chardonnay2.png" alt="" width="366" height="400" /></a></div>
      <div class="con">
        <h2>Un-Oaked Chardonnay</h2>
        <h1>The Clambake</h1>
      </div>
      <a class="btn-details" href="https://ripelifewines.com/product/un-oaked-chardonnay/">See wine details</a>

    </div>
  </div>
  <div class="cell">
    <div class="item">
      <div class="thumb"><a href="https://ripelifewines.com/product/limited-edition-rose/"><img class="alignnone" src="http://ripelifewines.com/wp-content/uploads/2016/12/Limited-Edition-Rose2.png" alt="" width="370" height="400" /></a></div>
      <div class="con">
        <h2>Limited Edition Rosé</h2>
        <h1>The Clambake</h1>
      </div>
      <a class="btn-details" href="https://ripelifewines.com/product/limited-edition-rose/">See wine details</a>

    </div>
  </div>
  <div class="cell">
    <div class="item">
      <div class="thumb"><a href="https://ripelifewines.com/product/moules-marinieres-brut-sparkling-wine/"><img class="alignnone" src="https://ripelifewines.com/wp-content/uploads/2020/10/admin-ajax.png" alt="" width="366" height="400" /></a></div>
      <div class="con2">
        <h2>Brut Sparkling Wine</h2>
        <h1>Moules Marinières</h1>
      </div>
      <a class="btn-details" href="https://ripelifewines.com/product/moules-marinieres-brut-sparkling-wine/">See wine details</a>

    </div>
  </div>
</div>  

Ответ №1:

Ваше правило CSS для .cell contains width: 33% , которое всегда составляет ~ одну треть ширины экрана — при любом размере. Вы не можете ожидать, что это изменится автоматически.

Но вы можете добавить медиа-запрос (с максимальной шириной для мобильных устройств), где вы устанавливаете ширину .cell разделов на 100%. Тогда каждая ячейка будет занимать всю ширину, и они будут находиться друг под другом.

 @media screen and (max-width: 600px) {
  .cell {
    width: 100%;
  } 
}
  

Ответ №2:

У вас есть относительная ширина ячеек (33%), поэтому они всегда уменьшаются до одной трети ширины контейнера. Если вы удалите это значение, оно «сломается».

Ответ №3:

Ofc, вам нужен запрос @media, чтобы сделать ваш сайт адаптивным.
Добавлен медиа-запрос для iPad и устройств меньшего размера.
Проверьте CSS

 .product-grid {
  display: flex;
  flex-wrap: wrap;
  padding: 0 2px;
  margin: 0;
}

.cell {
  width: 33.333%;
  word-wrap: break-word;
  box-sizing: border-box;
  border: 1px solid #ccc;
}

.product-grid .item {
  background: #ffffff;
  position: relative;
}
.product-grid .cell .item:nth-child(even) {
  background: #eeebe7;
}
.product-grid .item .thumb {
  width: 100%;
  text-align: left;
  padding: 0px;
}
.product-grid .item .con {
  position: absolute;
  right: 20px;
  top: 40px;
  text-align: right;
}
.product-grid .item .con2 {
  position: absolute;
  right: 20px;
  top: 40px;
  text-align: right;
}
.product-grid .item h1 {
  color: #000000;
  font-size: 32px;
  margin: 0;
}
.product-grid .item h2 {
  color: #9fa163;
  font-size: 22px;
  margin: 0;
}
.product-grid .item .btn-details {
  position: absolute;
  right: 50px;
  bottom: 30px;
}
.product-grid .item h2 {
  color: #a46a6a;
}
.product-grid .item .btn-details {
  background-image: url(images/plus2.png);
}

.btn-details {
  background: url(images/plus1.png) right center no-repeat;
  padding: 5px 44px 5px 0;
  font-size: 20px;
  text-transform: uppercase;
  font-weight: 400;
  font-family: "Oswald", sans-serif;
  color: #000000;
  display: inline-block;
}

/*Media Query*/
@media(max-width: 768px){
  .product-grid {
    flex-direction: column;
  }
  .cell {
    width: 100%;
  }
}  
 <div class="product-grid">
  <div class="cell">
    <div class="item">
      <div class="thumb"><a href="https://ripelifewines.com/product/un-oaked-chardonnay/"><img class="alignnone" src="http://ripelifewines.com/wp-content/uploads/2016/12/Un-Oaked-Chardonnay2.png" alt="" width="366" height="400" /></a></div>
      <div class="con">
        <h2>Un-Oaked Chardonnay</h2>
        <h1>The Clambake</h1>
      </div>
      <a class="btn-details" href="https://ripelifewines.com/product/un-oaked-chardonnay/">See wine details</a>

    </div>
  </div>
  <div class="cell">
    <div class="item">
      <div class="thumb"><a href="https://ripelifewines.com/product/limited-edition-rose/"><img class="alignnone" src="http://ripelifewines.com/wp-content/uploads/2016/12/Limited-Edition-Rose2.png" alt="" width="370" height="400" /></a></div>
      <div class="con">
        <h2>Limited Edition Rosé</h2>
        <h1>The Clambake</h1>
      </div>
      <a class="btn-details" href="https://ripelifewines.com/product/limited-edition-rose/">See wine details</a>

    </div>
  </div>
  <div class="cell">
    <div class="item">
      <div class="thumb"><a href="https://ripelifewines.com/product/moules-marinieres-brut-sparkling-wine/"><img class="alignnone" src="https://ripelifewines.com/wp-content/uploads/2020/10/admin-ajax.png" alt="" width="366" height="400" /></a></div>
      <div class="con2">
        <h2>Brut Sparkling Wine</h2>
        <h1>Moules Marinières</h1>
      </div>
      <a class="btn-details" href="https://ripelifewines.com/product/moules-marinieres-brut-sparkling-wine/">See wine details</a>

    </div>
  </div>
</div>