How to hide the input title when filled in the form using CSS

Asked

Viewed 59 times

-1

<div class="content">
   <span class="title" for="input_example">Título do campo</span>
   <div class="form-content">
       <input type="text" id="input_example" name="input_example">
   </div>
</div>

Is there any way to do CSS with which the .title {} become like display:none, if the text input has the value filled in?

I tried, but I could only do it when the div stays after the input, I would like to do this with it as it is, it is possible?

Fiddle Example reversed

1 answer

1


Basically, you can do this with the CSS Flex Box feature by giving a

flex-Direction: column-Reverse;

HTML

<div class="container column-reverse">
   <input type="text" placeholder="teste" class="item">
   <div class="item">
     título
   </div>    
</div>
 input[type="text"] ~ div {
                display: flex;
                flex-direction: column-reverse;
            }

            input[type="text"]:not(:placeholder-shown) ~ div {
                opacity: 0;
            }
            
.column-reverse {
    flex-direction: column-reverse;
}
.container {
    max-width: 400px;
    margin: 0 auto;
    display: flex;
    border: 1px solid #ccc;
}
.item {
    /* O flex: 1; é necessário para que cada item se expanda ocupando o tamanho máximo do container. */
    flex: 1;
    margin: 5px;
    font-size: 1.5em;
}
https://codepen.io/lucasbpereira/pen/rNjdqzX
  • Lucas, can you explain what was done? only putting up css may not help who needs to understand how to solve :)

  • Lucas he managed to do it that way, "but I could only do it when the div stays after input". He wants to do with the div or span on top of the input.

  • Man, thanks, excellent reply. But it was exactly this alternative that I ended up coming alone, but thank you so much! I could also use position Absolute, but I was looking for something better, and that was the best way out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.