Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically changing the color of an element li doesn't change the color of the bullet point

In Chrome (Version 45.0.2454.101 m) if I add a class to a list element to change its color, the bullet point color is only updated when the window is repainted (resized)

$("#a").click(function() {    
   $("#a").addClass('blue');
});
ul li {
    color: red;
    list-style-type: disc;
    margin-left: 2em;
}
.blue {
    color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
    <li id="a">a</li>
    <li id="b">b</li>
    <li id="c">c</li>
</ul>

Is it a bug in Chrome that can be solved with code? (or is it a bug at all?)

like image 460
gyc Avatar asked Oct 02 '15 12:10

gyc


2 Answers

Probably it is a bug but I would not rely on the standard disc elements.

You could use CSS ::before pseudo-element instead. It is much more configurable and is fully under your control.

$("#a").click(function() {    
   $("#a").addClass('blue');
});
ul li {
    color: red;
    list-style-type: none;
    margin-left: 2em;
}

ul li::before {
    content: "•";
}

.blue {
    color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
    <li id="a">a</li>
    <li id="b">b</li>
    <li id="c">c</li>
</ul>
like image 107
smnbbrv Avatar answered Oct 21 '22 13:10

smnbbrv


You could force the repaint using a fake CSS animation until this bug has been fixed.

$("#a").click(function() {    
   $("#a").addClass('blue');
});
ul li {
    color: red;
    list-style-type: disc;
    margin-left: 2em;
    -webkit-animation:1ms foo infinite; /* using prefix for webkit only */
}
.blue {
    color: blue;
    
}

@-webkit-keyframes foo { /* using prefix for webkit only */
    from { zoom: 0.99999; }
    to { zoom: 1; }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
    <li id="a">a</li>
    <li id="b">b</li>
    <li id="c">c</li>
</ul>

Alternatively, you could use:

$("#a").addClass('blue').hide().show(0);
like image 29
A. Wolff Avatar answered Oct 21 '22 14:10

A. Wolff