Hi, I'm new to javascript. I'd like to make a CSS style completely from within javascript. So far, I only know how to modify CSS styles in javascript. For example...
function newStyle(str){
var pa= document.getElementsByTagName('head')[0] ;
var el= document.createElement('style');
el.type= 'text/css';
el.media= 'screen';
if(el.styleSheet) el.styleSheet.cssText= str;// IE method
else el.appendChild(document.createTextNode(str));// others
pa.appendChild(el);
return el;
}
You can pass this method any amount of text-
so long as it is allowed in a stylesheet.
var str='#myStyle{height:300px;width:300px;display:block}'+
'h1{color:black}h3{color:green}p{line-height:1.2;text-indent:1em}';
Thanks for the help! My code still isn't completely working, but it may be because of my CSS. I want to cover the webpage with a semi-transparent block to darken everything. Here's the CSS:
The appendChild is adding the dynamically created css to the page specifically within the head tag. But if the html never uses the reference "myStyle" then that style is never used so nothing will happen. I might not be understanding your problem but the following works.
HTML Code:
<html><head><title>Untitled</title><script>
function DarkenPage() {
var str= "#myStyle{position: absolute; top: 0; left: 0; z-index: 90; width: 100%; height: 500px; background-color: #000; filter:alpha(opacity=60); opacity: 0.6;}"
var pa= document.getElementsByTagName('head')[0] ;
var el= document.createElement('style');
el.type= 'text/css';
el.media= 'screen';
if(el.styleSheet) el.styleSheet.cssText= str;// IE method
else el.appendChild(document.createTextNode(str));// others
pa.appendChild(el);
}
</script></head><body id="myStyle" onload="DarkenPage()"><span style="color:green">Some Text Here</span></body></html>
I tried that code in Safari but I'm not getting any transparency on top of text, just a completely black background behind the text. Maybe it's just a difference in browsers...
PS. I forgot to thank you for your help - this is definitely progress :-) I wonder if there's a way to dynamically set the body tag to use the "myStyle" id.
hmmm, yes IE makes it grey. but even so, I want to overlap the content with a semi-opaque darkness, not underneath. so i not only need to get the filter working...i also need to move it on top somehow.
setting the body element absolute occasionally does weird things- can you try it
set relative? Or put the content in an absolutely positioned Div called myStyle-
if you use the body just use it in your css string:
var str= "body{position:relative;
Bookmarks