简体   繁体   English

伪元素占用IE9中的空间

[英]Pseudo element takes up space in IE9

I have a div that I want to behave like an text input field. 我有一个div,想要表现得像文本输入字段。

Here's the full source code example: 这是完整的源代码示例:

HTML: HTML:

<div contenteditable='true'></div>

CSS: CSS:

div:empty:before {
    content: 'Type here...';
    color: #ccc;
}

div{
    padding: 4px;
    border: 1px solid #ccc;
}

See the fiddle in IE9. 参见IE9中的小提琴

In IE9, the problem is that the keyboard caret is displayed at the end. 在IE9中,问题在于键盘插入符显示在末尾。

In Chrome, the caret is at the beginning, which is correct. 在Chrome中,插入符号是开头,这是正确的。

It seems like the problem is that the pseudo :before element is taking up space in IE9. 似乎问题在于伪:before元素正在IE9中占用空间。 How can I make it take up no space like it does it Chrome (behave like a placeholder)? 如何使它像Chrome一样占用空间(就像占位符一样)?

To make it work in IE: 要使其在IE中起作用:

div:empty:before {
    content: 'Type here...';
    color: #ccc;
    display: inline-block;
    width: 0;
    white-space: nowrap;
}

display: inline-block makes it possible to set a width. display: inline-block可以设置宽度。

width: 0 makes sure that the element does not take up space. width: 0确保元素不占用空间。

white-space: nowrap forces the text to be in one line (would break lines otherwise because of the limited space) white-space: nowrap强制文本在一行中(否则会因为空间有限而导致换行)

updated fiddle 更新的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM