Solved

CSS cascading order

Posted on 2010-11-29
9
288 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

22 Experts available now in Live!

Get 1:1 Help Now