64

I've got this html:

<p>
    <span class="fancify">Parting is such sweet sorrow!</span><span> - Bill Rattleandrollspeer</span>
</p>

...and this css (added to the bottom of Site.css):

.fancify {
    font-size: 1.5em;
    font-weight: 800;
    font-family: Consolas, "Segoe UI", Calibri, sans-serif;
    font-style: italic;
}

So, I would expect the quote ("Parting is such sweet sorrow!") to be italicized, and of a different font than the name of the quotee (Bill Rattleandrollspeer), since its span tag has the class "fancify" attached to it. The class should certainly be seen, as the file in which it appears references the layout file which uses the Site.css file.

What rookie mistake am I making now?

UPDATE

I thought maybe the problem was that I had added the new class in Site.css following this section in that file:

/********************
*   Mobile Styles   *
********************/
@media only screen and (max-width: 850px) {

...but I moved it above there, and it is still not working, and not seen via F12 | Inspect element for the label in question.

I moved the reference to Site.css below the bootstrap.css file, which does indeed change the appearance of that text, but still not italicized, and still not seen in the element inspector...

UPDATE 2

Here's how the HTML is coming down:

<p>
    <span>
        <label class="fancify">Parting is such sweet sorrow!</label>

...and here's my css rule in Site.css:

p span label .fancify {
        font-size: 1.5em;
        font-weight: 800;
        font-family: Consolas, "Segoe UI", Calibri, sans-serif;
        font-style: italic;
        display: inline;
    }

...but it's not being recognized. I consider this a breech of css/html protocol, and should be adjudicated by some world body. Then again, I could be making some silly mistake somewhere.

14
  • are you saying, when you inspect element, you can see the css properties ?
    – karthikr
    Commented May 13, 2013 at 0:55
  • hit F12 in your browser and see what styles are being applied to the span - something may be overriding it
    – fnostro
    Commented May 13, 2013 at 0:59
  • 2
    Nothing with what you posted is wrong - can you post the rest of the relevant HTML/CSS?
    – Adrift
    Commented May 13, 2013 at 0:59
  • 2
    The question effectively asks people to debug a large piece of HTML and CSS code, without actually disclosing the code or giving access to it. Commented May 13, 2013 at 5:19
  • 2
    @Jukka: It seems to me the pertinent parts are there. I don't see the need to direct the firehose at the world. Commented May 13, 2013 at 14:14

18 Answers 18

81

There could be an error earlier in the CSS file that is causing your (correct) CSS to not work.

10
  • 6
    I had a stray semicolon Commented Oct 8, 2014 at 20:48
  • 5
    @VenkatapathiRajuM this is some wisdom you normally have to feel some pain to learn. :-) Commented Oct 9, 2014 at 0:52
  • Nice tip.... I'm building a page which needs to be integrated into a WP theme. The page first loaded my CSS and everything worked nicely, then it loaded some more CSS from that crappy WP, the page looked ok, but when I was adding dynamic content, nothing worked... that .... WP had some errors in the CSS, now try to find that. Forcing my own CSS to be loaded at the very end of the document, solved the problem. (masked it, because there is still something wrong in the WP CSS, but cannot be bothered with it). Thanks for the tip. Commented Apr 21, 2016 at 16:44
  • I had a stray semicolon such that the CSS worked locally and failed remotely.
    – JoshNaro
    Commented Jul 20, 2016 at 16:13
  • 1
    If you're looking for CSS errors, check out csslint.net
    – user9016207
    Commented Mar 7, 2018 at 22:31
66

Posting, since it might be useful for someone in the future:

For me, when I got here, the solution was browser cache. Had to hard refresh Chrome (cmd/ctrl + shift + R) to get the new styles applied, it seems the old ones got cached really "deep".

This question/answer might come in handy for someone. And hard refresh tips for different browsers for those who don't use Chrome.

3
  • 4
    Thanks, I have spent considerable amount of time validating my CSS, and trying to figure why my CSS was not getting picked at all in Chrome Inspector. Ctrl+Shift+R did the job! Commented Jan 28, 2019 at 5:34
  • 1
    OMG thank you, it was killing me ! I have tried many things but giving the correct path and clearing the cache solved it. Commented Feb 18, 2021 at 12:19
  • OMG! Really you're a life Savior.. I've spent a full day scratching my head, trying to find out why was it creating such a problem. Finally your answer helped me. Thanks a million ton! Small things really do have a big impact. It really worked as a miracle. Bless you for the worthy and helpful post!
    – Priya
    Commented May 5, 2022 at 6:19
54
+350

Have you tried forcing the selectors to be in the front of the class?

p span label.fancify {

    font-size: 1.5em;
    font-weight: 800;
    font-family: Consolas, "Segoe UI", Calibri, sans-serif;
    font-style: italic;
}

Usually it will add more weight to your CSS declaration. My mistake ... There should be no space between the selector and the class. The same goes for the ID. If you have for example:

<div id="first">
    <p id="myParagraph">Hello <span class="bolder">World</span></p>
</div>

You would style it like this:

div#first p#myParagraph {
     color : #ff0000;
}

Just to make a complete example using a class:

div#first p#myParagraph span.bolder{
    font-weight:900;
}

For more information about pseudo-selectors and child selectors : http://www.w3.org/TR/CSS2/selector.html

CSS is a whole science :) Beware that some browsers can have incompatibilities and will not show you the proper results. For more information check this site: http://www.caniuse.com/

