HTML - Keep placeholder when user types

前端 未结 5 643
清酒与你
清酒与你 2020-12-10 13:36

I have an input like this:


When I type something in the input the placeholder t

5条回答
  •  被撕碎了的回忆
    2020-12-10 14:11

    Hard to think of a good usecase for such a behaviour, as it is blocking some of the users input.

    An easy way would be to use input::after but this is not supported by any browser right now (thanks @JukkaK.Korpela).

    But you can use a wrapper element and a data attribute, as follows:

    With this css:

    .placeholder
    {
        position: relative;
    }
    
    .placeholder::after
    {
        position: absolute;
        left: 5px;
        top: 3px;
        content: attr(data-placeholder);
        pointer-events: none;
        opacity: 0.6;
    }
    

    Resulting in: enter image description here

    Click here for jsFiddle demo.


    Since you will have to do a lot of tweaking to make this look good, you may also consider using the wrapping

    element as a input "look alike":

    CSS:

    .editable
    {
        position: relative;
        border: 1px solid gray;
        padding: 3px;
        background-color: white;
        box-shadow: rgba(0,0,0,0.4) 2px 2px 2px inset;
    }
    
    .editable > input
    {
        position: relative;
        z-index: 1;
        border: none;
        background-color: transparent;
        box-shadow: none;
        width: 100%;
    }
    
    .editable::after
    {
        position: absolute;
        left: 4px;
        top: 5px;
        content: attr(data-placeholder);
        pointer-events: none;
        opacity: 0.5;
        z-index: 1;
    }
    

    Click here for the Demo 3. (with mocked )

    Click here for the Demo 2. (with contenteditable)

提交回复
热议问题