Why is the caret invisible in a contenteditable with position:relative?(为什么插入符号在可满足的With位置不可见:Relative?)
问题描述
当contenteditable元素具有position: relative和背景色时,插入符号放在该元素中时是不可见的。下面是一个例子:
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
.bug {
position: relative;
background-color: lightgrey;
}
<div contenteditable>
This has caret
<span class="bug">no caret here?!</span>
this has caret
</div>
我的第一个想法是这是一个浏览器错误,但它在Chrome和Firefox上是完全相同的错误! 插入符号消失的原因是什么?是否有解决办法?
推荐答案
不知道问题的确切原因,但您可以通过多种方法修复它。
我之前的解释(可能是错的):
这不是错误,您将background color放在<span>标记上(在position:relative)在可内容div元素中,因此 Span位于可内容目录的顶部。
我仍然认为这与z-index有关,因为我们可以在Chrome Focus边框顶部的红色背景下看到图像:
删除position:relative
删除<span>的position:relative修复问题:
.no-bug {
background-color: red;
}
<div contenteditable>
This has caret
<span class="no-bug">This has caret !</span>
this has caret
</div>
将z-index添加到<span>元素
添加负值z-index也可以解决此问题:
.no-bug {
background-color: red;
position: relative;
z-index: -10;
}
<div contenteditable>
This has caret
<span class="no-bug">This has caret !</span>
this has caret
</div>
将display: inline-block添加到<span>元素
添加display: inline-block(或display: block)解决问题:
.no-bug {
background-color: red;
position: relative;
display: inline-block;
}
<div contenteditable>
This has caret
<span class="no-bug">This has caret !</span>
this has caret
</div>
其他堆栈溢出相关问题
- Edit cursor not displayed on Chrome in contenteditable
- contenteditable cursor doesn't appear
这篇关于为什么插入符号在可满足的With位置不可见:Relative?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么插入符号在可满足的With位置不可见:Rel
基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
