【问题标题】:Responsive, 3 colums in deskview and 2 in mobile veiw响应式,桌面视图中 3 列,移动视图中 2 列
【发布时间】:2015-02-24 01:35:00
【问题描述】:

当用户有手机时,如何将 3 列缩放为两列。我可以了解很多有关媒体查询的信息,但我不知道该怎么做。像这样:

http://postimg.org/image/sox89ekqj/

【问题讨论】:

  • 请不要尝试将 stackoverflow 用于您自己的业务。请删除广告。

标签: html css responsive-design media-queries


【解决方案1】:

我将给你举两个例子来说明如何做到这一点,因为有很多方法可以实现你想要的。

使用display:tabledisplay:table-cell

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0
}
section {
  display: table;
  width: 100%
}
div {
  display: table-cell;
  border: 1px solid red;
  padding: 5px;
  width: 33%;
  height: 200px
}

@media(max-width:480px){
div {
  width: 50%
}
div:last-child {
  display: none
}
<section>
  <div></div>
  <div></div>
  <div></div>
</section>

使用display:inline-block

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0
}
section {
  font-size:0
}
div {
  display: inline-block;
  border: 1px solid red;
  padding: 5px;
  width: 33.3%;
  height: 200px
}

@media(max-width:480px){
div {
  width: 50%
}
div:last-child {
  display: none
}
<section>
  <div></div>
  <div></div>
  <div></div>
</section>

编辑

由于 OP 的评论,margins 的可能解决方案。

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0
}
section {
  border: 2px solid green;
  height: 204px;
}
div {
  float: left;
  background-color: red;
  margin-right: 2%;
  width: 32%;
  height: 200px
}
div:last-child {
  margin-right: 0
}
@media(max-width:480px){

div {
  width: 49%
}
div:nth-child(2) {
  margin-right: 0;
}
div:last-child {
  display: none;
}
<section>
  <div></div>
  <div></div>
  <div></div>
</section>

【讨论】:

  • 感谢您的好回答。您能否更新它并告诉我们在使用此方法时如何在 div 之间添加一些边距?通常,您将左侧浮动到左侧,将右侧浮动到右侧。但是,如果我先有 3 个列然后是 3 个,该怎么做呢?非常感谢。 @dippas
  • 非常感谢。请为这个问题投票,让更多人找到你的好答案! @dippas
【解决方案2】:

使用 Twitter Bootstrap,这就是它的完成方式

使用col-**-4 为桌面版定义3 列的部分,然后为移动版使用col-**-6。当总列数超过 12 列时,它会将第 12 列上的所有内容换行。

【讨论】:

  • 但是我想自己做。对不起老兄
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
相关资源
最近更新 更多