Solved

CSS cascading order

Posted on 2010-11-29
9
290 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How would I do a website like this? 5 56
HTML &#0153 2 17
Change div area and length 1 26
Expand HTML tables without having to click on it 16 41
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
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…

776 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