erj*_*ang 162 html css html-lists
我想控制一个子弹<li>
在一个<ol>
或多个中向右推动多少水平空间<ul>
.
也就是说,而不是总是拥有
* Some list text goes
here.
Run Code Online (Sandbox Code Playgroud)
我希望能够改变它
* Some list text goes
here.
Run Code Online (Sandbox Code Playgroud)
要么
*Some list text goes
here.
Run Code Online (Sandbox Code Playgroud)
我环顾四周,但只能找到左右移动整个区块的说明,例如,http://www.alistapart.com/articles/taminglists/
Bal*_*usC 145
将其内容放在span
相对定位的位置,然后您可以通过left
属性来控制空间span
.
li span {
position: relative;
left: -10px;
}
Run Code Online (Sandbox Code Playgroud)
Joe*_*oel 114
要总结其他答案 - 如果您希望更好地控制<li>
项目符号之间的空间和列表项中的文本,您的选项是:
(1)使用背景图片:
<style type="text/css">
li {
list-style-type:none;
background-image:url(bullet.png);
}
</style>
<ul>
<li>Some text</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
好处:
background-position
将图像放置在与文本相关的任何位置,使用像素,ems或%缺点:
2.在<li>
标签上使用填充
<style type="text/css">
ul {padding-left:1em}
li {padding-left:1em}
</style>
<ul>
<li>Some text</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
好处:
<li>
,您可以根据需要在项目符号和文本之间添加额外的水平空间缺点:
(3)将文本包装在一个额外的<span>
元素中
<style type="text/css">
li {
padding-left:1em;
color:#f00; /* red bullet */
}
li span {
display:block;
margin-left:-0.5em;
color:#000; /* black text */
}
</style>
<ul>
<li><span>Some text</span></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
好处:
padding-top
来改变垂直位置<span>
.其他人可能有解决这个问题的方法虽然......)缺点:
这里希望CSS4中有一些新的列表式功能,所以我们可以创建更智能的项目符号而无需借助图像或exta标记:)
Kev*_*vin 32
这应该做到这一点.务必将子弹设置到外面.你也可以使用CSS伪元素,如果你可以在IE7中向下删除它们.我真的不建议在这种事情上使用伪元素,但它确实可以控制距离.
ul {
list-style: circle outside;
width: 100px;
}
li {
padding-left: 40px;
}
.pseudo,
.pseudo ul {
list-style: none;
}
.pseudo li {
position: relative;
}
/* use ISO 10646 for content http://la.remifa.so/unicode/named-entities.html */
.pseudo li:before {
content: '\2192';
position: absolute;
left: 0;
}
Run Code Online (Sandbox Code Playgroud)
<ul>
<li>Any Browser really</li>
<li>List item
<ul>
<li>List item</li>
<li>List item</li>
</ul>
</li>
</ul>
<ul class="pseudo">
<li>IE8+ only</li>
<li>List item
<ul>
<li>List item with two lines of text for the example.</li>
<li>List item</li>
</ul>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
Dav*_*ann 13
老问题,但是:之前伪元素在这里工作得很好.
<style>
li:before {
content: "";
display: inline-block;
height: 1rem; // or px or em or whatever
width: .5rem; // or whatever space you want
}
</style>
Run Code Online (Sandbox Code Playgroud)
它工作得很好,不需要很多额外的规则或HTML.
<ul>
<li>Some content</li>
<li>Some other content</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
干杯!
小智 12
如果您只想减少项目符号和文本之间的间距,似乎有一个更清晰的解决方案:
li:before {
content: "";
margin-left: -0.5rem;
}
Run Code Online (Sandbox Code Playgroud)
小智 10
对于list-style-type:inline:
它与DSMann8的答案几乎相同,但css代码较少.
你只需要
<style>
li:before {
content: "";
padding-left: 10px;
}
</style>
<ul>
<li>Some content</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
干杯
您可以padding-left
在列表项上使用该属性(不在列表本身上!).
小智 7
在li上使用text-indent效果最好.
text-indent:-x px; 将子弹移近李,反之亦然.
使用相对于跨度,负向左可能无法与IE的旧版本一起正常工作.PS-尽量避免给出位置.
这是使用css计数器和伪元素的另一种解决方案.我发现它更优雅,因为它不需要使用额外的html标记或css类:
ol,
ul {
list-style-position: inside;
}
li {
list-style-type: none;
}
ol {
counter-reset: ol; //reset the counter for every new ol
}
ul li:before {
content: '\2022 \00a0 \00a0 \00a0'; //bullet unicode followed by 3 non breakable spaces
}
ol li:before {
counter-increment: ol;
content: counter(ol) '.\00a0 \00a0 \00a0'; //css counter followed by a dot and 3 non breakable spaces
}
Run Code Online (Sandbox Code Playgroud)
我使用不可破坏的空格,以便间距仅影响我的列表元素的第一行(如果列表元素长度超过一行).你可以在这里使用填充.
小智 6
无序列表以ul标记开头.每个列表项都以列表项开头.默认情况下,列表项将标有项目符号(小黑圈):
<!DOCTYPE html>
<html>
<body>
<h2>Normal List </h2>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
输出:
<!DOCTYPE html>
<html>
<body>
<h2>Normal List </h2>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
text-indent属性指定文本块中第一行的缩进.
注意:允许使用负值.如果值为负,则第一行将缩进到左侧.
<ul style='text-indent: -7px;'>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
小智 5
似乎您可以(在某种程度上)使用 <li> 标签上的填充来控制间距。
<style type="text/css">
li { padding-left: 10px; }
</style>
Run Code Online (Sandbox Code Playgroud)
问题是它似乎不允许您像最后一个示例一样紧贴它。
为此,您可以尝试关闭列表样式类型并使用 •
<ul style="list-style-type: none;">
<li>•Some list text goes here.</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
旧问题,但仍然有意义。我建议使用负数text-indent
。
list-style-position
必须是outside
。
优点:
缺点:
小智 5
当您想要将文本移近项目符号时,即使您有多行文本,以下解决方案也很有效。
margin-right
允许您将文本移近项目符号
text-indent
确保多行文本仍然正确排列
li:before {
content: "";
margin-right: -5px; /* Adjust this to move text closer to the bullet */
}
li {
text-indent: 5px; /* Aligns second line of text */
}
Run Code Online (Sandbox Code Playgroud)
<ul>
<li> Item 1 ... </li>
<li> Item 2 ... this item has tons and tons of text that causes a second line! Notice how even the second line is lined up with the first!</li>
<li> Item 3 ... </li>
</ul>
Run Code Online (Sandbox Code Playgroud)
它允许将空间设置得更小。
\nul > li::marker {\n content: \'\xe2\x80\xa2\'; /* as a side effect sets the space to zero */\n font: 1.2em/1 sans-serif; /* adjust the bullet size */\n}\nul > li {\n padding-left: .2em; /* have full control over the bullet space */\n}
Run Code Online (Sandbox Code Playgroud)\r\n<ul>\n <li>one\n <li>two\n <li>three\n</ul>
Run Code Online (Sandbox Code Playgroud)\r\n