Forum Moderators: open

Message Too Old, No Replies

Style for createElement()

Best, most cross-browser way to do it

         

adni18

8:01 pm on Jun 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the most accepted (and preferably most-crossbrowser way to set the style attribute(s) of an imaginary (not yet appended) element given life by document.createElement()?

birdbrain

9:27 pm on Jun 24, 2010 (gmt 0)



Hi there adni18,

I would suggest that you use something like this...


el=document.createElement('div');
el.setAttribute('id','foo');


...then apply the appropriate rule in your style sheet...


#foo {
width:780px;
margin:auto;
}

birdbrain

subexpression

4:21 am on Jun 25, 2010 (gmt 0)

10+ Year Member



Heya adni18,

birdbrain is right - that's pretty much standard.

You can also do it with CSS classes:

el=document.createElement('div');
el.className = 'foo';


...and in your stylesheet:

.foo {
width:780px;
margin:auto;
}


In case you didn't already know, the period "." is used for CSS classes, whereas the pound sign "#" is used for CSS id's