I have a DOM structure like
<div id="container">
<div id="one">Some fancy text</div>
<div id="two">Some other text</div>
<div id="three">and that's enough!</div>
</div>
In my structure, programmatically, during the webpage life, a lot of divs get added, removed and moved inside 'container' but i need a final < br > being at the end of the text the last div ('and that's enough!< br >' on div 'three', in this case) at any moment. It is possible to achieve this result by css, like using 'content' on last child of 'container'?
It would be the best solution for I would not have to insert the br in the last div and control any time what is the last div, remove it and re-add it to it if changes. I'm open to other solutions, anyway.
NB: I can't use jquery.
EDIT: It is not a style problem. Truly, I need the br to be there in the html code to solve another more complex problem, so it is not about style and spaces.
EDIT: I didn't mention the real problem for is way more complex than this... I'm making an editor. In this editor, text is inside the div 'container' which is a content editable div and then divided in several internal divs, changing while the user writes. Due to a problem in Firefox, if I don't have one br at the end of the last div, the caret (cursor) acts really weird, and the only solution seems to be keeping a br in that position at any moment.
EDIT: going deeper in the problem... The problem is known. Firefox solves this putting extra divs in html around the text. I don't want extra divs to be added so when a return is pressed, I force the behaviour of adding a simple br (instead of an extra div container, as normally it would be). Doing this, the first time you press a return at the end of the last div, the caret moves correctly (if you keep writing, it writes in the correct position on a new line), but displays in a strange position, like in the middle between the last and the previous line or at the beginning of the previous line. This happens only at the end of the last div. If you have a br at the end of the last div the caret acts normally.
It is shown here too, but proposed solution are not working for me Firefox sets wrong caret position contentEditable with :before
window.addEventListener('keydown', function (event) {
if(event.keyCode === 13){
event.preventDefault(); // Ensure it is only this code that runs
console.log("+++ Pressed return and adding a br!");
addHtmlElementAtCursor('br');
return false;
}
});
function keyPress(event) {
}
function addHtmlElementAtCursor(element) {
var sel, range, textNode;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
if (element==' ') {
textNode = document.createTextNode('\u00A0');
}else{
textNode = document.createElement(element);
}
range.insertNode(textNode);
// Move caret to the end of the newly inserted text node
range.setStartAfter(textNode, textNode.length);
range.setEndAfter(textNode, textNode.length);
sel.removeAllRanges();
sel.addRange(range);
}
} else if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
range.pasteHTML(text);
}
}
<div contenteditable=true>
<div id="one">some text</div>
<div id="two">some other text</div>
<div id="three">some other other text, try to give a return after the laste e of 'here' -> HERE</div>
</div>
` in there - CSS can not insert elements (unless you count ::before ::after etc, but these do not insert an element into the HTML - so, you'll need to insert the element yourself - why not always have it there, and insert/remove elements before it – Bravo Oct 08 '19 at 09:23
` there in the first place, and do your adding/removing/moving above it – Bravo Oct 08 '19 at 09:26