6

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?

AngryHacker
  • 59,598
  • 102
  • 325
  • 594

5 Answers5

7

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.

Federico Alvarez
  • 1,459
  • 1
  • 19
  • 30
4

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.

user1235285
  • 87
  • 2
  • 17
2

If you load the page with Chrome DevTools closed, open DevTools, then refresh the page, and you have the setting "Disable cache (while DevTools is open)" checked, this could explain your experience.

Ethan Selzer
  • 1,757
  • 15
  • 14
1

Check that you don't have content being added inside your <head> before the reference to your stylesheet.

I had this exact same problem on my Win7 box with IE9; that is, my styles were not being applied until I did a refresh. On my XP box with IE8 the problem did not occur; the styles were applied on initial load.

Additional info, may be helpful: Being new to master pages, I was following along "Beginning ASP.Net 4". The example has you add a ContentPlaceHolder in the <head> just before the link to your stylesheet (pg. 200, step 4). The example CPH is empty. Then they have you add the asp:Content ID="Content1"... to your ASPX on pg. 203. I put 'Content1' inside this tag, which put 'Content1' before the stylesheet link in the browser output. This was not in the instructions, I was seeing if it would display in the browser (it does). I had been refreshing all along, so the apply-stylesheet-only-on-refresh issue did not appear until much later. Removing 'Content1' on my ASPX fixed the problem -- maybe some other guru can tell us why IE9 (but not IE8) stops processing <head> content when it encounters a problem on initial load, but not on refresh.

Hope this fixes your problem!

Michael Gorsich
  • 327
  • 1
  • 10
0

I just solved this same exact issue.

My code looked like this while I was getting the error: (Simplified to leave out -webkit -moz etc)

<div style="width: 100%; background: linear-gradient(to bottom, rgba(255,255,255,0.7) 0%,rgba(255,255,255,0.2) 100%), url('relativePathToImg.png') repeat 0 0;">

What I did was use the chrome dev tools, inspected the div then selected the "Computed" tab, not the "Styles" tab. I noticed that chrome computed the css as this:

background-image: linear-gradient(rgba(255, 255, 255, 0.701961) 0%, rgba(255, 255, 255, 0.2) 100%), url(http://mywebsite.com/);

For unknown reasons the relative url was translated into the sites root url, thus the css was looking for an image at the wrong url. I then replaced the "relativePathToImg.png" with the absolute path to the image. This fixed the problem for me.

I noticed you don't explicitly list an image in your code above however you said styles are applied elsewhere. Take a look at the "computed" tab in the dev tools and see what really is going on.

Justin Brown
  • 101
  • 1
  • 1
  • 7