您可能遇到过 contenteditable 属性。它被用在很多地方。它是文本区域之类的更好的替代品。您可以将 contenteditable="true" 添加到任何 div,然后它就像一个输入字段。在本文中,我将向您展示如何向文本添加占位符,因为它不支持开箱即用的占位符属性。
div[contenteditable] { &[placeholder]:empty::before { content: attr(placeholder); z-index: 9; line-height: 1.7; color: #555; word-break: break-all; user-select: none; } &[placeholder]:empty:focus::before { content: ""; } }
这就是您需要的所有代码!但是,如果您只将其添加到 CSS 中,那么它将不起作用。您需要向 HTML 添加占位符属性,并确保 div 可见。
HTML
CSS
div[contenteditable] { /* Basic styles */ width: 20rem; height: 15rem; padding: 1rem; background: #292929; color: #fff; /* Placeholder code */ &[placeholder]:empty::before { content: attr(placeholder); z-index: 9; line-height: 1.7; color: #555; word-break: break-all; user-select: none; } &[placeholder]:empty:focus::before { content: ""; } }
这里的所有都是它的。希望你觉得它有用!
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3