如何使项目在移动视图上堆叠

问题描述 投票:0回答:3

我在桌面屏幕上使用显示柔性并排制作了 3 个图像,但现在我想让它们具有响应能力并使项目在移动模式下堆叠。有人可以告诉我该怎么做吗?

代码:

.row {
  display: flex;
}

.column {
  padding: 200px;
  text-align: center;
  flex: 10%;
}

.column img {
  border-radius: 50%;
  width: 300px;
  border: 2px solid white;
}

.column p {
  color: white;
  padding-top: 50px;
  font-size: 24px;
}

.partners-h1 p {
  font-family: "BatmanForeverAlternate";
  font-size-adjust: 10px;
  color: white;
}

.partners-h1 {
  text-align: center;
  font-size: 30px;
}

/* added for demonstration purpose by editor */
body {
  background-color: black;
}
<div class="row">
  <div class="column">
    <img src="images/sneak.jpg">
    <p>Sneak Energy</p>
  </div>
  <div class="column">
    <img src="images/kontrol.png">
    <p>Kontrol Freek</p>
  </div>
  <div class="column">
    <img src="images/astro.jpg">
    <p>Astro Gaming</p>
  </div>
</div>

html css flexbox responsive-design
3个回答
4
投票

我可以想到两种方法来解决这个问题。

方法一:使用媒体查询

.row {
  display: flex;
  flex-direction:column; /* Initially, mobile first */
}

/* If minimum screen width is 320, switch to side by side. Replace 320 with whatever screen size you want to swap between horizontal and stacked view */
@media (min-width:320px)
{
  .row {
    flex-direction:row;
  }
}

.column {
  padding: 200px;
  text-align: center;
  flex: 10%;
}

.column img {
  border-radius: 50%;
  width: 300px;
  border: 2px solid white;
}

.column p {
  color: white;
  padding-top: 50px;
  font-size: 24px;
}

.partners-h1 p {
  font-family: "BatmanForeverAlternate";
  font-size-adjust: 10px;
  color: white;
}

.partners-h1 {
  text-align: center;
  font-size: 30px;
}


/* added for demonstration purpose by editor */
body {
  background-color: black;
}
<div class="row">
  <div class="column">
    <img src="images/sneak.jpg">
    <p>Sneak Energy</p>
  </div>
  <div class="column">
    <img src="images/kontrol.png">
    <p>Kontrol Freek</p>
  </div>
  <div class="column">
    <img src="images/astro.jpg">
    <p>Astro Gaming</p>
  </div>
</div>

方法二:当2张图片不能并排放置时,使用flex-wrap自动堆叠

.row {
  display: flex;
  flex-wrap:wrap;
}

.column {
  padding: 200px;
  text-align: center;
  flex: 10%;
}

.column img {
  border-radius: 50%;
  width: 300px;
  border: 2px solid white;
}

.column p {
  color: white;
  padding-top: 50px;
  font-size: 24px;
}

.partners-h1 p {
  font-family: "BatmanForeverAlternate";
  font-size-adjust: 10px;
  color: white;
}

.partners-h1 {
  text-align: center;
  font-size: 30px;
}


/* added for demonstration purpose by editor */
body {
  background-color: black;
}
<div class="row">
  <div class="column">
    <img src="images/sneak.jpg">
    <p>Sneak Energy</p>
  </div>
  <div class="column">
    <img src="images/kontrol.png">
    <p>Kontrol Freek</p>
  </div>
  <div class="column">
    <img src="images/astro.jpg">
    <p>Astro Gaming</p>
  </div>
</div>


0
投票

添加媒体查询以将屏幕寻址到一定宽度,例如:

@media only screen 
  and (max-width: 800px) { 
    selector { 
      property: value;
    } 
}

然后将

flex-direction
属性从默认的
row
更改为
column
:
flex-direction: column;

媒体查询参考:MDN Web 文档

.row {
  display: flex;
}

@media only screen
  and (max-width: 800px) {
    .row {
      flex-direction: column;
    }
}

.column {
  padding: 200px;
  text-align: center;
  flex: 10%;
}

.column img {
  border-radius: 50%;
  width: 300px;
  border: 2px solid white;
}

.column p {
  color: white;
  padding-top: 50px;
  font-size: 24px;
}

.partners-h1 p {
  font-family: "BatmanForeverAlternate";
  font-size-adjust: 10px;
  color: white;
}

.partners-h1 {
  text-align: center;
  font-size: 30px;
}

/* added for demonstration purpose by editor */
body {
  background-color: black;
}
<div class="row">
  <div class="column">
    <img src="images/sneak.jpg">
    <p>Sneak Energy</p>
  </div>
  <div class="column">
    <img src="images/kontrol.png">
    <p>Kontrol Freek</p>
  </div>
  <div class="column">
    <img src="images/astro.jpg">
    <p>Astro Gaming</p>
  </div>
</div>


0
投票

.row {
  display: flex;
}

.column {
  padding: 200px;
  text-align: center;
  flex: 10%;
}

.column img {
  border-radius: 50%;
  width: 300px;
  border: 2px solid white;
}

.column p {
  color: white;
  padding-top: 50px;
  font-size: 24px;
}

.partners-h1 p {
  font-family: "BatmanForeverAlternate";
  font-size-adjust: 10px;
  color: white;
}

.partners-h1 {
  text-align: center;
  font-size: 30px;
}

/* added for demonstration purpose by editor */
body {
  background-color: black;
}
<div class="row">
  <div class="column">
    <img src="images/sneak.jpg">
    <p>Sneak Energy</p>
  </div>
  <div class="column">
    <img src="images/kontrol.png">
    <p>Kontrol Freek</p>
  </div>
  <div class="column">
    <img src="images/astro.jpg">
    <p>Astro Gaming</p>
  </div>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.