如何将其大小限制的可满足段落插入?

发布于 2025-02-02 03:07:43 字数 887 浏览 2 评论 0原文

我需要一个编辑一行段落,以便插入其中的文本一定不能溢出其大小。

目前,我正在使用CSS试图隐藏溢出并避免多行(不显示该段落的孩子)。

但是,理想情况下,我需要插入到段落中的文本在溢出后立即被“剪切”。

这是我到目前为止的代码(也 jsfiddle ):

div
{
  width: 50vw;
  height: 50vh;
  margin: 10px auto;
  background-color: gray;
}

p
{
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 25%;
  background-color: brown;
  text-align: center;
  
  white-space: nowrap;
  overflow-x: hidden;
}

/* Avoiding multiple lines */
p *
{
  display: none;
}
<div>
  <p contentEditable="true">
    Edit me! Unfortunally I can be overflowed... :(
  </p>
</div>

I need an editable one line paragraph such that the text inserted into it must not overflow its size.

I'm currently using CSS to try to hide the overflow and also to avoid multiple lines (by not displaying the paragraph's children).

However, ideally I need the text inserted into the paragraph to be "cut" as soon as it would overflow.

This is my code so far (also in JSFiddle):

div
{
  width: 50vw;
  height: 50vh;
  margin: 10px auto;
  background-color: gray;
}

p
{
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 25%;
  background-color: brown;
  text-align: center;
  
  white-space: nowrap;
  overflow-x: hidden;
}

/* Avoiding multiple lines */
p *
{
  display: none;
}
<div>
  <p contentEditable="true">
    Edit me! Unfortunally I can be overflowed... :(
  </p>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛溺水了 2025-02-09 03:07:43

您可以使用word-break:在这种情况下属于属性

div {
  width: 50vw;
  height: 50vh;
  margin: 10px auto;
  background-color: gray;
}

p {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 25%;
  background-color: brown;
  text-align: center;
  word-break: keep-all;
  white-space: normal;
}

p * {
  display: none;
}
<div>
  <p contentEditable="true">
    Edit me! Unfortunally I can be overflowed... :(
  </p>
</div>

You can use word-break: keep-all; property in this case

div {
  width: 50vw;
  height: 50vh;
  margin: 10px auto;
  background-color: gray;
}

p {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 25%;
  background-color: brown;
  text-align: center;
  word-break: keep-all;
  white-space: normal;
}

p * {
  display: none;
}
<div>
  <p contentEditable="true">
    Edit me! Unfortunally I can be overflowed... :(
  </p>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文