css - 将 3 个列表中的 2 个列表向右移动/ float ,但保持标记顺序

标签 css html css-float html-lists

我有三个内联 block 列表。 最后两个我想将它们移到屏幕右侧但保持标记顺序 我用和不用 clearfix 漂浮它们

问题是我无法保留他们的标记顺序,因为第三个列表出现在第二个列表之前。

我做错了什么? :/

ul {
  display: inline-block; 
}

ul:nth-child(2),
ul:nth-child(3) {
  color: red;
  float: right
}
<ul>
  <li>1</li>
   <li>2</li>
</ul>

<ul>
  <li>3</li>
    <li>4</li>
</ul>

<ul>
  <li>5</li>
  <li>6</li>
</ul>

最佳答案

使用 float:right 会将流程更改为从右侧开始,因此您得到的结果是合乎逻辑的(DOM 中的第一个元素将是右侧的第一个元素)。

除此之外,您可以对第一个使用左浮动并调整 text-align 以移动其他:

body {
  text-align:right;
}

ul {
  display: inline-block; 
}

ul:nth-child(2),
ul:nth-child(3) {
  color: red;
}
ul:nth-child(1) {
  float:left;
}
<ul>
  <li>1</li>
   <li>2</li>
</ul>

<ul>
  <li>3</li>
    <li>4</li>
</ul>

<ul>
  <li>5</li>
  <li>6</li>
</ul>

为了更简洁和现代的方式,您可以使用 flexbox:

body {
  display:flex;
}

ul:nth-child(2),
ul:nth-child(3) {
  color: red;
}
ul:nth-child(1) {
  margin-right:auto;
}
<ul>
  <li>1</li>
   <li>2</li>
</ul>

<ul>
  <li>3</li>
    <li>4</li>
</ul>

<ul>
  <li>5</li>
  <li>6</li>
</ul>

关于css - 将 3 个列表中的 2 个列表向右移动/ float ,但保持标记顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51580184/

相关文章:

javascript - TinyMCE - 图像大小属性问题 IE10 - Twitter Bootstrap

javascript - Pagespeed - 延迟加载幻灯片图像

Javascript 倒数计时器无法正常工作

css - 当第 2 列不存在时,将第 1 列扩展到最大宽度

html - 文本、div 和两个冒烟的花车

html - 在 CSS 选择器中使用标签名称可以吗?

javascript - ie 上的 div 为空白但不是空白

php - 在 Wordpress 页面上设置单独元素的样式

html - firefox textarea 占位符填充

html - 如何避免在 CSS 中换行文本