<div class="container">
  <div id="contenteditable" contenteditable data-placeholder="Edit this text"></div>
</div>
@import url("https://fonts.googleapis.com/css2?family=Exo:wght@600&display=swap");

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  inline-size: 100%;
  min-block-size: 100%;
  font-family: "Exo", system-ui, sans-serif;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr));
  gap: 20px;
  padding: 40px;
}

.container {
  background-color: #fff;
  border: 1px solid #fff;
  border-radius: 16px;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 10px 50px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-width: 300px;
}

[contenteditable] {
  position: relative;
  box-sizing: border-box;
  color: deeppink;
  border-radius: 5px;
  padding: 10px 16px;
  min-height: 180px;
  border: 1px solid deeppink;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 10px 15px -3px,
    rgba(0, 0, 0, 0.05) 0px 4px 6px -2px;
  font-size: 1.5rem;
  width: 100%;
  font-family: "Exo", system-ui, sans-serif;
  overflow: hidden;
  resize: vertical;
}

/* [contenteditable]:empty::before {
  content: attr(data-placeholder);
  color: rgb(0 0 0 / 0.6);
}
 */
const editableElement = document.getElementById("contenteditable");

// 获取占位符文本(data-placeholder)
const placeholder = editableElement.getAttribute("data-placeholder");

// 如果可编辑元素内容是空的,就使用占位符文本
editableElement.innerHTML === "" && (editableElement.innerHTML = placeholder);

editableElement.addEventListener("focus", function (e) {
  const value = e.target.innerHTML;
  value === placeholder && (e.target.innerHTML = "");
});

editableElement.addEventListener("blur", function (e) {
  const value = e.target.innerHTML;
  value === "" && (e.target.innerHTML = placeholder);
});

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.