отображение блоков в адаптивной версии - PullRequest
0 голосов
/ 21 февраля 2019

У меня есть 6 изображений, обернутых во внешнюю div.

Я хотел бы видеть 2 изображения в строке в мобильной версии, поэтому должно быть 3 столбца с 2 изображениями в строке.

У меня есть этот HTML:

           * {
  box-sizing: border-box;
}

img {
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: 100%;
}

.picture-box {
  width: 70%; /* limit screen width - max width could have been used aswell */
  margin: 0 auto; /* center content */
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

.ring {
  padding: 10px;
  text-align: center; /* Center ring div */
}

@media screen and (min-width: 1200px) {
   .ring {
      width: 25%;
    }
}

@media screen and (max-width: 1199px) {
   .ring {
      width: 33.33%;
    }
}

@media screen and (max-width: 768px) {
    .ring {
      width: 50%;
    }
    
    .picture-box {
      width: 100%;
    }
}

.thumb {
    display: inline-block;
  max-width: 200px;
  padding: 10px;
  border: 1px solid blue;
}
<div class="picture-box">
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
</div>  

Пример работы с JSFiddle

Я пытался изменить flex-direction с row на column, ноэто не помогло.Мне может понадобиться написать ширину для них, но я не знаю, как, и теперь я не могу сделать их в столбце.Как я могу решить это?

1 Ответ

0 голосов
/ 21 февраля 2019

Это то, что вы ищете?

Я использовал width, как вы и задали в своем вопросе - это один из способов сделать это.

Таким образом, вы можете контролировать, какмного блоков (.ring's), которые вы хотите показать в каждой точке останова.

* {
  box-sizing: border-box;
}

img {
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: 100%;
}

.picture-box {
  width: 70%; /* limit screen width - max width could have been used aswell */
  margin: 0 auto; /* center content */
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.ring {
  padding: 10px;
  text-align: center; /* Center ring div */
}

@media screen and (min-width: 1200px) {
   .ring {
      width: 25%;
    }
}

@media screen and (max-width: 1199px) {
   .ring {
      width: 33.33%;
    }
}

@media screen and (max-width: 768px) {
    .ring {
      width: 50%;
    }
    
    .picture-box {
      width: 100%;
    }
}

.thumb {
  display: inline-block;
  max-width: 200px;
  padding: 10px;
  border: 1px solid blue;
}
<div class="picture-box">
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
  <div class="ring">
    <div class="thumb">
      <img src="https://via.placeholder.com/200">
    </div>
  </div>
</div>

Пример JSFiddle

...