<div id="mydiv" contenteditable="true" tabindex="0"></div>
<style>
div{
width:25%;
height:25%;
background: green;
word-wrap: break-word;
overflow-y: auto;
}
span{
color: red;
}
</style>
<script>
document.getElementById("mydiv").addEventListener('keydown', event => {
document.getElementById("mydiv").insertAdjacentHTML('beforeend',"<span style='color:red;'>red</span>");
event.preventDefault();
});
</script>
<div id="mydiv" contenteditable="true" tabindex="0"></div>
<style>
div{
width:25%;
height:25%;
background: green;
word-wrap: break-word;
overflow-y: auto;
}
span{
color: red;
}
</style>
<script>
document.getElementById("mydiv").addEventListener('keydown', event => {
let sp = document.createElement('span')
sp.textContent = 'red'
sp.classList.add('red')
document.getElementById("mydiv").append(sp);
event.preventDefault();
});
</script>