<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/:matches -->
<div class="app">
<header>
<p>This is my header paragraph</p>
</header>
<main>
<ul>
<li><p>This is my first</p><p>list item</p></li>
<li><p>This is my second</p><p>list item</p></li>
</ul>
</main>
<footer>
<p>This is my footer paragraph</p>
</footer>
</div>
body {
width: 100vw;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: flex-start;
padding: 2vw;
background: #eaeaea;
}
.app {
background: white;
border-radius: 8px;
padding: 20px;
min-width: 30vw;
position: relative;
&::after {
content: ':matches() Cross Browser';
position: absolute;
padding: 10px;
background: linear-gradient(to right, #f36, #f63);
color: #fff;
border-radius: 5px;
top: -25px;
left: 20px;
}
}
:matches(header, main, footer) p:hover {
color: red;
cursor: pointer;
}
:-webkit-any(header, main, footer) p:hover {
color: red;
cursor: pointer;
}
:-moz-any(header, main, footer) p:hover {
color: red;
cursor: pointer;
}
View Compiled
let matchedItems;
try {
matchedItems = document.querySelectorAll(':matches(header, main, footer) p');
} catch(e) {
try {
matchedItems = document.querySelectorAll(':-webkit-any(header, main, footer) p');
} catch(e) {
try {
matchedItems = document.querySelectorAll(':-moz-any(header, main, footer) p');
} catch(e) {
console.log('Your browser doesn\'t support :matches() or :any()');
}
}
}
for(let i = 0; i < matchedItems.length; i++) {
applyHandler(matchedItems[i]);
}
function applyHandler(elem) {
elem.addEventListener('click', function(e) {
alert('This paragraph is inside a ' + e.target.parentNode.nodeName);
});
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.