- 中的元素这是一个常规字符,而不是图像
我意识到可以使用 CSS 属性指定一个自定义图形作为替换项目符号字符:
list-style-image
然后给它一个 URL。
但是,就我而言,我只想使用“+”符号。我不想为此创建一个图形然后指向它。我宁愿只是指示无序列表使用加号作为项目符号。
这可以完成还是我必须先将其制作成图形?
I realize one can specify a custom graphic to be a replacement bullet character, using CSS attribute:
list-style-image
And then giving it a URL.
However, in my case, I just want to use the '+' symbol. I don't want to have to create a graphic for that and then point to it. I'd rather just instruct the unordered list to use a plus symbol as the bullet symbol.
Can this be done or am I forced to make it a graphic first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
您可以使用 ::marker 伪-元素。当您需要为每个列表项使用不同的字符实体时,这非常有用。
正如 @Kal 提到的,编辑时 Safari 不支持
::marker
上的内容。You can make use of ::marker pseudo-element. This is useful in situations when you need to have different character entities for each list item.
As @Kal mentions, content on
::marker
is not supported in Safari at the time of edit.破折号样式:
Em dash style:
我更喜欢使用负边距,给你更多的控制权
I prefer to use negative margin, gives you more control
有趣的是,我不认为任何已发布的解决方案都足够好,因为它们依赖于所使用的字符为 1em 宽的事实,而这并不需要如此(John Magnolia 的答案可能是个例外,但它使用的浮点数可以另一种方式使事情变得复杂)。这是我的尝试:
与其他解决方案相比,它具有以下优点:
text-align: center;
替换为您喜欢的任何内容即可。例如你可以尝试<代码>
颜色: 红色;
文本对齐:右对齐;
右内边距:5px;
框大小:边框框;
或者,如果您不喜欢使用边框框,只需从宽度中减去填充(5px)(即本例中的宽度:25px)。有很多选择。
享受。
Interestingly enough I do not thing any of the posted solutions are good enough, because they rely on the fact that the character used is 1em wide, which does not need to be so (with maybe exception of John Magnolia's answer which however uses floats which can complicate things another way). Here is my attempt:
This has these advantages (over other solutions):
text-align: center;
by anything to your liking. For example you may trycolor: red;
text-align: right;
padding-right: 5px;
box-sizing: border-box;
or if you do not like playing with border-box, just subtract the padding (5px) from width (i.e. width:25px in this example). There are lots of options.
Enjoy.
首先,感谢@Jpsy,因为我的回答就是基于此。
我已经扩展了该答案以支持 Google Material Icons 包作为自定义图标。
First, thanks to @Jpsy as my answer is based on that.
I have extended that answer to support the Google Material Icons pack as the custom icon.
在 Bootstrap 中,您可以使用 class
list-unstyled
。然后,您可以使用 HTML emoji 或其他内容来代替项目符号:In Bootstrap you can use class
list-unstyled
. Then, instead bullet you can use for example HTML emoji or something else:这不是最漂亮的答案,但我喜欢最小的 CSS。
+ 的 unicode:002B
空格的 unicode:00a0
您可以在 unicode 中搜索其他符号,只需添加空格,直到对位置满意为止。
PS:有人给出了几乎相同的答案,但我无法评论。
Not the preetiest answer but i like minimal css.
unicode for +: 002B
unicode for white space: 00a0
You can search for other symbols in unicode and just add white space until you are satisfied with positioning.
P.S.: Someone gave almost the same answer but I couldn't comment on it.
试试这个
try this
这是一个迟到的答案,但我刚刚遇到这个...要在任何换行上正确缩进,请尝试以下方式:
This is a late answer, but I just came across this... To get the indenting correct on any lines that wrap, try it this way:
这是W3C 的解决方案。适用于 Firefox、Chrome 和 Edge。
This is the W3C solution. Works in Firefox, Chrome and Edge.
http://dev.w3.org/csswg/css-lists/#marker-content
You can also style that inline. Combine single
'
and double quotes"
to avoid conflict:If you can't risk to write unicode in your source code you still can set the HTML entity (ie:
🔔
) in thelist-style-type
and it will be properly rendered (ie:????
) - Run the code snippet to try it out.以下内容引自驯服列表:
The following is quoted from Taming Lists:
这么多解决方案。
但我仍然认为还有改进的空间。
优点:
(不包含绝对像素值)
(第一行和后续行之间没有轻微的偏移)
So many solutions.
But I still think there is room for improvement.
Advantages:
(no absolute pixel values contained)
(no slight shift between first line and following lines)
这是迄今为止我找到的最佳解决方案。它运行良好,并且是跨浏览器的(IE 8+)。
重要的是使字符位于具有固定宽度的浮动块中,以便文本在太长而无法容纳在单行中时保持对齐。
1.2em 是您想要的角色宽度,根据您的需要进行更改。
Here is the best solution I've found so far. It works great and it's cross-browser (IE 8+).
The important thing is to have the character in a floating block with a fixed width so that the text remains aligned if it's too long to fit on a single line.
1.2em is the width you want for your character, change it for your needs.
Font-awesome 提供了一个开箱即用的出色解决方案:
Font-awesome provides a great solution out of the box:
您可以使用
:before
伪选择器在列表项前面插入内容。您可以在 http://www.quirksmode.org/css/beforeafter 找到 Quirksmode 的示例。 html。我用它在块引号周围插入巨大的引号...HTH。
You can use the
:before
pseudo-selector to insert content in front of the list item. You can find an example on Quirksmode, at http://www.quirksmode.org/css/beforeafter.html. I use this to insert giant quotes around blockquotes...HTH.
我的解决方案使用定位来自动正确排列换行。所以你不必担心在 li:before 上设置 padding-right 。
My solution uses positioning to get wrapped lines automatically line up correctly. So you don't have to worry about setting padding-right on the li:before.
建议限定
的样式,这样它就不会影响
列表项。所以:
It's advisable to qualify the styling of the
<li>
so it does not affect<ol>
list items. So: