Solution 1 :

Just add focus-within before your ::after

.form-search:focus-within::after {
    display: none;
}
.form-search__input:not(:focus) {
  color: transparent !important;
}

.form-search__input {
  font-weight: 600;
  font-size: 18px;
  color: #abff00;
  font-style: italic;
  font-family: "Open Sans";
}

.search-txt {
  position: absolute;
  left: 3rem;
  font-weight: 600;
  font-size: 18px;
  color: #abff00;
  font-style: italic;
  font-family: "Open Sans";
}

.form-search::after {
  content: "Search";
  font-weight: 600;
  font-size: 18px;
  color: #abff00;
  font-style: italic;
  font-family: "Open Sans";
  position: absolute;
  left: 3rem;
}

.form-search:focus-within::after {
  display: none;
}
<form role="search" method="get" class="form-search" action="/" _lpchecked="1">
  <input type="text" value="Search" name="s" id="s" class="form-search__input" placeholder="Search">
</form>

Solution 2 :

I guess you want this inside the input tag:

onfocus="this.value=''" 

use this instead:

 <input type="text" onfocus="this.value=''"  value="Search" name="s" 
 id="s" class="form-search__input" placeholder="Search">

Solution 3 :

You need to use css event :focus-within

Because the focus is not directly on form but on input
:focus-within will listen on focus on parent and childs

You can replace

.form-search:after:focus {
    display: none;
}

with

.form-search:focus-within::after {
    display: none;
}

If you want to hidde after the first focus and don’t rerender after the focus out, you need to use Javascript

Solution 4 :

You just need to change

.form-search:focus-within::after {
  display: none;
}
.form-search__input:not(:focus) {
  color: transparent !important;
}

.form-search__input {
  font-weight: 600;
  font-size: 18px;
  color: #abff00;
  font-style: italic;
  font-family: "Open Sans";
}

.search-txt {
  position: absolute;
  left: 3rem;
  font-weight: 600;
  font-size: 18px;
  color: #abff00;
  font-style: italic;
  font-family: "Open Sans";
}

.form-search::after {
  content: "Search";
  font-weight: 600;
  font-size: 18px;
  color: #abff00;
  font-style: italic;
  font-family: "Open Sans";
  position: absolute;
  left: 3rem;
}

.form-search:focus-within::after {
  display: none;
}
<form role="search" method="get" class="form-search" action="/" _lpchecked="1">
  <input type="text" value="Search" name="s" id="s" class="form-search__input" placeholder="Search">
</form>

Problem :

How to hide Text with ::after on focus? I’ve tried this:

.form-search:after:focus {
   display: none; }

But :focus not works with after.
This is my code for this search bar:

.form-search__input:not(:focus) {
  color: transparent !important;
}
.form-search__input {
  font-weight: 600;
  font-size: 18px;
  color: #abff00;
  font-style: italic;
  font-family: "Open Sans";
}
.search-txt {
  position: absolute;
  left: 3rem;
  font-weight: 600;
  font-size: 18px;
  color: #abff00;
  font-style: italic;
  font-family: "Open Sans";
}
.form-search::after{
  content: "Search";
  font-weight: 600;
  font-size: 18px;
  color: #abff00;
  font-style: italic;
  font-family: "Open Sans";
  position: absolute;
  left: 3rem;
}
.form-search:after:focus {
  display: none;
}
<form role="search" method="get" class="form-search" action="/" _lpchecked="1">
  <input type="text" value="Search" name="s" id="s" class="form-search__input" placeholder="Search">
</form>

Search bar on focus

Comments

Comment posted by DecPK

It is always a good approach to show runnable code.

Comment posted by DecPK

It is always a good approach to show runnable code.

By