Solved

CSS cascading order

Posted on 2010-11-29
9
289 Views
Last Modified: 2012-05-10
Hi,

I was reading as

Cascading order

What style will be used when there is more than one style specified for an HTML element?

Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority:

Browser default
External style sheet
Internal style sheet (in the head section)
Inline style (inside an HTML element)
So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style defined inside the <head> tag, or in an external style sheet, or in a browser (a default value).



I have not understood clearly what it means. I was not clear on browser default CSS as well. Please advise. Any links ideas resources sample code highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 200 total points
ID: 34237086
Browser defaults means the styles that the browser will apply to any elements where you don't specify a particular styles yourself.

As for the rest; CSS bascially works on a "most precise wins" rule. So, if you had something like the following:
<html>
<head>
    <style type="text/css">
          p { color: green; font-weight: bold; }
          p.MyParagraph { color:blue; }
    </style>
</head>
<body>
    <p class="MyParagraph" style="color:red;">
       Some text
    </p>
</body>
</html>

Open in new window

All three styles would be valid for the <P> element; however, the inline "color" style would win because it is the most specific. But the font-weight would also apply because it is not being overridden by a more precise style.
0
 
LVL 8

Assisted Solution

by:Zado
Zado earned 150 total points
ID: 34237640
0
 
LVL 44

Assisted Solution

by:scrathcyboy
scrathcyboy earned 150 total points
ID: 34245326


Cascading is an important concept in CSS style sheets.  The rule is this --

The Browser reads the CSS statements from first to last -- top to bottom.

If at the top of the CSS you state --

font-family: Arial; color: black, font-size: 12px

then later on you change the size to 14 pixels, the FONT TYPE will remain Arial, color black, but the size will be the LAST SIZE specified -- 14 px.

The way you use this cascading feature is to specify the main attributes in the CSS,  then if you want to change it just for one or two areas, you use an INLINE designation --

<TD style = "font-size:16px; color:blue;" >  this is bigger text in blue </TD>

The LAST statement is the one that takes effect in CSS, thus it is "cascading".  The family is still ARIAL, but you have changed the color and size JUST for that TD content.
0
 
LVL 8

Expert Comment

by:Zado
ID: 34317466
Please grant the points and close the question, thanks :-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:Zado
ID: 34384851
To be clear here: I'm not asking for grant the points to me, but generally.
If you have a problem with any suggestion from experts or nothing works with your expectation, please let us know, so we will continue work on solution for you, thanks.
0
 
LVL 7

Author Comment

by:gudii9
ID: 34461916
>>>he inline "color" style would win because it is the most specific.

how we can say inline color is most specific.

>>> p.MyParagraph { color:blue; } also seems to be specific to me. Please advise
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 34462238
"Most specific" is not the best way to express it.  See my comment above.  Cascading order depends on the first declaration of an element being overridden by the LAST specification of that same element.

so if you had p.MyParagraph { color:red; }  in the CSS at the top of the page, then you had

<P style = "color:blue;"> you paragraph </P>

INLINE in the paragraph itself, it is the LAST designation that takes effect (the last in the page read order) -- so it is not a matter of specific -- it is that the LAST designation overrides the earlier ones.
0
 
LVL 7

Author Comment

by:gudii9
ID: 34661106
-->- it is that the LAST designation overrides the earlier ones.

So all i need to take into consideration is last designated one right. Please clarify
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 34669929
YES !!

If you specify -- P { color:blue; }

then all the text in the paragraphs after that is blue.

If you LATER specify --  P {color:red; }

then all the text in the paragraphs AFTER that new specification are RED.

The HTML file is read by the browser IN SEQUENCE -- whatever comes part-way through the page changes the way items are displayed AFTER that designation.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now