Monday, November 21, 2005

 

Putting CSS on your webpage

Putting CSS on your page

There are 3 types of “Style sheets”, namely:

Inline
is placed into the <BODY> section along with the tags immediately before its desired effect on text. It is good for overriding the “main” SS in specific parts of the web page, BUT not good in terms of separating presentation from content or SEO, also the style attribute is depreciated in XHTML 1.1 and is completely gone from the current Working Draft of XHTML 2.0, … so use sparingly or better yet use ‘class’ (later in lessons) for deviating from your “main” CSS. Also it only works once on the tag, so has to be rewritten every time, greatly increasing the file size/download time of your webpage.

This may be what is already on your page, so you have been using CSS without realizing it, BUT what you want to do is isolate it from the HTML or content.

<body>

<h1 style="font: 10em">A big h1 heading for just this heading</h1>

<h1>back to normal h1 heading</h1>

<p>content</p>

</body>

Embedded
or internal, is placed in the <HEAD> section and applies to that one page only. It is placed between <style> and </style>tags – see below for an example of wording required. It can be used if you have a one page website, or if a particular page has a look different to the rest of your web pages.

Also it can be used if starting out with CSS then you can change CSS codes on the page and see it’s effects immediately without perhaps opening a CSS editor etc and tabbing between windows, later on when you are satisfied with the outcome and overall look to the page, change to external SS and so applying it to all pages to give a corporate/uniform look to all webpages.




<html>
<head>

<style type = “text/css”>
<!—
body { color : red ; background : yellow; }
p { font-family: "new century schoolbook”, courier, serif }
-->
</style>

</head>
<body>
<p>content</p>
</body>
</html>



Notes:

The <!—and --> are optional and hide the CSS code from older, non CSS compliant browsers.

In p above the 1st choice for font is NCS, 2nd is courier if NCS not available on users computer, final choice (if 1&2 unavailable) is a generic font which will be selected by users computer e.g. Times if available. It is good to finish with a generic font.


External
is stored in a text file on the server and called upon when the page/s downloads. Its beauty is that all pages can link to it and act upon it’s instructions, so even in a thousand page site one little change in external CSS will change all pages in your site. It is best to use a relative URL linking to a CSS file on your server so the file downloads quickly and sets the page out quickly. The CSS file you link to doesn’t have to be on your server, with a full URL you can link to anyone’s CSS style sheet (with their permission!) but it’s not recommended.




<html>
<head>

<link rel=”stylesheet” href=”filename.css” type=”text/css” />

</head>
<body>
<p>content</p>
</body>
</html>



filename.css is a plain text document, made with say Windows Notepad (or in a CSS editor, web page maker that handles CSS), containing only CSS commands, no HTML commands – no <html> no <body> etc.

If made in Notepad it is “saved as” filename with a “.css” extension instead of “.txt” so that the computer knows how to handle the file (like changing a file to a “.html” extension so the computer knows it is web page and uses a browser to open and read it), and uploaded as normal (on the root directory in the above example) to your server via FTP or a web interface etc.

It is then accessed by your webpage through the link command above and applied to your page before being displayed accordingly on the users computer screen.

An example of an external CSS that would be saved as “filename.css” and transferred to your root directory of your server if used in above href link:

body{color: red; background: yellow;}
p { font-family: “comic sans”, serif; text-indent: 1em; line-height: 1.33em;}
h1, h2, h3, h4, h5 {text-align: center; text-decoration: underline; letter-spacing: 6px; color: black;}

That’s it just the above … no links, no html tags in the filename.css file.

What the C in CSS means

It is called cascading as the latest rule overrides any earlier rules if conflicting, as they are read into the browser. Also if there are any conflicting commands, for example 2 equally specific element commands in an inline CSS, the latter one is applied.

For example, an external style sheet has these properties for the h3 selector:
h3 {color: red; text-align: centre; font-size: 1.2em}

And an internal embedded style sheet has these properties set for the h3 selector:
h3 {text-align: right; font-size: 1.5em}

And an in-line style in the body has <h3> set at 2em font size

the properties for h3, once read into the browser, will be:

color: red only defined in external SS
text-align: right internal SS overrides external as rendered on page
font-size: 2em inline SS overrides embedded as acted upon last


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?