Again: CSS, UL/OL: Incorrect indent with custom counter

旧街凉风 提交于 2019-12-06 08:56:05

I revisited my previous solution and made some modifications to the CSS as follows.

For the top-level list:

ol.custom {
    margin-left: 0;
    padding-left: 0px;
    counter-reset: counter_level1;
    list-style: none outside none;
    display: block;
    line-height: 18px;
    width: 500px;
}
ol.custom li {
    list-style: none;
    margin: 0 0 0 0;
    padding: 0 0 0 20px;
    outline: 1px dotted blue;
    overflow: hidden;
}
ol.custom li:before {
    display: inline-block;
    width: 20px;
    margin-left: -20px;
    content: counter(counter_level1)". ";
    counter-increment: counter_level1;
    font-weight: bold;
}

and for the nested list:

ol.custom ol {
    margin: 0 0 0 0;
    padding: 0 0 0 0;
    counter-reset: counter_level2;
}
ol.custom ol li {
    position: relative;
    margin: 0 0 0 0;
    padding: 0 0 0 40px;
}
ol.custom ol li:before {
    display: inline-block;
    width: 40px;
    margin-left: -40px;
    content: counter(counter_level1, decimal)"." counter(counter_level2, decimal)". ";
    counter-increment: counter_level2;
}

Essentially, I removed the absolutely positioned pseudo-elements since those will not work near floated content.

However, because of the negative-margin for the pseudo-elements, the labels could still overlap the floated images, so add overflow: hidden to the top level li style and this creates a new block formatting context which takes care of the over lap issue.

Downside: depending on your content and the floated image, you can get some large chunks of white space as shown in my new demo:

http://jsfiddle.net/audetwebdesign/buXKy/

I guess from the CSSdesk that you post that you want the indentation to be similar to what shows in the bottom panel.

First, you want the numbers to be off the text. You can get this by setting a margin-left that matches the width:

ol.wrong li:before {
    ....
    width: 20px;
    margin-left: -20px
}

ol.wrong ol li:before {
    width: 40px;
    margin-left: -40px;
}

Since the margin left is the same amount than the width, it doesn't take space.

And, to adjust the global position, set:

ol.wrong ol li {
    margin-left:15px;
}

In your original code, this is margin-right. I would say that this is an error, doesn't seem to make sense to adjust the margin right

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