There are cases where you do not want turn the visibility of certain elements off in your default CSS file. The reason is simple. When you get user who turned off the javascript in his/her browser these elements should be visible. The visibility off these elements should only be turned off for users we javascript turned on. Here is an example. You might have a content in boxes with tab navigation on top off them. You only want the first tab content to be visible when the page loads and leave the other turned off. The tab navigation is using javascript so you need to make sure that even when javascript is turned off the content off the other tabs is visible. In this example you can’t just simple turn the other boxes off in your main CSS and you also can’t use onload javascript or jquery function to turned this off because they would be visible until the page fully loads.
Here is the solution. Create javascript file and link it from the top of your page.
You can see the example on lewro.com.
<script type="text/javascript" src="/scripts/script_styles.js"></script>
Create a CSS file called script_styles.css and inside of it turn off the elements you do not want to be visible. You could for example create CSS class called visibility_off.
.visibility_off {
display:none;
}
Inside the file javascript file we have created earlier and linked from the top of our page insert this function. Make sure the path to the CSS file is right.
(function () {
var head = document.getElementsByTagName("head")[0];
if (head) {
// Lets build the CSS link
var scriptStyles = document.createElement("link");
scriptStyles.rel = "stylesheet";
scriptStyles.type = "text/css";
// Set the path to your CSS file
scriptStyles.href = "http://www.lewro.com/wp-content/themes/caffee/css/script_styles.css";
head.appendChild(scriptStyles);
}
}());
So what is happening in here? The javascript fires when the page starts loading and inserts the link to your script_styles.css into the page’s head. This way your styles are immediately applied and there is not flickering of text which should not be visible during the loading of the site.