Kevin Lee
Kevin Lee

Reputation: 1420

Highlight a div element in a content editable input as if it was text

I current have the following JS Fiddle showing a div with a background image in a content editable input. I would like to be able to highlight the div as if it were text.

.input {
  border: 1px solid black;
  width: 200px;
}

.emoji {
  width: 16px;
  height: 16px;
  background-size: contain;
  display: inline-block;
  background-image: url(https://twemoji.maxcdn.com/2/72x72/1f609.png);
}
<div contenteditable="true" class="input">
    <div class='emoji'/>
</div>

Notice how in the fiddle above attempting to highlight the emoji with the mouse as if it were text does not highlight the emoji properly.

I have tried the following 2 solutions but they have not worked.

1) Setting a tab-index="-1" on the emoji div and adding the following css to set the background color to make the emoji look highlighted

.emoji {
    &:focus {
        background-color: #338fff;
    }
}

2) Using the ::selected css and setting the background color to make it look highlighted

.emoji {
    &:selected {
        background-color: #338fff;
    }
}

Any help is appreciated!

Upvotes: 0

Views: 1107

Answers (2)

Joseph Webber
Joseph Webber

Reputation: 2173

Change the emoji div to an image tag and link it to a transparent source.

#input {
  border: 1px solid black;
  width: 200px;
}

.emoji {
  width: 16px;
  height: 16px;
  background-size: contain;
  display: inline-block;
  background-image: url(https://twemoji.maxcdn.com/2/72x72/1f609.png);
}
<div contenteditable="true" id="input">
  <img src="http://upload.wikimedia.org/wikipedia/commons/c/ce/Transparent.gif" class="emoji"/>
</div>

Upvotes: 1

DCR
DCR

Reputation: 15698

It's not really clear what you are asking. How does the following work for you?

.input {
  border: 1px solid black;
  height:27px;
  width: 200px;
}

.emoji {
  width:16px;
  height:16px;
  background-size: contain;
  margin:0 auto;
  margin-top:6px;
 

  display: block;
  background-image: url(https://twemoji.maxcdn.com/2/72x72/1f609.png);
}

#a1{
width:173px;
display:block;
float:right;
line-height:1.1;
}

#a2{
display:inline-block;
 background-color: #338fff;
 width:27px;
 height:27px;}
<div  class="input">
   <div id='a2'>
       <div class='emoji' ></div>
   </div>    
  <div id='a1' contenteditable="true">1234</div>
</div>

Upvotes: 0

Related Questions