【问题标题】:Incorrect effect of content-editable attribute on the div content rendercontent-editable 属性对 div 内容渲染的不正确影响
【发布时间】:2017-10-21 12:03:53
【问题描述】:

为什么将contenteditable="true" 添加到 div 内的 span 中,会导致断线?

div{
  width: 110px;
  height: 80px;
  border:1px solid #5aa; 
}
<div>
  <span>IamANowSpaceLineSample</span>
</div>

<br/>

<div>
  <span contenteditable="true">IamANowSpaceLineSample</span>
</div>

【问题讨论】:

    标签: html css dom contenteditable


    【解决方案1】:

    文本“IamANowSpaceLineSample”比您分配给 div 的 110 像素宽度长。它没有空格,也没有 css 规则说它可以打破单词以适应,因此它不会被包装以适应其父 div 内部,也不会延伸到 div 之外。

    但是,当您添加 contenteditable="true" 时,样式会发生变化,并且会应用以下内容(如您在元素检查器中检查元素时所见):

    span[Attributes Style] {
        -webkit-user-modify: read-write;
        word-wrap: break-word;
        -webkit-line-break: after-white-space;
    }
    

    这会设置word-wrap: break-word;,它会强制浏览器将文本换行以适应父 div 的 110 像素宽度。

    如果你想防止它换行,你可以使用white-space: nowrap;(或者让 div 足够宽以适应!)

    示例:

    div{
      width: 110px;
      height: 20px;
      background-color: #aaa; 
    
      margin-bottom:2em;
    }
    
    span.nowrap{
    white-space: nowrap;
    }
    <div>
      <span>IamANowSpaceLineSample</span>
    </div>
    
    <div>
      <span contenteditable="true">EditableWithDefaultStyle</span>
    </div>
    
    <div>
      <span class = "nowrap" contenteditable="true">EditableWithNoWrapStyle</span>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 2012-01-15
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2020-07-01
      • 2018-11-26
      相关资源
      最近更新 更多