如何使用 CSS 在列表项之间制作垂直线?

新手上路,请多包涵

我指的是这些行: http://prntscr.com/d44uuk 我希望它们具有动态高度 - 所以如果我决定更改 <li> 元素之间的垂直距离 - 它们将自动调整大小。

这是我的小提琴: https ://jsfiddle.net/v6ccgkwo/

 ul {
  list-style: none;
}

ul li {
  margin-bottom: 40px;
}

ul li span {
  border-radius: 50%;
  border: 1px dashed black;
  padding: 5px 10px;
  margin-right: 10px;
}
 <ul>
  <li><span>1</span>Легко устанавливается на сайт</li>
  <li><span>2</span>Следит за поведением посетителей</li>
  <li><span>3</span>Замечает, когда они тянут курсор к крестику, намереваясь уйти</li>
  <li><span>4</span>И показывает всплывающее окно с заманчивым предложением</li>
</ul>

原文由 yaru 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 522
2 个回答

这将适用于动态内容,并且没有使用额外的 DOM。试一下

 ul {
  list-style: none;
}

ul li {
  padding-bottom: 40px;
  position:relative
}

ul li span {
  border-radius: 50%;
  border: 1px dashed black;
  padding: 5px 10px;
  margin-right: 10px;
  background:#fff
}
ul li span:before{
  content:'';
  position:absolute;
  border-left:1px solid ;
  left:14px;
  bottom:0;
  z-index:-1;
  height:100%
}

ul li:last-child span:before{
 content:none;
}
ul li:last-child{
  padding-bottom:0
}
 <ul>
<li><span>1</span>Легко устанавливается на сайт</li>
<li><span>2</span>Следит за поведением посетителей</li>
<li><span>3</span>Замечает, когда они тянут курсор к крестику, намереваясь уйти</li>
<li><span>4</span>И показывает всплывающее окно с заманчивым предложением</li>
  </ul>

原文由 Santhosh Kumar 发布,翻译遵循 CC BY-SA 3.0 许可协议

带有自动递增计数器的改进版本可生成编号

 ul {
  max-width: 400px;
  margin: 0 auto;
  list-style-type: none;
  counter-reset: steps;
  margin: 0;
  font-family: sans-serif;
}
ul li {
  padding: 0 0 20px 50px;
  position: relative;
  margin: 0;
}
ul li:after {
  position: absolute;
  top: 0;
  left: 0;
  content: counter(steps);
  counter-increment: steps;
  border: 2px solid #000;
  border-radius: 50%;
  display: inline-block;
  height: 24px;
  width: 24px;
  text-align: center;
  line-height: 24px;
  background: #fff;
}
ul li:before {
  position: absolute;
  left: 13px;
  top: 0;
  content: "";
  height: 100%;
  width: 0;
  border-left: 2px dashed #000;
}
ul li:last-of-type:before {
  border: none;
}
 <ul>
  <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</li>
  <li>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</li>
  <li>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</li>
  <li>Et harum quidem rerum facilis est et expedita distinctio.</li>
</ul>

原文由 stobasan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题