Learning Intermediate CSS Faster Than Ever.

Today we will be expanding on our previous tutorial “Learning the basics of CSS faster than ever” into what areas we are permitted to write our CSS. There are three areas we are permitted to write CSS and these are.

Combining Rules -
Selectors -
Pseudo Class -

Inline

We talked about how to write inline styles in our last tutorial. We even discussed how even though it’s possible and sometime necessary, it’s not always wise to clutter you document elements with inline styles. Writing inline CSS styles or rather writing styles directly into the elements is not only going against what we are trying to accomplish with CSS, but the styles are only downloaded on a per-page basis.

This means that if you write all your document element styles inline then they are never truly being cached in a visitor’s browser. Each page will only load that much slower depending on how many styles you have in your documents elements.

More inline styles = more page loading time on a per-page basis

More inline styles = a step backwards in the CSS mind frame

You may be asking yourself “Well why then do we have the ability to write styles inline?” The answer to this is simple, sometimes you have to override a decelerated CSS rule from one of the two other areas we are going to be talking about.

You may be asking yourself “Why would I want to override a decelerated CSS rule?” Sometimes you run into a situation where you have a style rule that cascades down over ever page for a particular element or two. Then lets say on one particular page you might need to have these two elements be slightly different from the rest of the pages. You would then write these styles inline in order to take control of these two elements on this particular page. Because inline styles is that last reading point for our browsers all inline styles override any previous decelerated CSS rule. Hence inline styles per-page basis idealism.

So there is a need for inline styling, but for only special situations. Only use inline styling for special situations such as this and never abuse them.

Ok everyone clear why we have CSS inline styling and how not to abuse it? Perfect let’s move onto our header area.

Header

Every html document has a header (<head></head>) but did you know you can write CSS styles in your documents header? All you need is to open up a space where you would want to write your CSS with “style tags”.

<style type="text/css">
</style>

These are style tags and it’s a way to tell the browsers that this area contains CSS. Like I said your style tags belong in your header tags like so:

<head>
<style type="text/css">
</style>
</head>

There are two questions you should now be asking yourself about these style tags.

Are style tags just like inline styles in that they too are a per-page basis method?

Will the number of styles you have in your style tags also affect your pages loading time?

Answer to these two questions is yes. You may be wondering “What’s the difference between inline styling and writing your styles in the header?” At first it might be confusing about what the difference between inline style and style tags but actually there is one huge difference. In style tags you have the ability to use classes and id’s, where as inline styling you don’t.

Imagine with me if you will that again we have 50 images on a page and each image looks like this:

<img src="image.jpg" style="width:100px; height:100px border:#000 solid thin;" />

If we simply take these style out of all 50 images and replace them with:

<img src="image.jpg" class="myImages" />

We could then sum up all them styles into one class rule thats named “.myImages” in our style tags like so:

<style type="text/css">
.myImages {width:100px;  height:100px; border:#000 solid thin;}
</style>

See the big difference between writing inline style and writing in your documents header? With style tags you can begin to sum up all the inline styles into rules that can then be cascaded down this single document.

Again if you have a style that is being cascaded down your entire site but then have a few pages that you need to slightly look different from the rest. You could then write your style rules in your headers style tags and apply them to your elements accordingly instead of having to write so many inline styles.

Awesome… right?

Everyone clear that we use inline styles to overwrite existing declared style rules. Everyone understand that we use header style tags to help cut down inline style and to help our inline styles mission of overriding existing style rules? superb, let’s move onto our last permitted location of CSS.

External

I just used the words “Permitted location” and the keyword here is location. Our last area of permitted CSS is the external CSS file. Ah yes the external CSS file the one and true way to cascade rules over your entire site. Up till now you might have heard me mention how inline styling and style tags are for overriding an already declared CSS rule. These pre-existing rules I was talking about only come from the external CSS file. So the chain of command for who overrides what goes as follows.

External CSS file get downloaded first therefor gets overridden by both inline and style tags.

Style Tags rules get downloaded second and only get overridden by inline styles.

Inline Styles don’t get overridden by any of the above.

Perfect let’s take a look at what’s required to bring an external CSS file to our document. In each of our documents header that we wish to link our external CSS file too we must include a <link />
link tag. Here is a link tag for linking CSS files into a document.

<link href="css/styles.css" rel="stylesheet" type="text/css" />

This element has three important attributes. The first is “href” and this is the location of our CSS file on our web server. You can have a relative path to your CSS file like this:

href="../../css/styles.css"

Or an absolute path to your CSS file like this:

href="http://www.mywebsite.com/css/styles.css"

Ok the second attribute is the rel=”stylesheet” and this lets our browser know that it needs to download this style sheet first before the rest of our style.
Our last attribute tells the browser that this external file is of made up of text and CSS.
All three of these attributes are required to perform as correct external style sheet link.

Ok so let recap here.

Inline styles are to perform an override of external file css rules for a special situation.
Style tags are to help assist inline styles by allow us to sum up a great number of would be inline styles.
External CSS files is the one and only true way to cascade your site’s style over every page.

Well everyone I think that just about wraps it up for this lesson. I hope this helps everyone to grasp a little bit more on how to use CSS effectively.

Next we will be finishing up our series and discussing advance css.

Thanks everyone.

Devin R. Olsen

About Author:

I work as a full time frontend web developer at ISITE Design located in Portland Oregon. I like to teach, share and dabble deep into the digital dark arts of web and game development.

Follow Me:
TwitterFacebookGoogle Plus