Let me start from the beginning.
var myDivElement = document.createElement("div");
You can then specify absolute position (known beforehand) in the document using css styles and associating a class with the element using:
myDivElement.className += ' my-div-class';
The position can then be defined in a css file.
myDivElement.style.position = 'absolute'; myDivElement.style.top = posY + "px"; myDivElement.style.left = posX + "px";
We can then add any code inside the div element using:
myDivElement.innerHTML = "Some HTML";
The next step is to add the created element to the document (or any other element on the document):
The element can also be removed later using:
- The next step is to align the text in the div element to center. The text can be aligned to center using the following css styles:
vertical-align : middle; text-align : center;
However, its not as simple as it sounds. Unless we define the width and line-height of the div element, this has no affect on the alignment. Define the width and line-height of the div element:
width: 20px; line-height: 20px;
myDivElement.style.width = myWidth + 'px'; myDivElement.style.lineHeight = myHeight + 'px';