CSS Priority Problem - I can't override an external rule with an embedded rule

free web hosting
Free Web Hosting > Computers & Tech > Designing > Web Design and HTML

CSS Priority Problem - I can't override an external rule with an embedded rule

szupie
First of all, to let you see my code, I've uploaded two example files: testpri.html and style.css.

In my external CSS file, I applied a black background style to the li's in #test. Then I added another style in the HTML file, turning the background color of the #hello list item into red. The result isn't what I expected. The #test list item became black, instead of the red style I applied to it in the HTML file. Don't embedded styles have a higher priority than external styles? And don't specific styles have higher priority too? #hello is more specific than #test li, right? Then what am I doing wrong?

Reply

jlhaslip

Actually, I think the "#named_id element" has more specificity and will override the "#named_id". If the "li" was missing, then the embedded css would over-ride the stylesheet, but with the "li" the cascade gives it more points, so it is the one that is set. Try adding the "li" to the embedded style and then it should over-ride the external css.

Reply

szupie
How can I add li to the embedded style? #hello li does not do anything, because I'm trying to apply it to #hello. I'm only trying to change the style of the #head list item, and not anything else in the #test unordered list.

I've updated the listpri.html file. Now it includes more than one li's. I hope that makes it more clear.

Reply

jlhaslip
reference here: http://www.w3.org/TR/REC-CSS2/cascade.html#q1

try #test ul { background-color: #ffoooo;}

That should colour the ul red and the other selector should colour the li's black. Might be a problem showing the red because there is only the black li as content, so add some margin and/or padding to the ul to see if the red breaks through.

Reply

szupie
No, I'm not trying to turn the ul black. What I'm trying to do is to make every li black using the external style sheet, then turn only one li red using the embedded style. That way, I can have multiple pages that use the same external style sheet, but each have a different embedded style to make that page look different than other pages.

Is that confusing? blink.gif

Reply

mastercomputers
Here you go, I don't think I need to explain it, but if you want an explanation I can give one:

bgtest.html
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
    <title>Testing CSS</title>
    <link rel="stylesheet" type="text/css" href="bgtest.css" title="bgtest" />
    <style type="text/css">
    /*<![CDATA[*/
      li#listone { background-color: red !important; color: black !important; }
    /*]]>*/
    </style>
  </head>
  <body>
    <ul id="unordered">
      <li id="listone">First</li>
      <li id="listtwo">Second</li>
      <li>Third</li>
    </ul>
  </body>
</html>


bgtest.css
CODE
ul#unordered li { background-color: black; color: white; }


External styling has higher priority, because it's last to be loaded. Your document is loaded first, all elements, embedded and inline styling are done at this point, then the external styling is loaded.

In order of importance:

External
Inline
Embedded

Use !important to make it higher priority.


Cheers,


MC

 

 

 


Reply

szupie
Thanks, mastercomputers! I knew you would come to save me.
But I always thought external styles had the lowest priority. That's what I read in every CSS book and website. Weird.

EDIT: By the way, what does the ul in ul#unordered li do? Is it just good practice? Or does it prevent bugs in different browsers?

Reply

mastercomputers
External stylesheets do have the lowest priority and what you've read about CSS is correct, the problem is Firefox doesn't do cascade order correctly (which I was meant to refer to in the above) and I'm just so use to working with Firefox, that sometimes not relying on the browser to do what you want is better than relying on it. I never rely on browser default styling, every element I use within a document, I style how I want it to appear.

The reason why I do ul#unordered li is because it's more specific, anything that is more specific will have higher importances.

each element used is 1 point, each class is 10 points and each id is 100 points:

li = 1pt
ul li = 2pt
#unordered li = 101pt
ul#unordered li = 102pt
ul#unordered li#itemone = 202pt

So lets say I had

ul#unordered li { background-color: green; }
#unordered li { background-color: pink; }
ul#unordered li { background-color: blue; }

What background colour am I going to expect should show?

When an item has the same specifications, the last one that appeared would override the ones above it. We should expect background to be blue, if we took that out, we would expect green to show, even though the latter of styling for the same element does appear, the one using green is higher in importance because it's more specific.

Another reason is because it lets me know what type of element I'm working on and what that element is capable of doing, since not all elements accept every CSS styling.


Cheers,


MC

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. stylesheet priority override class id - 13.56 hr back. (1)
  2. css cascading "100 points" - 35.38 hr back. (1)
  3. priority in css - 37.19 hr back. (1)
  4. css priority - 27.13 hr back. (2)
  5. css priority inline external - 37.94 hr back. (1)
  6. set priority of css - 43.34 hr back. (1)
  7. css inline style priority - 43.54 hr back. (1)
  8. issue in background color using external css files in firefox - 47.99 hr back. (1)
  9. priority reg css - 61.93 hr back. (1)
  10. css class priority - 66.83 hr back. (1)
  11. css id class priority - 67.01 hr back. (1)
  12. border priority css - 71.09 hr back. (1)
  13. css cascade priority - 72.35 hr back. (1)
  14. css priority rules - 81.72 hr back. (1)
Similar Topics

Keywords : css, priority, problem, override, external, rule, embedded, rule

  1. Outsourcing A Style Section To An External Css File
    (7)
  2. Html W/ Embedded Flash
    How do I get rid of borders?? (8)
    Hey if any of you HTML wizzes out there could help me - I would greatly appreciate it. I am
    designing some web site stuff with flash only. I know I know I should be using dreamweaver or
    something else in addition - which I don't have access to on my computer here at work. When I
    publish my files I can't get rid of the border around my swf file when I view it in a browser.
    Is there any way to get rid of this $#%$ed border?? I have tried all the options I can
    find in flash to no avail. I'm thinking there probably is some simple HTML code to get ....

    1. Looking for css, priority, problem, override, external, rule, embedded, rule

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for css, priority, problem, override, external, rule, embedded, rule
advertisement




CSS Priority Problem - I can't override an external rule with an embedded rule



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE