1

I am trying to make an application to edit documents using contenteditable. I would like to apply a :focus state to an element, like this:

.focus:focus {
color: red;
}

However, this doesn't appear to work when the element is nested in another contenteditable element. If I have some html like this, it works:

<span contenteditable class="focus">content</span>

But if I do something like this, it doesn't work:

<div contenteditable>
<span contenteditable class="focus">content</span>
</div>

For the purposes of how I am using this, I need the outer contenteditable element. I would prefer to use css only for this. Is there a workaround for this?

Demo

firefoxuser_1
  • 1,793
  • 13
  • 22
  • I don't want the :focus to apply to the div, though. I want it to apply to the span with the "focus" class. Also, if I change the span in the working example to a div, it still works. – firefoxuser_1 Mar 07 '15 at 21:43

2 Answers2

1

Will this work for you? http://jsfiddle.net/tdovuyyr/2/

div:focus span {
    color: white;
    background: green;
}
Stickers
  • 75,527
  • 23
  • 147
  • 186
1

In the end, I added a wrapper element around the spans that cannot be contenteditable. This makes the focus work correctly.

<div contenteditable>
<span contenteditable="false">
<span contenteditable class="focus">content</span>
</span>
</div>

I don't like this solution very much, but I also don't want to add a lot of javascript to this.

firefoxuser_1
  • 1,793
  • 13
  • 22