博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在不使用任何图像或跨度标签的情况下通过CSS在UL / LI html列表中设置子弹颜色[复制]
阅读量:2290 次
发布时间:2019-05-09

本文共 3910 字,大约阅读时间需要 13 分钟。

本文翻译自:

This question already has an answer here: 这个问题在这里已有答案:

  • 12 answers 12答案

Imagine a simple unsorted list with some <li> items. 想象一下带有一些<li>项的简单未排序列表。 Now, I have defined the bullets to be square shaped via list-style:square; 现在,我已经通过list-style:square;定义子弹为方形list-style:square; However, if I set the color of the <li> items with color: #F00; 但是,如果我用颜色设置<li>项目的color: #F00; then everything becomes red! 一切都变红了!

While I only want to set the color of the square bullets. 虽然我只想设置方形子弹的颜色。 Is there an elegant way to define the color of the bullets in CSS... 是否有一种优雅的方式来定义CSS中子弹的颜色......

...without using any sprite images nor span tags! ...不使用任何精灵图像也不使用span标签!

HTML HTML

  • Item 1
  • Item 2
  • Item 3

    CSS CSS

    li{   list-style:square;}

    #1楼

    参考:


    #2楼

    browsing sometime ago, found this site, have you tried this alternative? 浏览前一段时间,发现这个网站,你试过这个替代方案吗?

    li{    list-style-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAE0lEQVQIW2NkYGD4D8RwwEi6AACaVAQBULo4sgAAAABJRU5ErkJggg==");}

    sounds hard, but you can make your own png image/pattern , then copy/paste your code and customize your bullets =) stills elegant? 听起来很难,但你可以制作自己的png图像/模式,然后复制/粘贴你的代码并自定义你的子弹=)仍然优雅?

    EDIT: 编辑:

    following the idea of @lea-verou on the other answer and applying this philosophy of outside sources enhancement I've come to this other solution: 按照@ lea-verou关于另一个答案的想法并应用这种外部源代码增强的理念,我来到另一个解决方案:

    1. embed in your head the stylesheet of the Webfont icons Library, in this case Font Awesome: 将Webfont图标库的样式表嵌入您的脑海中,在本例中为Font Awesome:

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

    1. take a code from FontAwesome (or any other webfont icons). 从FontAwesome (或任何其他webfont图标)获取代码。
    ie: fa-angle-right []

    and use the last part of f... followed by a number like this, with the font-family too: 并使用f的最后一部分...后跟一个这样的数字,也使用font-family

    li:before {    content: "\f105";    font-family: FontAwesome;    color: red; /* or whatever color you prefer */    margin-right: 4px;}

    and that's it! 就是这样! now you have custom bullet tips too =) 现在你也有自定义项目符号提示=)


    #3楼

    I tried this and things got weird for me. 我试过这个,事情对我来说很奇怪。 (css stopped working after the :after {content: "";} part of this tutorial. I found you can color the bullets by just using color:#ddd; on the li item itself. (CSS停止后的工作:after {content: "";} ,部分本教程中,我发现,你可以通过只使用颜色子弹color:#ddd;li项目本身。

    Here's an . 这是一个

    li{    color:#ff0000;        list-style:square;                }a {    text-decoration: none;    color:#00ff00;}a:hover {    background-color: #ddd;}

    #4楼

    I know it's a bit of a late answer for this post, but for reference... 我知道这篇文章有点迟到,但供参考......

    CSS CSS

    ul {    color: red;}li {    color: black;}

    The bullet colour is defined on the ul tag and then we switch the li colour back. 子弹颜色在ul标签上定义,然后我们将li颜色切换回来。


    #5楼

    I simply solve this problem like this, which should work in all browsers: 我只是像这样解决这个问题,它应该适用于所有浏览器:

    HTML: HTML:

    • Foo
    • Bar
    • Bat

    CSS: CSS:

    ul li{    color: red}ul li span{    color: blue;}

    #6楼

    Taking Lea's demo, here's a different way of making unordered lists, with borders: 以Lea的演示为例,这是一种制作无序列表的不同方式,带有边框: :

    HTML HTML

    • Foo
    • Bar
    • Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
      • Son
      • Of
        • Foo
        • Bar
        • Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

    CSS CSS

    ul {list-style: none;margin: 0;}ul:first-child {   padding: 0;}li {    line-height: 180%;    border-bottom: 1px dashed #CCC;    margin-left: 14px;    text-indent: -14px;}li:last-child {    border: none;}li:before {    content: "";    border-left: 4px solid #CCC;    padding-left: 10px;}

    转载地址:http://hscnb.baihongyu.com/

    你可能感兴趣的文章
    90天吃透阿里P8推荐的625页Java编程兵书技术实战,直接入职阿里定级P6
    查看>>
    清华毕业扫地僧,用157集终于把java给讲完了,总计3.13GB
    查看>>
    清华毕业大佬用了一个项目就把SpringMVC+Spring+MyBatis给讲完了
    查看>>
    终于学完阿里P8架构师多年实战经验总结分享微服务之道实战文档
    查看>>
    年薪120W的架构师简历你见过吗?java程序员该如何达到?
    查看>>
    超赞!80W美团架构师7年心血终成轻量级框架SSM整合开发实战文档
    查看>>
    超赞!终于学完了阿里P8架构师7年心血整理总结的微服务实战文档
    查看>>
    真666!阿里资深架构师熬夜纯手写的238页微服务容器化开发实战笔记
    查看>>
    Mygod!阿里P8架构师20年经验总结成微服务设计企业架构转型之道笔记
    查看>>
    终于拿到阿里架构师分享的557页深入理解Java模块系统文档笔记,赶紧分享给大家
    查看>>
    蚂蚁上市延期!还想进阿里做技术专家?这套java面试题送给你
    查看>>
    终于学完了2021年阿里内部480道全套java面试题及答案
    查看>>
    面试官:熟悉Redis吗,项目中你是如何对Redis内存进行优化的
    查看>>
    JVM 实战学习总结:JVM 的分代模型:年轻代、老年代、永久代
    查看>>
    面试官:聊聊 Spring 中的线程安全性
    查看>>
    一篇文教你在 Java 中利用 redis 实现分布式全局唯一标识服务
    查看>>
    深入浅出数据库事务和4种隔离级别
    查看>>
    对限频限流的思考
    查看>>
    15天内4面阿里,成功拿下offer,分享个人面经
    查看>>
    万字长文,带你深入浅出五种负载均衡策略。
    查看>>