自定义缩进

时间:2018-10-17 04:47:49

标签: html css

我发现了一些与此问题相关的主题,但没有一个对我有帮助。

如何确定li的第二行与第一行一样尊重缩进? enter image description here

ol {
  list-style: none;
  padding: 0;
  margin: 0;
  counter-reset: my-awesome-counter;
  columns: 2;
  -webkit-columns: 2;
  -moz-columns: 2;
}

ol li {
  counter-increment: my-awesome-counter;
  margin: 1rem 0.25rem;
  line-height: normal;
}


ol li:before {
  display: inline-block;
  content: counter(my-awesome-counter);
  width: 40px;
  height: 40px;
  line-height: 24px;
  background: #000;
  border-radius: 25px;
  color: white;
  text-align: center;
  margin-right: 0.5rem;
  font-size: 24px;
  font-weight: 700;
 }

我想念什么?

5 个答案:

答案 0 :(得分:2)

尝试将position:absolute;用于伪元素,并将padding-left赋予<li>

查看更新的代码段:

ol {
  list-style: none;
  padding: 0;
  margin: 0;
  counter-reset: my-awesome-counter;
  /*columns: 2;
  -webkit-columns: 2;
  -moz-columns: 2;
  */
  width: 300px;
}

ol li {
  counter-increment: my-awesome-counter;
  margin: 1rem 0.25rem;
  line-height: normal;
  padding-left: 60px;
  position: relative;
}

ol li:before {
  display: inline-block;
  content: counter(my-awesome-counter);
  width: 44px;
  height: 40px;
  line-height: 24px;
  background: #000;
  border-radius: 25px;
  color: white;
  text-align: center;
  margin-right: 0.5rem;
  font-size: 24px;
  font-weight: 700;
  position: absolute;
  left: 0;
}
<ol>
  <li> test test test testtest test test testtest test test testtest test test testtest test test testtest test test test</li>
  <li> test test test testtest test test testtest test test testtest test test testtest test test testtest test test test</li>
</ol>

答案 1 :(得分:1)

尝试为伪元素赋予负的边距,并为li赋予左填充,如代码所示:

ol {
  list-style: none;
  padding: 0;
  margin: 0;
  counter-reset: my-awesome-counter;
  columns: 2;
  -webkit-columns: 2;
  -moz-columns: 2;
}

ol li {
  counter-increment: my-awesome-counter;
  margin: 1rem 0.25rem;
  line-height: normal;
  padding-left: 3rem;
}

ol li:before {
  display: inline-block;
  content: counter(my-awesome-counter);
  width: 40px;
  height: 40px;
  line-height: 24px;
  background: #000;
  border-radius: 25px;
  color: white;
  text-align: center;
  margin-right: 0.5rem;
  font-size: 24px;
  font-weight: 700;
  margin-left: -3rem;
}
<ol>
  <li>abv asfbakjh dslsjdgljs lsgljgklj ljsdgll lslgklsdljlsdjgjksd lljljkj hghjgjhgjhgjhjg gjhghjsdf . dgdsg dfg fgsdfsg fg dsfgsf sdfg</li>
  <li>abv asfbakjh dslsjdgljs lsgljgklj ljsdgll lslgklsdljlsdjgjksd lljljkj hghjgjhgjhgjhjg gjhghjsdf . dgdsg dfg fgsdfsg fg dsfgsf sdfg</li>
  <li>abv asfbakjh dslsjdgljs lsgljgklj ljsdgll lslgklsdljlsdjgjksd lljljkj hghjgjhgjhgjhjg gjhghjsdf . dgdsg dfg fgsdfsg fg dsfgsf sdfg</li>

</ol>

答案 2 :(得分:1)

您缺少的是您在pseudo元素中添加了counter,但是您期望像ol li的默认样式一样,尽管您通过添加ol重设了list-style: none;的默认行为。要按预期工作,请按照以下方式操作:

ol {
    list-style: none;
    padding: 0;
    margin: 0;
    counter-reset: my-awesome-counter;
    columns: 2;
    -webkit-columns: 2;
    -moz-columns: 2;
}
ol li {
    counter-increment: my-awesome-counter;
    margin: 1rem 0.25rem;
    line-height: normal;
    position:relative;
    padding-left:45px;
    min-height:40px;
}
ol li:before {
    display: inline-block;
    content: counter(my-awesome-counter);
    width: 40px;
    height: 40px;
    line-height: 36px;
    background: #000;
    border-radius: 25px;
    color: white;
    text-align: center;
    margin-right: 0.5rem;
    font-size: 24px;
    font-weight: 700;
    position:absolute;
    left:0;
    top:0;
}

答案 3 :(得分:1)

尝试绝对赋予前一个位置,以使其出现在列表顶部

我在这里附加了两个示例:

第一个是答案。

第二个原因是我们应该给予绝对职位。

<!DOCTYPE html>
<html>
<head>
<style>
ol{width:250px;}
ol.a{
  list-style-position: outside;
  margin: 0;
  counter-reset: my-awesome-counter;
  display:block;
  position: relative;
  list-style-type:none;
  }
ol.a li {
  counter-increment: my-awesome-counter;
  display:block;
  padding-left: 10px;
  margin-bottom: 10px;
}

ol.a li:before {
  display: inline-block;
  content: counter(my-awesome-counter);
  width: 40px;
  height: 40px;
  line-height: 24px;
  background: #000;
  border-radius: 25px;
  color: white;
  text-align: center;
  margin-right: 0.5rem;
  font-size: 24px;
  font-weight: 700;
  position:absolute;
  left: 0;
 }
 ol.b li:before{
     display: inline-block;
  content: counter(my-awesome-counter);
  width: 40px;
  height: 40px;
  line-height: 24px;
  background: #000;
  border-radius: 25px;
  color: white;
  text-align: center;
  margin-right: 0.5rem;
  font-size: 24px;
  font-weight: 700;
  }



</style>
</head>
<body>
 <h2>With `postion:absolute`</h2>
<ol class="a">
  <li>Coffee - A brewed drink prepared from roasted coffee beans, which are the seeds of berries from the Coffea plant</li>
   <li>Coffee - A brewed drink prepared from roasted coffee beans, which are the seeds of berries from the Coffea plant</li>
   <li>Coffee - A brewed drink prepared from roasted coffee beans, which are the seeds of berries from the Coffea plant</li>
</ol>
 <br><br>
<h2>Without `position:absolute`</h2>
   <ol class="b">
  <li>Coffee - A brewed drink prepared from roasted coffee beans, which are the seeds of berries from the Coffea plant</li>
   <li>Coffee - A brewed drink prepared from roasted coffee beans, which are the seeds of berries from the Coffea plant</li>
   <li>Coffee - A brewed drink prepared from roasted coffee beans, which are the seeds of berries from the Coffea plant</li>
</ol>

</body>
</html>

当我们不定位之前的位置时,就会发生这种情况。

答案 4 :(得分:0)

尝试将display: flex赋予ol li

赞:

ol li {
  counter-increment: my-awesome-counter;
  margin: 1rem 0.25rem;
  line-height: normal;
  display: flex;
}

在这里工作的演示: https://codepen.io/NightShade1993/pen/ReQWOb