HOME   HELPFUL HINTS HOME

CSS- Cascading Style Sheets

Where the styling goes

file: hh4-css-where.htm

My grasp of CSS is severely limited.

But after struggling for a while, I've grasped a few things.

The way I do things may be crude. It may not be best practice. But sometimes seeing the fumblings of another newbie can be more useful than trying to see what is happening in something sophisticated.




Before the main essay, a summary of the results...

CSS/ Styling/ Style sheets

"CSS" stands for "Cascading Style Sheets".

"Styling" is a word you don't often see. Perhaps it ought to used more often? Style sheets are important, but the style of what is on your reader's screen can be set several ways, not all of them requiring a "sheet".

The color of text, the font family and size, etc are matters of style. Style also governs how things are arranged on the page. The color of the background. Etc! Start with color. As you learn to control that, you'll find other things to tweak to improve the usefulness of your pages.

The most obvious thing that can affect the style of what your reader sees it the external style sheet. This is the thing that is most aptly named a "style sheet" It is a file on your server. The rules in it are, as it were, incorporated by reference to them. That reference, or link, goes in the <head>... </head> block of the page to be styled.

More on style sheets, in that narrow sense, below.

Styling whole chunks of a page... or just bits of one

I learned how to, say, turn a whole paragraph green- and- bold fairly early on.

