javascript - css textAlign with innerHTML - Stack Overflow

I have trouble align text using innerHTMLa= document.createElement('a');a.style.cssText= �

I have trouble align text using innerHTML

a= document.createElement('a');
a.style.cssText= 'font-size: 20px; cursor: pointer; text-align:center;';
a.innerHTML = 'Align ME TO CENTER!';

Default align to left, and it stuck that way... Any suggestion?

I have trouble align text using innerHTML

a= document.createElement('a');
a.style.cssText= 'font-size: 20px; cursor: pointer; text-align:center;';
a.innerHTML = 'Align ME TO CENTER!';

Default align to left, and it stuck that way... Any suggestion?

Share Improve this question edited Mar 27, 2015 at 0:13 Unheilig 16.3k193 gold badges70 silver badges101 bronze badges asked Mar 26, 2015 at 20:14 user4713659user4713659 251 gold badge1 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

First of all, there is a bug in your code:

where you have

var a = document.createElement('a');
a.style.cssText= 'font-size: 20px; cursor: pointer; text-align:center;';
a.innerHTML = 'Align ME TO CENTER!';

You should have:

var a = document.createElement('a');
a.setAttribute('style','font-size: 20px; cursor: pointer; text-align:center;');
a.innerHTML = 'Align ME TO CENTER!';

And to fix your problem, in the style, you need to set the display property to block, like so:

a.setAttribute('style', 'font-size: 20px; cursor: pointer; text-align: center; display:block;');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745543337a4632227.html

相关推荐

  • javascript - css textAlign with innerHTML - Stack Overflow

    I have trouble align text using innerHTMLa= document.createElement('a');a.style.cssText= �

    23小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信