【问题标题】:Swapping columns (left / right) on alternate rows在交替行上交换列(左/右)
【发布时间】:2017-02-17 17:37:33
【问题描述】:

我有一系列行,每行包含两列,宽度为 50/50。

我希望每隔一行将左列 (.image) 向右交换,但我需要保持 HTML 中的顺序,因为它在较小的屏幕上显示为一列。

CSS:

ul {
  list-style: none;
  padding-left: 0;
}

.row {
  text-align: center;
}

@media (min-width: 768px) {

  .image,
  .text {
      display: inline-block;
      width: 50%;
      vertical-align: middle;
  }

  /* alternate */
  .row:nth-child(odd) .image {
    float: right;
  }

  /* clearfix */
  .row:before,
  .row:after {
      content: " ";
      display: table;
  }

  .row:after {
      clear: both;
  }

}

HTML:

<ul>
  <li class="row">
    <div class="image">
      <img src="http://placehold.it/350x150">
    </div><div class="text">
      <p>Lorem ipsum</p>
  </div>
  </li>
  <li class="row">
    <div class="image">
      <img src="http://placehold.it/350x150">
    </div><div class="text">
      <p>Lorem ipsum</p>
    </div>
  </li>
  <li class="row">
    <div class="image">
      <img src="http://placehold.it/350x150">
    </div><div class="text">
      <p>Lorem ipsum</p>
    </div>
  </li>
</ul>

我知道我可以用 flexbox 做到这一点,但我不能在这个项目中使用它。并且使用float: left 不允许我垂直对齐元素。我还有哪些其他选择?

注意:我将display: inline-block 用于这些列,因为它们的高度各不相同,我希望它们彼此垂直对齐。

编辑:我已经made a CodePen 使用如上所示的浮动示例。

【问题讨论】:

  • 请显示您的完整代码(包括 CSS)
  • 我已经用 CSS 和 CodePen 更新了它,谢谢。

标签: html css layout grid


【解决方案1】:

您可以使用display:table(可选):nth-child(even)direction 来交换 div 位置: codepen

ul {
  margin: 0;
  padding: 0;
  width: 100%;
}
.row {
  width: 100%;
  display: table;
  table-layout: fixed;
}
.row:nth-child(even) {
  direction: rtl;
}
.row:nth-child(odd) .image {
  text-align: right
}
.image,
.text {
  display: table-cell;
  direction: ltr;
  border: solid;
}
.text {
  background: tomato;
}
img {vertical-align:top;}
@media screen and (max-width: 640px) {
  .row,
  .image,
  .text {
    display: block;
    direction:ltr
  }
  .row:nth-child(odd) .text {
  text-align: right
}
}
<ul>
  <li class="row">
    <div class="image">
      <img src="http://placehold.it/350x150">
    </div>
    <div class="text">
      <p>Lorem ipsum</p>
    </div>
  </li>

  <li class="row">
    <div class="image">
      <img src="http://placehold.it/350x150">
    </div>
    <div class="text">
      <p>Lorem ipsum</p>
    </div>
  </li>
  <li class="row">
    <div class="image">
      <img src="http://placehold.it/350x150">
    </div>
    <div class="text">
      <p>Lorem ipsum</p>
    </div>
  </li>
</ul>

【讨论】:

  • 这非常有效。如果您将vertical-align: middle 添加到.image, .text,它还会使内容垂直居中,这是我无法使用最简单的修复方法做到的:float: right - 感谢您的聪明解决方案!
【解决方案2】:

试试这个:

/* CSS */

.row .image {
   display: table-cell
}
.row .text {
   display: table-cell;
   vertical-align: top;
}
.row:nth-child(even) .image {
   float: right;
}
.row {
   clear: both;
}

这是一个有效的plunker

【讨论】:

  • 似乎是一个很好的答案。但是 OOP 说不能使用 float lef (我不明白为什么),而你正在使用 float right ...
  • 使用浮点数不允许我垂直允许列,即vertical-align: middle 等。您可以在我更新的帖子中使用 CSS 和 CodePen 看到这个问题。我刚刚接受的答案可以解决这个问题。
【解决方案3】:

我不太明白您所说的“垂直对齐”是什么意思。究竟是如何对齐的?

但也许您想使用 vertical-align: middle 将它们垂直对齐,然后,是的,float: left 不是您的朋友。

您说您的列的宽度为 50/50。然后您可以将position: relativeleft: 50%;right: 50%; 规则一起使用。

像这样:

.row {
  display: block;
  position: relative;
}

.row > .image {
  display: inline-block;
  position: relative;
  width: 50%;
  background-color: #eef;
  vertical-align: middle;
}
.row > .text {
  display: inline-block;
  position: relative;
  width: 50%;
  background-color: #fee;
  vertical-align: middle;
}

.row:nth-child(even) > .image {
  left: 50%;
}
.row:nth-child(even) > .text {
  right: 50%;
}

背景色只是为了演示。

这样每个偶数行都会交换列,您仍然可以使用vertical-align 规则。请参阅plunker

【讨论】:

  • 我应该说“对齐中间”来暗示vertical-align: middle,这就是我要找的。我已经用 CSS 和 CodePen 更新了我的原始帖子。
  • @Benji,那我猜到你想要什么了。 :) 你试过我的css或plunker吗?它可以按您的意愿工作。 position: relative 具有左/右 50% 偏移量就可以了。但是@GCyrillus 使用direction 的解决方案我更喜欢,因为它甚至可以应用于可变宽度元素。
  • 是的,我做到了,谢谢!它确实可以满足我的需要,因为我知道宽度。但我发现@GCyrillus 的解决方案非常聪明,我还是选择了它:)
猜你喜欢
  • 2014-10-29
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多