I struggled for ages with how to set just a part of a paragraph to what I wanted. (It is frustratingly easy, when you know how! Read on, or view this page's source to see how I did those things).

But the real magic is not in those procedures, per say, but in learning to apply your wishes just once, or once in a while, or across a whole page. For instance, I often write about programming, and wanted to have actual code displayed in a style which set it apart from the rest of the text in the page. If I used the simple "in line" styling to do this, and then wanted, say, to change the color used for code, it would be a pain to make that change. (I was using in line styling a moment ago.) With either embedded or external styling making such a change would be easy.

External/ Embedded/ In Line

If I want to use "#00ff00" for the color for something... it gives the green you saw a moment ago... then it can go in three places!

You can specify the color (or any other styling) with...

... styling.

The first two require things in the <head>... </head> block of your html.

-------
EXTERNAL style sheets: These, as I said, are separate files held on your server. To use one such file, if it were called "sg-19216.css", and held in the folder indicated, you would put the following would go in the <head>... </head> block of your html...

<link href='http://sheepdogguides.com/css/sg-19216.css'
   rel="stylesheet" type="text/css" media="all">

If it were in the same folder as the page using what is in it, it could be referenced as follows...

<link href='sg-19216.css'
   rel="stylesheet" type="text/css" media="all">

... and other ordinary variations on the "where is the file" theme are allowed.

What goes in the stylesheet is covered below.

-------
EMBEDDED styling: The following would go in the <head>... </head> block of your html...

<style type="text/css">
.ex1 {background-color:#ffff00;}
.ex2 {background-color:#ffccff;}
</style>

That would permit enclosing things in <div class=ex1>...</div>

... and it would override any width specification already in class "ex1", or add a width specification, if there weren't one already from an external style sheet.

You could also set, say, a paragraph to class .ex1. (Or .ex2, etc. )

-------
Or styling may be IN-LINE. The following would go in the <body>... </body> block of your html...

<div class=ex1
style="width:50%;">

... and it would override any width specification already in class "ex1", or add a width specification, if there weren't one already.

Note the magic! We can, say, set the overall color for several web pages with an external style sheet. Then use embedded styling to change the color of some of the pages in the set. And then at an even lower level, change the styling of some parts of any page with in line styling. This is where the "cascading" comes from in the name "CSS".





And now for main content of this page...



Cascading STYLE sheets

If there is any justice, and for once, there is some, the name should give you a hint.

Cascading style sheets (and things like them) affect the style of what you see on a web page.

Many things come under the category "style". What the text on the page looks like, and how it is arranged being two of them.

But all the things you can do with style are not going to be the focus of this essay. This essay is about how you get styling into your pages in the first place.

This essay is going to cover some basics of where you "put stuff" to achieve the styling that CSS allows.




A really simple web page

The following is about the simplest "proper" web page you can do... but it does include the core elements of any web page, plus the traditional "Hello world"...

<html>
  <head>
  </head>

  <body>
    <p>
    Hello world. This is a pretty simple page.
    </p>
  </body>
</html>

At first glance, you might think there was no styling active in that, but you'd be wrong.

For example, everything between the p and /p tags will be presented in the default style for "p" text.

But styling can be a whole lot fancier than that!

You are not required to indent lines the way I did.



Let's make it a green world.

To have that page produce...

Hello green world.

... I changed the "Hello world" to...

Hello <span style="color:#00ff00;">green</span>world.

This is inline styling. And you can "do things" to the font, the font size, the margins... etc, etc! But we'll simply make changes to color for a while, so you can concentrate on where the styling stuff goes.

Now... what if we wanted a whole block of stuff green? Easy.

Here's a bunch of stuff,
all in green.

That was achieved with...

<div style="color:#00ff00;">
<p>Here's a bunch of stuff,<br>
all in green.</p>
</div>

(We're still using inline styling.)

The enclosing tags are now "div", not "span"... but the way we archived the green has not changed.

We're going to come back to this later. First...


Embedded Styling

What if you wanted an essay to have paragraphs in red alternating with paragraphs in green? You have the tools to do it. But doing it the only way you know so far would be tedious. And deciding to change the red to blue would be really tedious.

Happily, there's an answer.

We can do this.

It really isn't nearly as hard as you might fear, and changing the colors after the page is "done" can be done too....

Once you know how.

-------
In our Really Basic web page template, there is the <head>...</head> section.

In that, for a clever way to have the two paragraph style options, we need....

  <head>
    <style type="text/css">
     .ParaFirstColor {color:#ff0000;}
     .ParaOtherColor {color:#00ff00;}
    </style>
  </head>

That creates two "classes"... "ParaFirstColor" and "ParaOtherColor". (Note that in the code, we need a "." in front of each class name. But also note that how we do the coloring is pretty much as before.)

...AND to get the result just demonstrated, we start with....

<p>Happily, there's an answer.</p>
<p>We can do this.</p>
<p>It really isn't nearly as hard...</p>
<p>Once you know how.</p>

... and we just add some bits...

<p class=ParaFirstColor>Happily, there's an answer.</p>
<p class=ParaOtherColor>We can do this.</p>
<p class=ParaFirstColor>It really isn't nearly as hard...</p>
<p class=ParaOtherColor>Once you know how.</p>

External Styling

Here's another option... and don't be dismayed... these multiple options are Good News!!

In the same folder as the html we have been working with, create a small file, as follows...

/*This is a comment
External style sheet created 16 Feb 19*/
.ParaFirstColor {color:#ff0000;}
.ParaOtherColor {color:#00ff00;}

Look familiar? It should!

Save it as MyFirstExternalCSS.cs. When I say "save it", I mean save just the four lines you see above. That's all it takes!

(Actually, it can be in any folder, but having it in the same folder makes writing this easier... assuming that the page you are going to USE the stylesheet in is in the same folder.)

In the head.../head section of your webpage, put...

<link href='http://MyFirstExternalCSS.cs'
   rel="stylesheet" type="text/css" media="all">

Once that is in place, the....

  <head>
    <style type="text/css">
     .ParaFirstColor {color:#ff0000;}
     .ParaOtherColor {color:#00ff00;}
    </style>
  </head>

.. which, previously, was in the head.../head section can be REMOVED! (Removed from the page the styling is being used in.) (For now, just "rem it out" with HTML's usual "comment" tags: <--, closed by -->) The page will still know how to present text the "ParaFirstColor" way... because it now "sees" the definition in the external style sheet.


Why bother with external style sheets?

For a single page- no reason.

But what if you had, say, five pages, and you wanted their appearance to match a common theme? Using the external style sheet makes the ParaFirstColor style available to all of them.

Now the advantage of an external style sheet becomes clear, I hope? Not only is creating a set of pages in a consistent style easier, but imagine if, subsequently, you decide the color you've used for preformatted text isn't dark enough? One change to the external style sheet, and, "bingo", ALL the pages using that style have darker text!


But- you can have your cake, and eat it too...

Suppose you were using the external style sheet explained above. And were using it with 5 webpages. But then you wanted one of them to use a different color? (The example is strained, but you will want to do analogous things.)

This is easy to arrange... just put an EMBEDDED style in the sheet that you want different... but put it AFTER the...

<link href="MyFirstExternalCSS.cs"
   rel="stylesheet" type="text/css" media="all">

Not only can embedded stylesheets be used to override external stylesheets, but, by the same logic, inline styling will override both of them.

If, in addition to a color for pages generally, changed for just a few or them, you wanted just one paragraph different from either of the colors in play so far, you would use the inline styling that we started this essay with...

<p class=ParaOtherColor style="color:#000000;">We can do this.</p>

That would, in a sense, make a browser go through four stages before "deciding" what color to use for the text.

It would start with the default text color. Then it would "think" it should use the color specified in the external stylesheet. But no, that is being over-ridden by the internal styling... and lastly, for this line, THAT is being overridden by the inline styling!

Now- that example is a little extreme. However, you can determining many things with styling. And in many cases, the "layers" (cascades) of determining the color, etc, will be more sensible.

CSS gives you superb granularity.

I hope that why it is called CASCADING styles is more evident to you now?

Suppose your external stylesheet only said that the text should be green. Fine. (As long as you wanted that sort of text green in most of the pages that will have it specified as one of the page's external style sheets.)

But! A page can have many external stylesheets.

(Remember that the order of everything matters. Which color setting happens last is the one that will matter... whether the setting is made in one (of perhaps several) external stylesheets, or it happens because of some embedded styling... or in an in-line styling code.

But further suppose you wanted SOME of the pages to make ParaFirstColor paragraphs italic, as well.

Styling is cumulative.

.ParaFirstColor (perhaps a bad name... .ParaFirstStyle might have been better) says make the color green.

If somewhere else, after saying the color should be green, you say...

.ParaFirstColor {font-style:italic;}

... you will make the font italic.

Adding the italic doesn't interfere with the color setting.

Pretty cool

The whole thing... the "cascading" aspect, the things you can do, etc... is pretty cool once you can get your head around it. It took me several years. It's one of those things that grows on you slowly, if you are struggling down the self-taught route. A bit like riding a bicycle. You're told how. Pump the pedals. Steer with the handlebars. Fine. You scrape knees for a while. And then suddenly, you can just do it. But you had to scrape the knees for a bit. And you can't isolate any "but you didn't tell me.." element.

With something like cascading style sheets, start simple. Try things. Add more things. Use the W3.org validator. (Run that on a page CSS you've put CSS into. The validator will check your page and any external sheets called in by your page.)

Keep doing more Google searches. I find the W3Schools pages particularly helpful. (You might want to consider using the W3Schools "omnibus" external stylesheet! (Why re-invent the wheel, when someone with expertise and a good reputation is inviting to use theirs for free? Do something the easy way? (I read about that once.)) Or even use one of the free W3Schools site templates?

Even if you read another essay about "the same thing", you're reading it explained differently, and that may help you catch the bit you mis-understood previously. The computer is always right. It can't read your mind. Doesn't know what you think it should do. It only knows what it does do for a given command. And the command has to be expressed correctly.


What's missing?

There's something missing! Alas, it is also missing from my grasp of styling.

I've used "class=" to apply styling specified in external or embedded styling. And dots before the class name when I was defining them.

This may be pretty pedestrian. I have encountered hints that we can be more clever. It may, for instance, be possible to set TWO "style xyz"s, one for paragraphs, one for H2 headings, and use them thus...

<h2.xyz>Heading in "xyz" HEADING 2 style.</h2>

<p.xyz>Paragraph in "xyz" PARAGRAPH style.</p>

That, however, in a phrase I like from the St. James bible, is "a thing to high for me"... for the moment. I see its charms. I aspire to the skills.

For now, I will content myself with using the following. It works!

<h2 class=xyzH2>Heading in "xyzH2" style.</h2>
<p class=xyzP>Paragraph in "xyzP" style.</p>

I've shown you how you can do that in the essay above.