?
Solved

CSS cascading order

Posted on 2010-11-29
9
Medium Priority
?
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 800 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 600 total points
ID: 34237640
0
 
LVL 44

Assisted Solution

by:scrathcyboy
scrathcyboy earned 600 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Zado
ID: 34317466
Please grant the points and close the question, thanks :-)
0
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
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…
Suggested Courses

764 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