Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

CSS cascading order

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
gudii9
Asked:
gudii9
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
ZadoCommented:
0
 
scrathcyboyCommented:


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!

 
ZadoCommented:
Please grant the points and close the question, thanks :-)
0
 
ZadoCommented:
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
 
gudii9Author Commented:
>>>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
 
scrathcyboyCommented:
"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
 
gudii9Author Commented:
-->- 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
 
scrathcyboyCommented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now