html - 如何正确标记/设置有序列表的样式以补偿大元素编号

标签 html css semantic-markup

我正在动态生成一个包含有序列表的网页。该页面包含 10 个元素,并使用属性“开始”对元素进行相应编号。具有“list-style-position: outside”的通用有序列表非常适合元素编号小于 1000 的元素。

当元素编号长度为 4 位或更多位时,部分元素编号被容器边框部分遮挡时会出现问题。调整列表填充并不是一个真正的解决方案,因为它仍然会破坏位数大于调整填充以处理的位数的元素计数,并且由于过多的填充会使个位数元素看起来很糟糕。

使用 'list-style-position: inside' 解决了元素编号被遮挡的问题,但引入了一个新问题,因为这样做会导致元素内容包裹在列表元素编号下方,而不是对齐到数。

我总是可以隐藏元素编号并在每个

  • 中引入一个新的 float div 并将内容设置为列表元素编号,但是虽然这解决了我的演示问题,但在语义上它不太正确,因为我正在添加标记和内容纯粹出于展示原因。

    有没有我不知道的解决这个难题的 css 解决方案?

        <style>
            #container {
                border: solid;
            }
    
            #container div, #container h1 {
                border: solid 1px blue;
            }
    
            #outsideOl {
                list-style-position: outside;
            }
    
            #insideOl {
                list-style-position: inside;
                padding-left: 0;
            }
    
        </style>
    
    
    
        <div id="container">
    
        <ol id="outsideOl" start="3000">
            <li><h1>one</h1><div>the content inside the &lt;li&gt; is aligned to the right of the numbers, which is what I want, but long numbers are obscured by the container's border.  The list elements are shifted to the right by the default padding for an &lt;ol&gt; element.  If the list item number happens to be longer than the padding, part of the number is obscured.</div></li>
            <li>two</li>
            <li>three</li>
            <li>four</li>
            <li>five</li>
            <li>six</li>
            <li>seven</li>
        </ol>
    
        <hr/>
    
        <ol id="insideOl" start="3007">
            <li><h1>one</h1><div>long numbers affect content flow and as such are left aligned to the container border (which is good because they are no longer obscured). The problmen now is the block content inside the li gets bumped down to the next line, and left aligns with the numbers. Curse you css!</div></li>
            <li>two</li>
            <li>three</li>
            <li>four</li>
            <li>five</li>
            <li>six</li>
            <li>seven</li>
        </ol>
    
        </div>
    

  • 最佳答案

    您想要的行为是什么?您可以设置溢出的可见性,以便非常大的数字突出到边距中。如果您希望数字列的宽度实际根据列表项的数量而改变,您将需要使用 JS。很遗憾,但它应该只有一行(类似于 $('ul').each(function(el, index){el.css('padding-left',(Math.floor(Math.log (el.childNodes.length)/Math.LN10)+1)+'em');});)

    关于html - 如何正确标记/设置有序列表的样式以补偿大元素编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3442856/

    相关文章:

    javascript - 数据未显示在下拉列表中

    javascript - 当用户在 chosen select 中选择一个或多个选项时,是否可以使用不同的颜色?

    javascript - 如何使用 NVDA 屏幕阅读器读取工具提示?

    html - 在固定的 div 中水平和垂直居中不同大小的图像

    css - 推特 Bootstrap css 中的中心行元素

    javascript - 有没有正确的方法来制作响应式文本?

    html - 语义旁注的 HTML 元素是什么?

    html - 为什么我的 span 元素在 IE9 中看起来很糟糕?

    css - 元标记 "viewport"不适用于外部主机

    css - 语义标记和语义网有什么区别?