/    Sign up×
Community /Pin to ProfileBookmark

CSS priority question

In the following code, the program actions work fine.
The question is why does the “mark” background color work as expected
when the “.tabBtns li { ” background:lime is removed (line 17 of code)
but does not work correctly when the same setting is included?
Is it my mis-understanding of the basic CSS or is this a CSS priority condition I’m missing?

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width-device-width,initial-scale=1.0, user-scalable=yes”/>
<title> Test Page Mark </title>

<style>
/* * { margin: 0; padding: 0; box-sizing: border-box; } /* */
.hide { display: none; }
.mark { background: orange; }

ul { list-style-type: none; cursor: pointer; }

.tabBtns li { display: inline-block; }
.tabBtns li { margin: 0.3em; padding: 0.2em; /* background: lime; /* */
border: 1px solid black;
width: 10em; font-size: 1.25em; }
.tabBtns li:hover { background: yellow; }
</style>

</head>
<body>

<ul class=”tabBtns”>
<li class=”mbtn”> Stack </li>
<li class=”mbtn”> Queue </li>
<li class=”mbtn”> Sets </li>
<li class=”mbtn”> Hash </li>
</ul>

<div class=”tabs”>
<div class=”hide”> Stack </div>
<div class=”hide”> Queue </div>
<div class=”hide”> Sets </div>
<div class=”hide”> Hash </div>
</div>

<hr>
<ol class=”otherList”>
<li> A </li>
<li> B </li>
<li> C </li>
<li> D </li>
</ol>

<script>
console.clear();
function init() {
const tBtns = document.querySelectorAll(‘.tabBtns li’);
// length of tTabs MUST EQUAL tBtns (indexed order)
const tTabs = document.querySelectorAll(‘.tabs div’);

tBtns.forEach( (el,ndx) => {
el.addEventListener( // substitute your own Fat-Arrow => function actions
‘click’, () => { el.classList.toggle(‘mark’); let tab = tTabs[ndx].classList;
(el.classList.contains(‘mark’)) ? tab.remove(‘hide’)
: tab.add(‘hide’);
} // console.log(ndx); } // optional console.log message
)
} );
} init();
</script>
</body>
</html>
[/code]

I expected that the lime background color would be replaced by the
‘mark’ background color (orange) when the link of the list is selected.

to post a comment
CSS

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumOct 14.2019 — Indeed this seems to be a subject of priority or specifity: The background lime is assigned for the selector `.tabBtns li</C>. However the background orange is defined for the selector <C>.mark</C>. The former is more specific than the latter, therefore it overrides the latter.<br/>
Fixing this is easy by changing the selector for the marked button to <C>
.tabBtns li.mark</C> - this is more specific than <C>.tabBtns li`
Copy linkTweet thisAlerts:
@JMRKERauthorOct 15.2019 — Dang, you make it seem so easy.

Appreciate the lesson.

:)
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...