【问题标题】:CSS HTML List Item ClassesCSS HTML 列表项类
【发布时间】:2020-12-20 04:03:18
【问题描述】:

我正在尝试创建一个有序的任务列表。随着每项任务的完成,任务变为绿色,线条变为实线。我正在为下面的静态示例寻求帮助。

我已经看到了一些这样做的例子,但我在线条和颜色方面遇到了困难。如果我将 ul li.circle:after 用于圆,将 ul li.circle:before 用于线,我可以将其连接起来,但它们都变成实线,我无法弄清楚如何与虚线互换。我想使用列表项来定义线条和圆圈的颜色。

在 css 中有 padding: 0 0 20px 50px;这就是为什么线条没有按照需要连接到点,但是没有填充,之间没有间距。要更改颜色,我可以使用 before/after 更改所有的颜色,但我不能为颜色添加另一个类,如下所示。

开始的 css 类应该是一条带顶部的线,如字母 T,并且 arrowDown 应该有一个连接到虚线的词干。

CSS 文件和页面布局:

.red {
      background: #ff0000;
    }
    
    .green {
      background: #00ff00;
    }

    .start {
    /* polygon? T like shape*/
    }
    
    ul {
      max-width: 400px;
      margin: 0 auto;
      list-style-type: none;
      margin: 0;
      font-family: sans-serif;
      color: #ffffff;
    }
    
    ul li {
      padding: 0 0 20px 50px;
      position: relative;
      margin: 0;
    }
    
    /* Circle */
    ul li.circle:after {
      position: absolute;
      color: #ccc;
      top: 0;
      left: 0;
      content: '';
      border: 2px solid #fff;
      border-radius: 50%;
      height: 24px;
      width: 24px;
      text-align: center;
      line-height: 24px;
      background: #000000;
    }
    
    /* Circle
    ul li.circle:before {
      position: absolute;
      left: 10px;
      top: 0px;
      content: "";
      height: 100%;
      width: 0;
      border-left: 2px solid #ffffff;
    }
    */
    
    /* Line */
    ul li.line:before {
      position: absolute;
      left: 10px;
      top: 0px;
      content: "";
      height: 100%;
      width: 0;
      border-left: 2px solid #ffffff;
    }
    
    /* dottedLine */
    ul li.dottedLine:before {
      position: absolute;
      left: 10px;
      top: 0px;
      content: "";
      height: 100%;
      width: 0;
      border-left: 2px dotted #ffffff;
    }
    
    /* Missing the stem */
    ul li.arrowDown:after {
      position: absolute;
      transform: rotate(45deg);
      top: 10px;
      left: 4px;
      content: '';
      display: inline-block;
      text-align: center;
      line-height: 24px;
      border: solid #ffffff;
      border-width: 0px 3px 3px 0px;
      display: inline-block;
      padding: 6px;
    }
<li>
<li class="start">Monday</li>
<li class="line"></li>
<li class="circle green">Task 1</li>
<li class="line"></li>
<li class="circle green">Task 2</li>
<li class="dottedLine"></li>
<li class="circle red">Task 3</li>
<li class="dottedLine"></li>
<li class="arrowDown">Tuesday</li>
</ul>

请参阅附件以获取上面所需的输出。 enter image description here

【问题讨论】:

  • 您能否向我们展示您正在努力实现的目标?如果我们能看到您想要做什么,这将对我们有所帮助:)

标签: html css list class html-lists


【解决方案1】:

首先,您的开头ulli

然后,您需要为 .line.dottedline 类添加高度,如您所愿。

ul li.line, ul li.dottedLine {
  height: 20px
}

让你为 ul li 填充 padding: 0 0 10px 50px;

在你开始上课之前使用伪类

为您圈出背景颜色,将颜色添加到ul li.circle:after,然后将其覆盖为ul li.red:after

这是调整后的代码

body {
     background: black;
}

.red {
  /* Not needed add color to pseudo element
   * background: #ff0000;*/
}

.green {
  /* Not needed add color to pseudo element
   * background: #00ff00;*/
}

.start {
/* See below: ul li.start:before */
}

ul {
  max-width: 400px;
  margin: 0 auto;
  list-style-type: none;
  margin: 0;
  font-family: sans-serif;
  color: #ffffff;
}
ul li {
  padding: 0 0 10px 50px;
  position: relative;
  margin: 0;
}

/* Circle */
ul li.circle:after {
  position: absolute;
  color: #ccc;
  top: 0;
  left: 0;
  content: '';
  border: 2px solid #fff;
  border-radius: 50%;
  height: 24px;
  width: 24px;
  text-align: center;
  background: #00ff00;
}
ul li.red:after {
  background: #ff0000;
}

/* Line */
ul li.line {
  list-style-type:none;
}
ul li.line, ul li.dottedLine {
  height: 20px;
}

/* make the first line which is 2nd child 0 */
ul li:nth-child(2) {
  height: 10px;
}

ul li.line:before {
  position: absolute;
  left: 13px;
  top: 0px;
  content: "";
  height: 100%;
  width: 0;
  border-left: 2px solid #ffffff;
}

/* dottedLine */
ul li.dottedLine:before {
  position: absolute;
  left: 13px;
  top: 0px;
  content: "";
  height: 100%;
  width: 0;
  border-left: 2px dotted #ffffff;
}

/* Start T top added */
ul li.start:before {
  position: absolute;
  content: "";
  width: 26px;
  left: 0;
  top: 24%;
  border-top: 2px solid #ffffff;
  background: white;
}

/* Missing the stem 
 * stem added with pseudo class               
 * start:after slightly modified below
 */
ul li.start:after, ul li.arrowDown:before {
  position: absolute;
  left: 13px;
  top: 0px;
  content: "";
  height: 90%;
  width: 0;
  border-left: 2px solid #ffffff;
}   
ul li.start:after {
  top: 24%;
  height: 78%;
}
ul li.arrowDown:after {
  position: absolute;
  transform: rotate(45deg);
  top: 10px;
  left: 7px;
  content: '';
  display: inline-block;
  text-align: center;
  line-height: 24px;
  border: solid #ffffff;
  border-width: 0px 3px 3px 0px;
  display: inline-block;
  padding: 6px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<ul>
<li class="start">Monday</li>
<li class="line"></li>
<li class="circle green">Task 1</li>
<li class="line"></li>
<li class="circle green">Task 2</li>
<li class="dottedLine"></li>
<li class="circle red">Task 3</li>
<li class="dottedLine"></li>
<li class="arrowDown">Tuesday</li>
</ul>
</body>
</html>

【讨论】:

  • 感谢您为 Stack Overflow 做出贡献。但是请注意,问题和答案应该是独立的,而不是依赖于外部链接,因为它们会随着时间的推移而改变或中断。只要您在答案本身中包含代码,就可以包含它们。您可以使用编辑器中的[&lt;&gt;] 按钮将代码添加到Stack Snippet
  • 感谢@FluffyKitten 我将代码添加为堆栈片段
  • 谢谢。稍加调整,我就可以让它做我需要做的事情。
  • @mike 是的,再做一些调整会很好,将文本与圆心对齐可能会很好。很高兴能帮上忙。
  • 也许您可以提出一个新问题?似乎与原著无关。您说标记必须保持原样,或者您不能添加新类?你说“但我不能添加其他课程”请解释一下。
猜你喜欢
  • 2011-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多