Learn Responsive Web Design by Building a Piano - Step 18

Tell us what’s happening:
I’m having difficulty understanding exactly how the “::after” pseudo selector works in this case with the piano…I referenced step 18 because this is the step I’m asked to initially create this selector. I’m now at step 20, where I have to assign the width and height of the “black keys” using this pseudo selector. Once done, the piano, well, actually looks like a piano:

But by way of experimentation, when I remove the “::after” pseudo selector, the piano looks like this:

So I guess what I’m not understanding is how the “::after” pseudo selector actually causes this to look like a piano? But then removing it causes the whole thing to look like complete nonsense? I know the pseudo selector inserts content after the content of the selected element, but removing the pseudo selector causes a change so drastic that I can’t wrap my brain around how it works here… Hopefully I got my question across effectively…any help is appreciated and if you need me to elaborate further please let me know.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Piano</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./styles.css">
  </head>
  <body>
    <div id="piano">
      <div class="keys">
        <div class="key"></div>
        <div class="key black--key"></div>
        <div class="key black--key"></div>
        <div class="key"></div>
        <div class="key black--key"></div>
        <div class="key black--key"></div>
        <div class="key black--key"></div>

        <div class="key"></div>
        <div class="key black--key"></div>
        <div class="key black--key"></div>
        <div class="key"></div>
        <div class="key black--key"></div>
        <div class="key black--key"></div>
        <div class="key black--key"></div>

        <div class="key"></div>
        <div class="key black--key"></div>
        <div class="key black--key"></div>
        <div class="key"></div>
        <div class="key black--key"></div>
        <div class="key black--key"></div>
        <div class="key black--key"></div>
      </div>
    </div>
  </body>
</html>
/* file: styles.css */
html {
  box-sizing: border-box;
}

*, *::before, *::after {
  box-sizing: inherit;
}

#piano {
  background-color: #00471b;
  width: 992px;
  height: 290px;
  margin: 80px auto;
  padding: 90px 20px 0 20px;
}

.keys {
  background-color: #040404;
  width: 949px;
  height: 180px;
  padding-left: 2px;
}

.key {
  background-color: #ffffff;
  position: relative;
  width: 41px;
  height: 175px;
  margin: 2px;
  float: left;
}

.key.black--key::after {
  background-color: #1d1e22;
  content: "";
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0

Challenge: Learn Responsive Web Design by Building a Piano - Step 18

Link to the challenge:

maybe playing with another example will help?

All it’s doing is adding more ‘elements’ after the the selected elements.

1 Like

I think after analyzing it a little bit, I am starting to grasp it more…although there are still gaps in my understanding. It’s probably gonna take me revisiting it a few times. Thanks for that link, I had actually found myself at that very same exercise you referenced at w3schools! I understood how it works there but I guess the apparent, to me, complexity of this css piano left me confused. Down the rabbit hole I go I guess :sweat_smile:

they’re just making a different shape.
(black rectangle). You can play with the colors the width etc to try to understand more.

2 Likes

Hello @alecv0824 !
I had the same doubt and did the same. Did you finally get to the point? How do the pseudoselectors work?

Hello! Could this be a way to add a container with the content we want by setting a content attribute in the css?
First I thought it was about positioning (after->right, before->left or, under and above, like in a z axis)… but after trying different colors and sizes as you suggested, I still can´t completely understand…
Using both, before and after I got the following image:

here’s a pen showing the selectors (before and after) doing different things to the piano

Hopefully this illustrates how they work. (the reason you couldn’t see a difference I’m guessing is because you didn’t actually differentiate their values)

1 Like

Thanks a lot! I will look into it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.