.fa {display: inline-block;font-family: FontAwesome;font-style: normal;font-weight: normal;line-height: 1;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;}
/* set all stars to 'empty star' */.stars-container {display: inline-block;}
/* set all stars to 'empty star' */.stars-container .star {float: right;display: inline-block;padding: 2px;color: orange;cursor: pointer;
}
.stars-container .star:before {content: "\f006"; /* fontAwesome empty star code */}
/* set hovered star to 'filled star' */.star:hover:before{content: "\f005"; /* fontAwesome filled star code */}
/* set all stars after hovered to'filled star'** it will appear that it selects all after due to positioning */.star:hover ~ .star:before {content: "\f005"; /* fontAwesome filled star code */}
<p>The oranges elements are the previous sibling li selected using li:nth-child(-n+4)</p>
<div id="list"><span>1</span><!-- this will be selected --><p>2</p><!-- this will be selected --><p>3</p><!-- this will be selected --><div>4</div><!-- this will be selected --><div>5</div><p>6</p><p>7</p><p>8</p><p>9</p></div>
/* default link color is blue */.parent a {color: blue;}
/* prev siblings should be red */.parent:hover a {color: red;}.parent a:hover,.parent a:hover ~ a {color: blue;}
.reverse {display: inline-flex;flex-direction: row-reverse;}.reverse span:hover ~ span { /* On SPAN hover target its "previous" elements */background:gold;}
Hover a SPAN and see the previous elements being styled!<br>
<div class="reverse"><!-- Reverse the order of inner elements --><span>5</span><span>4</span><span>3</span><span>2</span><span>1</span></div>
2.使用Flex与方向:RTL
.reverse {display: inline-flex;direction: rtl;}.reverse span:hover ~ span { /* On SPAN hover target its "previous" elements */background: red;}
Hover a SPAN and see the previous elements being styled!<br>
<div class="reverse"><!-- Reverse the order of inner elements --><span>5</span><span>4</span><span>3</span><span>2</span><span>1</span></div>
3.使用浮点右
.reverse {display: inline-block;}.reverse span{float: right;}.reverse span:hover ~ span { /* On SPAN hover target its "previous" elements */background: red;}
Hover a SPAN and see the previous elements being styled!<br>
<div class="reverse"><!-- Reverse the order of inner elements --><span>5</span><span>4</span><span>3</span><span>2</span><span>1</span></div>
.flex {display: flex;flex-direction: row-reverse;/* Align content at the "reversed" end i.e. beginning */justify-content: flex-end;}
/* On hover target its "previous" elements */.flex-item:hover ~ .flex-item {background-color: lime;}
/* styles just for demo */.flex-item {background-color: orange;color: white;padding: 20px;font-size: 3rem;border-radius: 50%;}
<h2>Hover over any of the blocks below</h2>
<div></div><div></div>
<div class="immediate-previous">Hover for <code>?</code> selector</div><div class="any-previous">Hover for <code>!</code> selector</div><div class="any-subsequent">Hover for <code>~</code> selector</div><div class="immediate-subsequent">Hover for <code>+</code> selector</div>
<div></div><div></div>
<script src="https://rouninmedia.github.io/axe/axe.js"></script>
/* all items (will be styled as previous) */li {color: blue;}
/* the item i want to distinguish */li.milk {color: red;}
/* next items */li ~ li {color: green;}
<ul><li>Tea</li><li class="milk">Milk</li><li>Juice</li><li>others</li></ul>
// Just to check input value// Constsconst starRadios = document.querySelectorAll('input[name="rating"]');
// EventListenersstarRadios.forEach((radio) => radio.addEventListener('change', getStarRadioValue));
// Get star radio valuefunction getStarRadioValue(event) {alert(event.target.value)// Do something with it};
/* Add a style to all the children, then undo the style to the targetand sibling children of your target. */
ul>li {color: red;}
ul>li.target,ul>li.target~li {color: inherit;}