https://codesandbox.io/s/damp-worker-k7fj6y?file=/src/App.js
Why is the .row:nth-of-type(1) > .content:nth-of-type(4)
.content <Link/>
not displaying?
Is it a bug, am I just missing something?
JavaScript
x
22
22
1
import "./styles.css";
2
import { Link } from "react-router-dom";
3
4
export default function App() {
5
return (
6
<div className="App">
7
<div className="row">
8
{/* I dont want this div to be a Link */}
9
<div className="content"></div>
10
<Link to="/" className="content"></Link>
11
<Link to="/" className="content"></Link>
12
<Link to="/" className="content"></Link>
13
</div>
14
<div className="row">
15
<Link to="/" className="content"></Link>
16
<Link to="/" className="content"></Link>
17
<Link to="/" className="content"></Link>
18
<Link to="/" className="content"></Link>
19
</div>
20
</div>
21
);
22
}
JavaScript
1
33
33
1
.App {
2
height: 100vh;
3
width: 100vw;
4
}
5
6
.row:nth-of-type(1) {
7
height: 500px;
8
width: 100%;
9
display: grid;
10
grid-template-areas:
11
"a a b c"
12
"a a d d";
13
grid-template-columns: 1fr 1fr 1fr 1fr;
14
grid-template-rows: 1fr 1fr;
15
gap: 20px;
16
}
17
18
.row:nth-of-type(1) > .content:nth-of-type(1) {
19
grid-area: a;
20
background-color: orange;
21
}
22
.row:nth-of-type(1) > .content:nth-of-type(2) {
23
grid-area: b;
24
background-color: blue;
25
}
26
.row:nth-of-type(1) > .content:nth-of-type(3) {
27
grid-area: c;
28
background-color: green;
29
}
30
.row:nth-of-type(1) > .content:nth-of-type(4) {
31
grid-area: d;
32
background-color: red;
33
}
I am not looking for an alternative approach to achieve the same result, I am simply asking why the fourth <Link/>
is not displaying so I know what is going wrong.
Advertisement
Answer
Use the :nth-child
psuedoselector since you are mixing element types (div
and Link
(a
)), there isn’t a 4th link type element to style.
JavaScript
1
17
17
1
.row:nth-of-type(1) > .content:nth-child(1) {
2
grid-area: a;
3
background-color: orange;
4
}
5
.row:nth-of-type(1) > .content:nth-child(2) {
6
grid-area: b;
7
background-color: blue;
8
}
9
.row:nth-of-type(1) > .content:nth-child(3) {
10
grid-area: c;
11
background-color: green;
12
}
13
.row:nth-of-type(1) > .content:nth-child(4) {
14
grid-area: d;
15
background-color: red;
16
}
17