Skip to content

Commit 6cce03d

Browse files
committed
Add css scope demo.
1 parent b7113fb commit 6cce03d

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ with.
1010

1111
## My JavaScript Demos - I Love JavaScript!
1212

13+
* [Using :scope To Identify The Host Element In A CSS Selector](https://bennadel.github.io/JavaScript-Demos/demos/scope-pseudo-class)
1314
* [Comparing Undefined Values In JavaScript](https://bennadel.github.io/JavaScript-Demos/demos/undefined-comparison)
1415
* [Using The Button Form Attribute To Create Standalone Buttons In HTML](https://bennadel.github.io/JavaScript-Demos/demos/link-buttons)
1516
* [Box Breathing Exercise With SpeechSynthesis And Alpine.js](https://bennadel.github.io/JavaScript-Demos/demos/box-breathing-alpine)

demos/scope-pseudo-class/index.htm

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<link rel="stylesheet" type="text/css" href="./main.css" />
4+
<body>
5+
6+
<h1>
7+
Using :scope To Identify The Host Element In A CSS Selector
8+
</h1>
9+
10+
<p onclick="selectOdds( event )">
11+
<span> <em>1</em> </span>
12+
<span> <em>2</em> </span>
13+
<span> <em>3</em> </span>
14+
<span> <em>4</em> </span>
15+
<span> <em>5</em> </span>
16+
<span> <em>6</em> </span>
17+
<span> <em>7</em> </span>
18+
<span> <em>8</em> </span>
19+
<span> <em>9</em> </span>
20+
<span> <em>10</em> </span>
21+
</p>
22+
23+
<script type="text/javascript">
24+
25+
function selectOdds( event ) {
26+
27+
var targets = event
28+
.currentTarget
29+
// In the selector, ":scope" represents the current element on which the
30+
// query is being called. This allows us to use the direct-descendant
31+
// operator since it gives us a way to identify the parent of the direct
32+
// descendants.
33+
.querySelectorAll( ":scope > :nth-child( odd )" )
34+
;
35+
36+
for ( var node of targets ) {
37+
38+
node.style = "background-color: hotpink ; color: white ;";
39+
40+
}
41+
42+
}
43+
44+
</script>
45+
46+
</body>
47+
</html>

demos/scope-pseudo-class/main.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
html {
3+
box-sizing: border-box ;
4+
}
5+
html *,
6+
html *:before,
7+
html *:after {
8+
box-sizing: inherit ;
9+
}
10+
11+
body {
12+
font-family: monospace ;
13+
font-size: 20px ;
14+
}
15+
16+
p {
17+
display: flex ;
18+
font-size: 30px ;
19+
gap: 10px ;
20+
}
21+
p span {
22+
background-color: #f0f0f0 ;
23+
border-radius: 5px ;
24+
padding: 3px 15px ;
25+
}
26+
p:hover {
27+
outline: 2px solid cyan ;
28+
outline-offset: 3px ;
29+
}

0 commit comments

Comments
 (0)