Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sass child effecting parents values

I’m trying to make an input where I focus on the input field it removes the placeholder. My code looks as following…

HTML:

<div className="container">
  <input id="input" placeholder="username" />
</div>

I have tried using the ~ operator as following in sass…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#input {
  height: 30px;
  &::placeholder {
     display: flex;
     padding-left: 6px;
   }
  &::focus {
    & ~ .input::placeholder {
      display: none
  }
}
and also the focus-within as following...
#input {
  height: 30px;
  &:focus-within {
     #input::placeholder{
        padding-left: 100px;
     }
   }
  &::placeholder {
     display: flex;
     padding-left: 6px;
   }
  &::focus {
    // set somehow #input::placeholder { display: none }
  }
}

But neither of these work so I would like to know how this is possible in SaSS. I know I could do this with js, but would want to do it in css.

fiddle with the idea..
https://jsfiddle.net/o0qftx3b/13/

>Solution :

You need to set the opacity of the ::placeholder, since it’s not necessarily a pseudo-element like ::before or ::after. It represents attribute text, which doesn’t necessarily adhere to the display property.

You can use this:

#input {
  height: 30px;
  &::placeholder {
    padding-left: 6px;
  }
  &:focus,
  &:focus-within {
    &::placeholder {
      opacity: 0;
    }
  }
}
<div className="container">
  <input id="input" placeholder="username" />
</div>

As a side note:

& ~ .input::placeholder { display: none
That would mean a sibling of the #input element on focus

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use