html的li浮动之后往下移动,UL/LI向下流动,然后向右移动时出现空间

我找到了一种方法......仅使用CSS,使用了第n个子选择器。 (这可能会在旧浏览器上遇到麻烦)。

下面是相关的CSS:

ul {

white-space: nowrap;

margin: 0;

padding: 0;

margin-bottom: 100px; /* space the two demos apart */

}

li {

width: 180px;

height: 30px;

background-color: rgba(0,0,255,.5);

color: white;

padding: 5px;

margin: 0px;

border-radius: 8px;

font-family: Helvetica;

font-size: 24px;

line-height: 30px;

display: inline-block;

}

/******************************************/

/* N = 2 */

/******************************************/

ul.rows2 {

margin-left: 180px; /* li.width * (N-1) */

}

ul.rows2 li {

margin-left: -180px; /* -(li.width * (N-1)) */

}

ul.rows2 li:nth-child(even) {

background-color: rgba(255,0,255,.5);

position: relative;

top: 50px; /* li.height + vertical spacing */

left: -195px; /* -(li.width + horizontal spacing */

margin-left: 0px;

}

/******************************************/

/* N = 3 */

/******************************************/

ul.rows3 {

margin-left: 360px; /* li.width * (N-1) */

}

ul.rows3 li {

margin-left: 0px;

}

ul.rows3 li:nth-child(3n+1) { /* top */

margin-left: -360px; /* -(li.width * (N-1)) */

}

ul.rows3 li:nth-child(3n+2) { /* second */

background-color: rgba(255,0,0,.5);

position: relative;

top: 50px; /* li.height + vertical spacing */

left: -195px; /* -(li.width + horizontal spacing */

}

ul.rows3 li:nth-child(3n+3) { /* bottom */

background-color: rgba(255,0,255,.5);

position: relative;

top: 100px; /* 2*(li.height + vertical spacing */

left: -390px; /* -2*(li.width + horizontal spacing */

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值