Align list items with custom CSS content before bullets

假装没事ソ 提交于 2019-12-10 22:06:46

问题


I have this snippet of HTML:

<ul class="custom">
    <li data-bullet="a">Item 1</li>
    <li data-bullet="aa">Item 2</li>
    <li data-bullet="ab">Item 3.1<br/>Item 3.2</li>
    <li data-bullet="c">Item 4</li>
    <li data-bullet="d">Item 5</li>
    <li data-bullet="de">Item 6</li>
</ul>

and CSS:

.custom {
    list-style:none;
    padding:0;
    margin:0;
}
.custom li:before {
    display:inline-block;
    content:attr(data-bullet);
    width:50px;
}

/*
.custom {
    list-style:none;
    padding:0;
    margin:0;
}
.custom li:before {
    display:inline-block;
    content:attr(data-bullet);
    position:relative;
    top:0;
    left:-50px;
}
.custom li {
    padding-left:50px;
}
*/

http://jsfiddle.net/g0w989aa/1/

It uses CSS before content of list items in order to create a custom list numbering. The problem is that the alignment of the text doesn't work properly if the content of the list item is longer than a single line.

Is there a way to align the content of the lines properly? I already tried to position the content relative and giving the list items a padding (see commented out css in example) but this doesn't work either.


回答1:


Try offsetting the margin on the entire div by the width of the before and then take the margin off on the before after.

Like this:

.custom {
  list-style: none;
  padding: 0;
  margin: 0;
  margin-left: 50px;
}
.custom li:before {
  display: inline-block;
  content: attr(data-bullet);
  width: 50px;
  margin-left: -50px;
}
<ul class="custom">
  <li data-bullet="a">Item 1</li>
  <li data-bullet="aa">Item 2</li>
  <li data-bullet="ab">Item 3.1
    <br/>Item 3.2</li>
  <li data-bullet="c">Item 4</li>
  <li data-bullet="d">Item 5</li>
  <li data-bullet="de">Item 6</li>
</ul>

You can alternatively do it with positioning but you may hit snags with other elements being affected.




回答2:


Hope this is what you are looking for.

http://jsfiddle.net/xcfc63sL/

CSS

.custom {
    list-style: none;
    padding: 0;
    margin: 0;
}
.custom li {
    position: relative;
    padding-left: 50px;
}
.custom li:before {
    display: inline-block;
    content: attr(data-bullet);
    width: 50px;
    position: absolute;
    left: 0;
}

HTML

<ul class="custom">
    <li data-bullet="a">Item 1</li>
    <li data-bullet="aa">Item 2</li>
    <li data-bullet="ab">Item 3.1
        <br/>Item 3.2</li>
    <li data-bullet="c">Item 4</li>
    <li data-bullet="d">Item 5</li>
    <li data-bullet="de">Item 6</li>
</ul>



回答3:


You can remove the width from your li elements before and use padding-left and position: relative for your li and make your :before contents to be position:absolute as follows.

.custom {
    list-style: none;
    padding: 0;
    margin: 0;
}
.custom li {
    position: relative;
    padding-left: 50px;
}
.custom li:before {
    display: inline-block;
    content: attr(data-bullet);
    position: absolute;
    left: 0;
}

Working Fiddle



来源:https://stackoverflow.com/questions/27246689/align-list-items-with-custom-css-content-before-bullets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!