Click to See Complete Forum and Search --> : How to center text created with doc.createTextNode()?


Doug_Dyer
12-13-2002, 11:59 AM
Im trying to center text in a dynamically created <b> tag. Im really new at this, can someone point me in the right direction?


var label = document.createElement("b");
label.appendChild(document.createTextNode("Hello!"));
label.style.position = "absolute";
label.style.left = "100px";
label.style.bottom= "100px";
label.width = "500px";
label.style.fontSize = "8px";

label.style.textAlign = "center";

document.body.appendChild(label);


Thelabel shows up but the textAlign option doesn't seem to do anything.

Thanks
-Doug

gil davis
12-13-2002, 03:37 PM
You have created a block element that is positioned absolute at 100px from the left, and all the text contained in it is centered relative to the block. You haven't specified a width for the block, so it is the size of the contained text. Since you have such a small amount of text, you'll never see any difference between centered and not.

Absolute positioning removes the object from the flow of the document (it no longer takes up space).

Doug_Dyer
12-13-2002, 04:10 PM
Oh I was under the impression it would center it within the width, but I think I see what you are saying. Its the object that is centered at the block level.