html - 清除 li 元素中的 float 问题

标签 html css

尝试在 HTML 和 CSS 中重现以下内容:

enter image description here

以下是我尝试构建它的方式:

.layout ul > li {
  list-style: none;
}

.layout ul > li:nth-child(1n) > img {
    float: left;
}

.layout ul > li:nth-child(2n) > img {
    float: right;
    z-index: 9999;
}
<div class="layout">
  <ul>
    <li><img src="http://placeimg.com/120/80/grayscale" /></li>
    <li><img src="http://placeimg.com/120/80/grayscale" /></li>
    <li><img src="http://placeimg.com/120/80/grayscale" /></li>
    <li><img src="http://placeimg.com/120/80/grayscale" /></li>
  </ul>
</div>

问题是在应用 clearfix 时它根本没有帮助。这些图像似乎排成一排。也许在 li 元素和 clearfix 方面有什么特别之处?如何解决?

最佳答案

您可以简单地考虑 text-align 属性和重叠的一些负边距(这将避免所有 float 问题)

.layout {
  width: 200px;
}

.layout ul {
  margin: 0;
  padding: 0;
}

.layout ul>li {
  list-style: none;
  margin-bottom: -20px;
  position: relative; /* don't forget this to be able to use z-index */
}

.layout ul>li {
  text-align: left;
}

.layout ul>li:nth-child(2n) {
  text-align: right;
  z-index: 999; 
}
<div class="layout">
  <ul>
    <li><img src="http://placeimg.com/120/80/grayscale" /></li>
    <li><img src="http://placeimg.com/120/80/grayscale" /></li>
    <li><img src="http://placeimg.com/120/80/grayscale" /></li>
    <li><img src="http://placeimg.com/120/80/grayscale" /></li>
  </ul>
</div>

关于html - 清除 li 元素中的 float 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48224548/

相关文章:

javascript - 在AngularJS中,如何将span中的文本 "bind"发送到文本框,并在单击span时更新它?

javascript - 覆盖对话框的问题

jquery 显示 :block not working correclty when more than 2 divs

html - css代码中的语法错误

html - 滑动菜单 : when check box is checked it moves to the right but the menu does not follow

html - 如何在我的网站中实现预加载动画?

html - 删除 iframe 下方的空白

jquery - 移动设备上的 Wordpress 下拉故障

javascript - 如何在浏览器上使用 TailwindCSS 强制暗模式?

html - 是否可以从CSS延迟加载背景图像(无需jQuery)?