简体   繁体   English

如何获得第二行(第三行等)的缩进,以使用CSS使用自定义编号匹配列表中的第一行?

[英]How can I get second (third, etc.) line indentation to match first line in a list with custom numbering using CSS?

I'm new to HTML, CSS and JavaScript, so this may be a very basic problem, however, I haven't been able to find a solution by searching. 我是HTML,CSS和JavaScript的新手,所以这可能是一个非常基本的问题,但是,我一直无法通过搜索找到解决方案。 Most solutions I've found deal with automatically numbered lists, or un-ordered lists. 我发现的大多数解决方案都处理自动编号列表或无序列表。

I'm using Javascript to populate a list on a webpage from a JSON document. 我正在使用Javascript从JSON文档填充网页上的列表。 The output HTML list looks like this: 输出的HTML列表如下所示:

<article>
    <div>
        <ul class='foo'><li class='num'>1.</li><li class='txt'>bar</li></ul>
        <ul class='foo'><li class='num'>2.</li><li class='txt'>foo</li></ul>
        <ul class='foo'><li class='num'>2a.</li><li class='txt'>very long test here so that it moves to a new line on the webpage</li></ul>
        <ul class='foo'><li class='num'>5.</li><li class='txt'>bar</li></ul>
        <ul class='foo'><li class='num'>6.</li><li class='txt'>foo</li></ul>
    </div>
</article>

I then use CSS to style these to look like a single list: 然后,我使用CSS设置样式,使其看起来像一个列表:

.foo li{
    display: inline;
    padding-left: 20px;
}

This shows the generated numbers with the text to the right, however, if the text is long, like in the third ul in the example above, it loses all indentation when it moves down to a new line. 这会在文本右边显示生成的数字,但是,如果文本很长(如上例中的第三个ul),则当向下移动到新行时,它将丢失所有缩进。 Also, if one number is longer than another, the text to the right of it is indented by the same amount extra, but I'd like to have each line of text start in line with the text above and below it. 另外,如果一个数字长于另一个数字,则其右边的文本会缩进相同的数量,但我希望文本的每一行都与该数字上下对齐。

I've tried floating each list left and right, but that wrecked the formatting I had on the page. 我尝试过左右浮动每个列表,但这破坏了页面上的格式。 I'm sure there's some simple way to overcome this, but any search I've made has been related to automatically generated numbers for ol objects. 我敢肯定有一些简单的方法可以解决这个问题,但是我所做的任何搜索都与为ol对象自动生成的数字有关。

How do I keep my own list numbering, but control the consistency of indentation over multiple lines? 如何保留自己的列表编号,但如何控制多行缩进的一致性?

The easiest way to prevent this is to simply give the list element display: table-cell : 防止这种情况的最简单方法是简单地使列表元素display: table-cell

 .foo li { display: table-cell; padding-left: 20px; } 
 <article> <div> <ul class='foo'> <li class='num'>1.</li> <li class='txt'>bar</li> </ul> <ul class='foo'> <li class='num'>2.</li> <li class='txt'>foo</li> </ul> <ul class='foo'> <li class='num'>2a.</li> <li class='txt'>very long test here so that it moves to a new line on the webpage</li> </ul> <ul class='foo'> <li class='num'>5.</li> <li class='txt'>bar</li> </ul> <ul class='foo'> <li class='num'>6.</li> <li class='txt'>foo</li> </ul> </div> </article> 

Alternatively, you could make use of display: flex on the parent and display: inline-flex on the list element: 此外,也可以利用display: flex的家长和display: inline-flex列表元素上:

 .foo { display: flex; } .foo li { display: inline-flex; padding-left: 20px; } 
 <article> <div> <ul class='foo'> <li class='num'>1.</li> <li class='txt'>bar</li> </ul> <ul class='foo'> <li class='num'>2.</li> <li class='txt'>foo</li> </ul> <ul class='foo'> <li class='num'>2a.</li> <li class='txt'>very long test here so that it moves to a new line on the webpage</li> </ul> <ul class='foo'> <li class='num'>5.</li> <li class='txt'>bar</li> </ul> <ul class='foo'> <li class='num'>6.</li> <li class='txt'>foo</li> </ul> </div> </article> 

