admin管理员组文章数量:1395248
Is that possible to append CSS
from a variable. for example
var myHtml = "<div>My example html</div>";
// in html i can do this
<div [innerHTML]="myHtml"></div>
But what if I have CSS
stored in variable. like below:-
var myCss = ".exampleClass {margin: 0;width: 100%;}";
How can I add this css variable in my CSS
file OR in HTML
as a working css?
I'm using Angular 4+ but even JavaScript solution also appreciated.
Is that possible to append CSS
from a variable. for example
var myHtml = "<div>My example html</div>";
// in html i can do this
<div [innerHTML]="myHtml"></div>
But what if I have CSS
stored in variable. like below:-
var myCss = ".exampleClass {margin: 0;width: 100%;}";
How can I add this css variable in my CSS
file OR in HTML
as a working css?
I'm using Angular 4+ but even JavaScript solution also appreciated.
Share Improve this question asked Apr 1, 2018 at 10:27 RajnishCoderRajnishCoder 3,7357 gold badges21 silver badges36 bronze badges2 Answers
Reset to default 5You can simply insert a new <style>
tag into your page using JavaScript:
var myCss = ".exampleClass {margin: 0;width: 100%;color:blue}";
var style = document.createElement("STYLE");
style.innerText = myCss;
document.body.appendChild(style);
<div class="exampleClass">Lorem ipsum</div>
Try:
<div [style.width.%]="WidthValue" [style.marginTop.px]="marginTopValue"></div>
where, in *.ts file, you are having value in a variable
WidthValue: number = 100;
marginTopValue: number = 0;
Adding one more way,
<div [ngStyle]="styleObject"></div>
where,
styleObject = {
'width': '100px',
'margin-top': '10px'
}
本文标签: javascriptHow to append CSS from a variable in CSS or HTML file Angular 4Stack Overflow
版权声明:本文标题:javascript - How to append CSS from a variable in CSS or HTML file? Angular 4+ - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744719134a2621588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论