Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS precedence order? My lecture slides are correct or not?

I've noticed that there are a couple of similar questions and answers at SO already, but let me clarify my specific question here first:

I've got lecture slides which states like this:

http://mindinscription.net/webapp/csstest/precedence.PNG

To be frank, I haven't heard of this rule of css precedence myself, and I googled to find something with similar topic but not quite like that : here

To have a test myself, I've made a test page on my own server here

After running it on FireFox 3.6.3, I am sure it does not show the way as it should be, according to the statement in lecture slides:

  • imported stylesheet ? am I doing it wrong? I cannot see its effect using FireBug
  • it says that embedded stylesheet has a higher precedence over linked/imported stylesheets, however, it doesn't work, if I put the linked/imported tag AFTER that.
  • inline style vs html attributes ? I've got an image where I firstly set its inline style to control the width and height, then use direct html attributes width/height to try modifying that, but failed...

Below is the source code :

<html>
<head>
    <style type="text/css">
        #target
        {
            border : 2px solid green;
            color  : green;
        }
    </style>
    <link rel="stylesheet" href="./linked.css" type="text/css" media="screen" />
</head>
<body>
    <div id="target">A targeted div tag on page.</div>

    <img src="cat.jpg" alt="" style="width : 102px; height : 110px;" width="204px" height="220px" />
</body>
</html>

Can any experienced CSS guys help me figure out if the slide is correct or not?

Frankly speaking, I am puzzled myself, as I can clearly see some other "incorrect" statements here and there amongst the slides, such as JavaScript is on client-side (how about server-side JavaScript?) and "Embedded styles are in the head section of a web page "(what the heck? I am not allowed to put it inside the body tag?)

Sorry about this silly question, the exam is on TOMORROW, and I now see a lot of things to think about :)

like image 834
Michael Mao Avatar asked Jun 18 '10 02:06

Michael Mao


People also ask

Which is the correct CSS Order of Precedence?

The location order of precedence is: browser default rules, external style sheet rules, embedded styles, and inline style rules.

What is the correct rules of precedence for CSS code?

Inline CSS has a higher priority than embedded and external CSS. So final Order is: Value defined as Important > Inline >id nesting > id > class nesting > class > tag nesting > tag.

Does order of CSS sheets matter?

CSS Order Matters In CSS, the order in which we specify our rules matters. If a rule from the same style sheet, with the same level of specificity exists, the rule that is declared last in the CSS document will be the one that is applied.

Does CSS execute top to bottom?

The browser loads from top to bottom. CSS files help the browser to layout the page. JS files, in many times, are larger files.


1 Answers

First, with imported stylesheets they mean stylesheets embedded using the @import rule.

Second, a few lines below that explanation in the CSS 2.1 spec there's an explanation of the cascading order. Other parts of the spec might be useful for your exam, too. Good luck.

Update: A bit of googling resulted in:

  • http://www.blooberry.com/indexdot/css/topics/cascade.htm
  • http://monc.se/kitchen/38/cascading-order-and-inheritance-in-css
  • http://www.boogiejack.com/CSS_4.html
  • http://www.communitymx.com/content/article.cfm?page=2&cid=2795D

etc.

like image 162
Marcel Korpel Avatar answered Nov 15 '22 04:11

Marcel Korpel