3
  • Yes, I have now. Based on what Google Dev Tools is showing me, it only wants to style that bit as a label. Commented May 13, 2013 at 2:10
  • Technically just the regular class .fancify would work, unless you are over writing it somewhere else. Other wise you can always try to force the selector to really narrow down to the element you are trying to style. Look at this fiddle here : jsfiddle.net/MM6qQ
    – hayonj
    Commented May 13, 2013 at 2:16
  • 2
    I wouldn't use the !important flag as it is not recommended
    – hayonj
    Commented May 13, 2013 at 2:36
26

I was going out of my mind when a rule was being ignored while others weren't. Run your CSS through a validator and look for parsing errors.

I accidentally used // for a comment instead of /* */ causing odd behavior. My IDE said nothing about it. I hope it helps someone.

3
  • Yes, what the world needs now is more accurate error detection, not just a "whoops-a-daisy!" or bizarre non-functioning responses. Commented Oct 8, 2015 at 22:09
  • Thanks! In my case there was an extra closing brace earlier in the CSS file.
    – Sam
    Commented Sep 24, 2018 at 23:16
  • 1
    Notepad++ helps to spot these issues as it color codes errors like that.
    – Xavier
    Commented Sep 18, 2019 at 14:12
5

Maybe your span is inheriting a style that forces its text to be normal instead of italic as you would like it. If you just can't get it to work as you want it to you might try marking your font-style as important.

.fancify {
    font-size: 1.5em;
    font-weight: 800;
    font-family: Consolas, "Segoe UI", Calibri, sans-serif;
    font-style: italic !important;
}

However try not to overuse important because it's easy to fall into CSS-hell with it.

1
  • 1
    Presumably because my .fancify declaration is not being seen at all for some reason, even marking it as !important does nothing. Commented May 13, 2013 at 4:10
4

For me, the problem was incorrect content type of the served .css file (if it included certain unicode characters).

Changing the content-type to text/css solved the problem.

3

I know this is an old post but I thought I might add a thought for people who come across a similar problem. I'm assuming that you are using ASP.NET MVC since you mentioned site.css.

Check your Bundles.config file to see if you have BundleTable.EnableOptimizations = true; If you don't, then it can be your problem since this allows the program to be bundles and "minified". Depending on if you run in debug mode or not this could have an effect.

2

In addition to the solutions posted above, having gone through the exact same problem, make sure you check your HTML. More specifically whether you've properly labelled your elements, as well as class and id selectors. You can do this either manually or through a validator (https://validator.w3.org/).

For me, I missed the equal sign next to the class (<div class someDiv> vs <div class = "someDiv">, hence why no CSS property was applied.

2

A key point, here, may be the way the CSS rules propagate. Some rules are more important than others, so CSS rules do not always "cascade" in the way you might imagine that they ought to. This precedence of CSS rules is known as specificity - see (for example) description at w3schools.com

So, if you have a P element inside a DIV, you can control the font color with, say,

DIV P.highlight { color: red; }

If you have a later CSS instruction, like

.highlight { color: green; }

then it will NOT override the earlier instruction. This has confused me greatly, especially when loading multiple CSS files and naively thinking that I could override the earlier CSS.

2
  • so how do you get around overwriting it in this case?
    – knarf
    Commented Oct 22, 2021 at 18:43
  • 1
    Im not an EXPERT on Specificity but it is generally true that a "specific" CSS instruction will not be over-ridden by a vaguer one. So, in my above example, if the first CSS instruction has executed in a style sheet, then only way to over-ride it is by an equally, or more specific CSS instruction. For example, #highlight { color: green; } or an inline style, like <P STYLE="color: blue;">text</P>
    – D.Gibson
    Commented Oct 24, 2021 at 9:56
1

I had a similar problem which was caused by a simple mistake in CSS comments.

I had written a comment using the JavaScript // way instead of /* */ which made the subsequent css-class to break but all other CSS work as expected.

1

Reasoning for my CSS styles not being applied, even though they were being loaded:

The media attribute on the link tag which was loading the stylesheet had an incorrect value. I had inadvertently set it to 1 instead of all. This meant the browser was ignoring those styles in that linked stylesheet.

Broken:

<link rel="stylesheet" type="text/css" href="css/style.css" media="1" />

Corrected:

<link rel="stylesheet" type="text/css" href="css/style.css" media="all" />
1

For me, it was the local overrides in Sources -> Overrides. A file gets saved locally whenever you change the styling of a page and chrome uses that file to override the server's css.

1

Clear the cache and cookies and restart the browser .As the style is not suppose to change frequently for a website browser kinda store it .

1

I also faced this issue. And this how it got resolved!

My css filename was gt.css. I was working on Visual Studio (eg.2017).

  • I went to solution explorer (press Ctrl+Alt+L) and searched gt.css (enter your css filename). Right click on your css filename and then on Bundler and Minifier (4th option curently) and then Re-Bundle file (or directly press Shift+Alt+F).
  • Save any unsaved file, then empty cache and hard reload your web browser.

You can learn more about Bundler and Minifier here.

1

I had custom styling applied only on some elements (rows of table). I use Bootstrap. This was caused by having "table-striped" class. Once removed, all required rows had the custom class applied correctly.

1

I'm too used to setting the className attribute in JSX with React, but not too used to setting the class attribute in plain old HTML. So my mistake when spinning up a quick CodePen was setting a classname attribute, which sets no actual class whatsoever in plain HTML. The correction was, of course, to give the element a class instead.

0

Hard reload your chorome Shift+F5

3
-1

Look at the spacing between selectors.

p span selects all span in p

span label selects all label in span

p span label selects all label in span in p

so label .fancify selects all .fancify in label

there is nothing of class fancify in label. label is on the same level, not above

so label.fancify

Not the answer you're looking for? Browse other questions tagged or ask your own question.