有序列表的CSS和子章节

3
我正在尝试创建一个有小节的有序列表,其小节以十进制形式进行CSS排版,如下图所示。
但是到目前为止,我只能得到下面的图片,其中现在有数字,但它们的对齐方式不正确。
以下是我的代码:
<style>
    body{
        width: 500px;
        font-family: helvetica;
        font-size: 12px;
        counter-reset:section;
    }

    OL { counter-reset: item }
    LI { display: block }
    LI:before { content: counters(item, ".") " "; counter-increment: item }
    p{
        display:inline-block;
        width: 400px;
    }

</style>

<ol>
  <li>
    <strong>The Card</strong>
    <ol>
        <li><p>When you receive your Card, you will receive a PUK and you must choose a PIN.</p></li>
        <li><p>You must either memorise the PIN or keep record of it in a safe place, separate from your Card. Do not tell anyone your PUK or PIN.</p></li>
    </ol>
    </li>
</ol>

只是提醒一下:在CSS标记中,没有必要将元素选择器写成大写。 - Jeff Noel
5个回答

2
尝试将您的li样式修改为以下内容:
 OL { margin-left: 0; padding-left: 0; }

请添加以下内容:

 li p { padding-left: 10px; }

1
你可以将padding设置为0。
li p {padding: 0; display: block;}

如果你想更进一步,甚至可以尝试调整一下

list-style-position: outside/inside/inherit

1
在您的CSS规则li:before中添加vertical-align: top;。根据您想要对齐元素的方式,您可能还需要调整<p> margin CSS属性。

0

这就是让我成功的原因

body{
        width: 500px;
        font-family: helvetica;
        font-size: 12px;
        counter-reset:section;
    }

    OL { 
        counter-reset: item; 
        margin: 0px;
        padding: 0px;
    }
    LI { 
        display: block;

    }
    LI:before { 
        content: counters(item, ".") " "; 
        counter-increment: item ;
        vertical-align: top;
    }
    p{
        display:inline-block;
        width: 400px;
        margin-top: 0px;
        margin-left: 50px;
    }
    strong{
        margin: 0px;
        padding: 0px;
        margin-left: 20px;
    }

    ol li ol{
        margin-top: 20px;
        margin-botom: 20px;
    }


</style>

你只需要发布相关的更改,例如,正文是无关紧要的。此外,看起来Skyhawk的答案是正确的方法,所以公平地接受他的答案。 - Christoph

0

这就是你想要的(完全按照你的要求):JSFiddle


CSS

body {
    width: 500px;
    font-family: helvetica;
    font-size: 12px;
    counter-reset:section;
}
ol li ol {
    padding-left:0px;
}
ol li ol li {
    padding-left:20px;
}

HTML

<ol>
    <li> <strong>The Card</strong>

        <ol>
            <li>
                <p>When you receive your Card, you will receive a PUK and you must choose a PIN.</p>
            </li>
            <li>
                <p>You must either memorise the PIN or keep record of it in a safe place, separate from your Card. Do not tell anyone your PUK or PIN.</p>
            </li>
        </ol>
    </li>
</ol>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接