2

I need to style inner span element which is inside of contenteditable block by focus on that inner span, it has to be highlighted when is focused. Is that even possible somehow?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="block" contenteditable="true" data-id="123">
        <p>
            Lorem ipsum dolor sit amet <span>consectetur</span> adipisicing elit. Non facere 
            atque aspernatur, pariatur architecto inventore explicabo eligendi 
            <span>maxime</span> laudantium corrupti esse ab nostrum, 
            at quis <span>quas</span> temporibus eum? Asperiores, iste.
        </p>
    </div>
    <style>
        .block {
            outline: none;
        }
        .block span {
            background-color: antiquewhite;
        }
        .block span:focus {
            background-color: red;
        }
    </style>
</body>
</html>
Nazariy
  • 408
  • 3
  • 10

2 Answers2

1

In order to highlight just the span element when you focus it try this in your stylesheet :)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="block" contenteditable="true" data-id="123">
        <p>
            Lorem ipsum dolor sit amet <span>consectetur</span> adipisicing elit. Non facere 
            atque aspernatur, pariatur architecto inventore explicabo eligendi 
            <span>maxime</span> laudantium corrupti esse ab nostrum, 
            at quis <span>quas</span> temporibus eum? Asperiores, iste.
        </p>
    </div>
    <style>
        .block {
            outline: none;
        }
        .block span {
            background-color: antiquewhite;
        }
     
        [contenteditable="true"] span:active {
           background-color: red;
      }
       
    </style>
</body>
</html>

I added a simple script so the element keeps being red when the mouse leaves. Have a look :)

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div class="block" contenteditable="true" data-id="123">
    <p>
      Lorem ipsum dolor sit amet <span>consectetur</span> adipisicing elit. Non facere atque aspernatur, pariatur architecto inventore explicabo eligendi
      <span>maxime</span> laudantium corrupti esse ab nostrum, at quis <span>quas</span> temporibus eum? Asperiores, iste.
    </p>
  </div>
  <style>
    .block {
      outline: none;
    }
    
    .block span {
      background-color: antiquewhite;
    }
  </style>
  <script>
    let selectedElement = null;
    const setFocus = e => {
      if (selectedElement)
        selectedElement.style.backgroundColor = 'antiquewhite';

      selectedElement = window.getSelection().focusNode && window.getSelection().focusNode.parentNode;

      if (selectedElement && selectedElement.tagName == 'SPAN')
        selectedElement.style.backgroundColor = 'red';
    };
    document.onkeyup = setFocus;
    document.onmouseup = setFocus;
  </script>
</body>

</html>

Ok, as you requested no JavaScript and I already invested a lot of time in this question, this is my last answer. I know it is not entirely correct but it is not entirely wrong either.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div class="block" data-id="123">
    <p contenteditable="true">
      Lorem ipsum dolor sit amet </p><span contenteditable="true">consectetur</span>
    <p contenteditable="true"> adipisicing elit. Non facere atque aspernatur, pariatur architecto inventore explicabo eligendi
    </p><span contenteditable="true">maxime</span>
    <p contenteditable="true"> laudantium corrupti esse ab nostrum, at quis </p><span contenteditable="true">quas</span>
    <p contenteditable="true"> temporibus eum? Asperiores, iste.
    </p>
  </div>
  <style>
    .block {
      outline: none;
    }
    
    .block span {
      background-color: antiquewhite;
    }
    
    .block span:focus {
      background-color: red;
    }
    
    p,
    span {
      display: inline;
    }
  </style>
</body>

</html>

Similar case here: CSS: :focus of elements within contenteditable

reymon359
  • 1,240
  • 2
  • 11
  • 34
1

You can use tabindex in span tags. that will solve your problem.

you can read more about tabindex here enter link description here

and focus will work when you use your keyboard. in this case, use your tab to get focus in span tags.

body {
  font-size: 1.2rem;
  line-height: 2rem;
}

.block {
  outline: none;
}
.block span {
  background-color: antiquewhite;
}
.block span:focus,.block span:hover {
  background-color: red;
  color: white;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div class="block" contenteditable="true" data-id="123">
      <p>
        Lorem ipsum dolor sit amet <span tabindex=0>consectetur</span> adipisicing elit.
        Non facere atque aspernatur, pariatur architecto inventore explicabo
        eligendi <span tabindex=0>maxime</span> laudantium corrupti esse ab nostrum, at
        quis <span tabindex=0>quas</span> temporibus eum? Asperiores, iste.
      </p>
    </div>
  </body>
</html>
Kumar Gaurav
  • 738
  • 4
  • 11