If you want to get the text 'fully' in line, you'll want to give margin-left: -8px to the element that has the additional letter in the numbering: 如果要使文本“完全” margin-left: -8px ,则需要在编号中带有附加字母的元素margin-left: -8px

 .foo { display: flex; } .foo li { display: inline-flex; padding-left: 20px; } .foo .long { margin-left: -8px; } 
 <article> <div> <ul class='foo'> <li class='num'>1.</li> <li class='txt'>bar</li> </ul> <ul class='foo'> <li class='num'>2.</li> <li class='txt'>foo</li> </ul> <ul class='foo'> <li class='num'>2a.</li> <li class='txt long'>very long test here so that it moves to a new line on the webpage</li> </ul> <ul class='foo'> <li class='num'>5.</li> <li class='txt'>bar</li> </ul> <ul class='foo'> <li class='num'>6.</li> <li class='txt'>foo</li> </ul> </div> </article> 

Having said all that, the 'best' way to handle this would be to only use one <ul> and make use of list-style-type: decimal . 综上所述,处理此问题的“最佳”方法是仅使用一个<ul>并使用list-style-type: decimal This way the indentation lines up, and your numbering is handled for you automatically: 这样缩进排成一行,并自动为您处理编号:

 .foo li { list-style-type: decimal; padding-left: 20px; } 
 <article> <div> <ul class='foo'> <li>bar</li> <li>foo</li> <li>very long test here so that it moves to a new line on the webpage</li> <li>bar</li> <li>foo</li> </ul> </div> </article> 

This can even be extended to have sub-lists to cover the Roman lettering, offset with a negative margin-left : 这甚至可以扩展为包含罗马字母的子列表,并以负的margin-left抵消:

 .foo li { list-style-type: decimal; padding-left: 20px; } .foo li ul li { list-style-type: lower-alpha; margin-left: -40px; } 
 <article> <div> <ul class='foo'> <li>bar</li> <li>foo</li> <li>foo <ul> <li>very long test here so that it moves to a new line on the webpage</li> </ul> </li> <li>bar</li> <li>foo</li> </ul> </div> </article> 

You could render your list as a table: 您可以将列表呈现为表格:

ul.foo {
     display: table;
}
ul.foo > li {
    display: table-cell;
}
ul.foo > li.num {
    min-width: 40px;
}

Demo: http://jsfiddle.net/xbsp30kd/1/ 演示: http//jsfiddle.net/xbsp30kd/1/

ul tag should be used only once it should be something like this ul标签仅应使用如下形式

    <ul>
    <li>Items</li>
    <li>Items</li>
    <li>Items</li>
    </ul>

you kept on repeating your ul tags and for the custom indexing, you could use span tags instead of using another li tag 您一直重复执行ul标签,并且对于自定义索引,可以使用span标签而不是另一个li标签

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取textarea中第一行和第二行的值? - How can I get the value of the first and second line in a textarea? 返回一个数组,该数组包含:一个第一项,两个第二项,树第三项等,使用循环 - Returns an array that consists of: one first item, two second items, tree third items etc. withour using loops 获取列位置(如第一、第二等) - get column position (like first, second, etc.) 如何在段落中添加行号? - How can I add line numbering to my paragraph? 如何在Intellij Idea上的“ if”,“ try”等…块之后获取换行的正确缩进? - How to get correct indentation for new line after “if”,“try”, etc… blocks on Intellij Idea? 量角器-是否可以将$$(elementLocator).first()用于更多元素,例如第二,第三等等。 - Protractor - Can I use $$(elementLocator).first() for more elements like the second, third and etc..? 如何将第三个网格的内容显示到第一个和第二个网格? - How can i show content of third grid to first and second grid? 如何获取变量的内容,直到第一行? - How can I get the contents of a variable up to the first new line? 如何在元素上通过by.css使用子选择器&gt;(或+等)? - How can I use child selector > (or + etc.) with by.css on an element? 在CSS中,我不知道如何在“第一行”或“第一字母”中添加“内容” - in CSS, I can't figure out how to add “content” to “first-line” or “first-letter”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM