【问题标题】:clear property not working on nth-child selector清除属性不适用于第 n 个子选择器
【发布时间】:2019-03-16 14:28:21
【问题描述】:

这看起来很简单,但是当我使用 nth-child() 选择器时,clear:both; 无法处理我的 li 标签。

li{
    display: block;
    width:100px;
    height: 100px;
    list-style-type: none;
    margin:1px;
    float: left;
}
li:nth-child(1){
    background:#ff0000;
    clear:both;
}
li:nth-child(even){
    background: #0000ff;
}

但在 li 主标签中使用时效果很好。

<body>
    <li></li>
    <li></li>
</body>

即使我在li 上使用div,代码也不起作用。

【问题讨论】:

  • 这不是所有的 HTML 吗?无效...
  • 其实这是我现在写的所有HTML。我会添加更多,但我发布的所有内容都是我的想法的基础,如果它不起作用,那么我必须知道为什么并找出其他东西
  • 你必须用&lt;ul&gt; 或&lt;ol&gt; 包裹&lt;li&gt;,否则你会得到意想不到的(和不受欢迎的)结果。
  • 那么它也不起作用
  • 不确定你的意思...

标签: css css-selectors


【解决方案1】:

也许是因为它仍然漂浮在左侧。

用float:none删除它

li{
    display: block;
    width:100px;
    height: 100px;
    list-style-type: none;
    margin:1px;
    float: left;
}
li:nth-child(1){
    background:#ff0000;
    clear:both;
  float:none;
}
li:nth-child(even){
    background: #0000ff;
}
<body>
    <li></li>
    <li></li>
</body>

【讨论】:

  • 对不起,我需要浮动它们,只是我计划添加多个 LI 标签,只需要清除第一个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 2011-03-11
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
相关资源
最近更新 更多