Is it possible to add placeholder in div tag

前端 未结 4 1197
南方客
南方客 2020-12-04 12:26

I would like to display some text to the user when my div element is empty.

I have tried adding a placeholder attribute to my div but the t

4条回答
  •  攒了一身酷
    2020-12-04 12:51

    This can also be achieved without the use of the contentEditable attribute, using plain CSS.

    Using the :empty pseudo-class along with the ::before pseudo-element you can add a placeholder to an empty element.

    The following example provides a fallback placeholder for div elements that do not have a data-placeholder attribute defined.

    Example:

    div:empty::before {
      color: grey;
    }
    div[data-placeholder]:not([data-placeholder=""]):empty::before {
      content: attr(data-placeholder);
    }
    div:empty::before {
      content: 'fallback placeholder';
    }

提交回复
热议问题