Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The CSS style not applied until refresh

I have a webpage with the following CSS style that overrides some higher level styles.

#pnlActions { 
    background-image: -webkit-gradient(linear,left top,left bottom,from(#000),to(#000));
    background-image: -webkit-linear-gradient(#000,#000);
    background-image: -moz-linear-gradient(#000,#000);
    background-image: -ms-linear-gradient(#000,#000);
    background-image: -o-linear-gradient(#000,#000);
    background-image: linear-gradient(#000,#000);               
}

The odd thing is that the first time I load the page, the styles are completely ignored until I refresh the page - then the styles are applied. Then I'll quit the browser, load the page again and the styles are no longer applied (until I refresh the page).

I can tell all this from chrome/firefox/ie browser tools.

I am at a complete loss to explain why this is happening. Any ideas?

like image 489
AngryHacker Avatar asked Sep 14 '13 00:09

AngryHacker


People also ask

Why is my CSS style not being applied?

Make sure that you add the rel attribute to the link tag When you add an external CSS file to your HTML document, you need to add the rel="stylesheet" attribute to the <link> tag to make it work. If you omit the rel attribute from the <link> tag then the style won't be applied to the page.

Why is CSS style not updating?

Browser Cache If you are adding/modifying in-line CSS or JavaScript and not seeing the changes reflected in the HTML source, the page is likely being cached. The solution may be to purge the WordPress object cache, which stores our page caching (Batcache) renders.

How do you refresh CSS?

Anytime you make changes to CSS, JavaScript and are viewing the page you've updated - you will see this concern. You can force a refresh by pressing CTRL+F5 on the browser and that will get the latest version.

In what order are CSS styles applied?

Style rules take precedence in right-to-left order. Rules to the right take precedence over rules to the left. Properties that are not declared are carried over and reapplied to succeeding style sheets.


2 Answers

A couple of years later, but this is how I solved it.

For some reason having the STYLE inside the HEAD tag (Internal CSS) was only working after a refresh.

But adding it as a LINK (External CSS) solved it. Now it works from the first run.

Inside the CSS I added !IMPORTANT to the styles because I needed to override some other elements, just like the OP.

For those who have doubts on how to add Internal or Extenal CSS, please check this link.

like image 104
Federico Alvarez Avatar answered Sep 25 '22 17:09

Federico Alvarez


Try appending the following php code to your .css file in the header. Your file must have the extension of .php for this to work.

<link rel="stylesheet" type="text/css" media="screen" href="your.css?<?php echo time(); ?>" />

That will rule out caching of the style sheet.

like image 20
user1235285 Avatar answered Sep 23 '22 17:09

user1235285