Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style contenteditable=true when hovered?

Tags:

css

I want to change background of elements inside a div, only when hovered and when their property contenteditable is true.

I have tried :

#myDiv[contenteditable="true"]:hover  { background-color: rgba(217, 245, 255,0.5);}

But it doesn't work. And if I move the pseudo class to the div :

#myDiv:hover [contenteditable="true"]  { background-color: rgba(217, 245, 255,0.5);}

Then all fields with contenteditable=true get the background... any trick to fix that in pure css ?

EDIT : HTML example:

<div id="myDiv">
    <span contenteditable="true">blabla</span>
    <div "subdiv" contenteditable="true">blibli</div>
</div>
like image 343
Vincent Teyssier Avatar asked Feb 09 '16 21:02

Vincent Teyssier


People also ask

What is the role of Contenteditable true attribute?

The contenteditable attribute specifies whether the content of an element is editable or not.

What is Contenteditable true?

The contenteditable is used to specify whether the element's content is editable by the user or not. Syntax: <element contenteditable="true|false"> This attribute has two values. true: If the value of the contenteditable attribute is set to true then the element is editable.

How do you use Contenteditable in Javascript?

You can add the contenteditable="true" HTML attribute to the element (a <div> for example) that you want to be editable. If you're anticipating a user to only update a word or two within a paragraph, then you could make a <p> itself editable.


1 Answers

This will select all direct children of .myDiv with [contenteditable="true"]

.myDiv:hover > *[contenteditable="true"] {
  background-color: rgba(217, 245, 255,0.5);
}
<div class="myDiv">
  <p contenteditable="true">Lorem ipsum dolor sit amet.</p>
  <span contenteditable="true">Lorem ipsum dolor.</span>
  <p contenteditable="false">Lorem ipsum dolor.</p>
</div> 
like image 129
Nenad Vracar Avatar answered Oct 15 '22 20:10

Nenad Vracar