Archive for the ‘Css’ Category


How CSS works

Monday, April 14th, 2008

CSS is a new smarter way to apply style properties to HTML elements.

You can set all kinds of style properties, like border, font, background, spacing etc. (We’ll go into these in detail later.)

There are 3 main ways CSS styles can be applied:

  1. Inline with HTML
  2. On-page style definitions
  3. Separate style sheets

1. CSS Inline with HTML (use with caution)

You can write CSS directly into an HTML tag. In the example below, don’t worry about the specific styles for now.

Code

<div style=”background-color:#ff3; border:1px solid black; color:red; font-size:150%; padding:1em;”>I am a styled div!</div>

Looks like

I am a styled div!

This approach is very similar to the old-style inline HTML styling, and suffers from all the same problems.

It is only appropriate where the styling really is one-off. If there’s a possibility that you’ll use the same styling elsewhere, you really should use one of the alernative methods below, because it will save time and effort if you have to change your styling later.

On-page CSS definitions
A better way to write CSS is to define your styles once in the document (preferably in the document <head> section).

Code

<head>
<style type=”text/css”>
div {
background-color:#339;
color:#fff;
padding:15px;
border-bottom:5px solid red;
margin-bottom:15px;
}
</style>
</head>

<body>
<div>
I am affected by the definition above..
</div>
<div>
So am I, but the styles are only defined once.
</div>
</body>

Looks like

I am affected by the definition above..

So am I, but the styles are only defined once.

The benefit of this over the previous approach is that the style definitions are only written once. In this case, they’d apply to any <div> elements on the page.

Use this method when you want to apply similar styles to multiple elements on a page, but not on any other pages. To apply the same styles to things on multiple pages, you need to use method 3 below.


Separate style sheets

Monday, April 14th, 2008


The ideal way to define styles for your HTML elements is to put the definitions in a separate stylesheet document. Then, any page that includes the CSS file will inherit the same styles.

(Another benefit of this method is that it enables you to use different style sheets for different user agents .)

HTML pages can include as many CSS files as you need, and the styles will be combined together (according to inheritance and cascading rules ).

Code (2 different files):

my-styles.css

body {
background-color:#ccf;
letter-spacing:.1em;
}
p {
font-style:italic;
font-family:times,serif;
}

my-html.html

<head>
<link href=”my-styles.css” media=”screen” rel=”stylesheet” type=”text/css” />
</head>

<body>
<p>Here is some content in a paragraph.</p>
<p>Here is another paragraph.</p>
</body>

How it looks

Here is some content in a paragraph.
Here is another paragraph.

Note that, again, the styles defined once have been applied to more than one qualifying element. The difference between this method and the one above is that, because we’ve defined the styles in a separate stylesheet, all we need to do to apply those styles to another HTML page is include the same stylesheet reference.


How CSS styles apply to HTML elements

Monday, April 14th, 2008


There are a few basic rules that govern which (separately-defined) styles apply to what elements. Fortunately, these are relatively simple and logical, once explained.

1. Applying to HTML elements themselves

In CSS, if you want some styles to apply to HTML elements of a certain type, you write the HTML element type, followed by style definitions all contained between curly braces and separated with semicolons.

p {
font-weight:bold;
line-height:1.3em;
}

<p>The styles above will apply to me because I am a paragraph.</p>
<div>But they won’t apply to me.</div>

The code above will apply bold text and slightly increased line-height to the contents of any element of type <p> (i.e. paragraphs).

2. Applying to elements with class names

You can give any HTML element one or more class names. In CSS, these are defined with a preceding period (full-stop).

Styles derived from classnames (normally) override any of the same styles defined for the HTML element type (more on this later).

<style type=”text/css”>
.custom {
color:red;
}
</style>

<p class=”custom”>Applies to me.</p>
<p>But does not apply to me.</p>

This is extremely useful, because you can definte style characteristics under a single class, and then apply those characteristics to multiple elements by giving them that class (even if they’re different types of elements).

3. Elements can have multiple classes

HTML elements can have more than one class. Class names must be separated in the class property with spaces.

<style type=”text/css”>
.makered {
color:red;
}
.yellowbg {
background-color:#ff0;
}
</style>

<p class=”makered yellowbg”>Both styles will apply to me.</p>

4. Applying styles by id property

HTML elements can have id properties as well as class names.

While there can be multiple elements that share any class name, there should only be one element with a particular id property. This isn’t important in CSS, but it is important in DHTML, so good practice to follow.

Styles are associated with particular ids in CSS by prefixing the name with a hash/pound sign (#).

Another thing to remember is that properties inherited from an id will overwrite properties an element gets from its class (or from its HTML element type).

<style type=”text/css”>
#banner {
color:green;
}
.special {
color:red;
background-color:#ff9;
}
div {
color:blue;
font-weight:bold;
}
</style>

<div id=”banner” class=”special”>
This text would come out green, because the ID trumps the class and the element type.
</div>

Note that, in the example above, the font-weight:bold; and background-color:#ff9; styles still apply to the text, because it’s only the color: property that is overwritten by the superior style definition.

5. CSS styles apply to elements within elements, unless overwritten

Taking the above example…

<style type=”text/css”>
div {
color:green;
}
.special {
color:red;
background-color:#ff9;
}
#banner {
text-decoration:underline;
font-weight:bold;
color:blue;
}
</style>

<div>
  This text is only inside the div…
<p class=”special”>
    This text is inside the div and inside the paragraph…
<span id=”banner”>
This text is inside the div, paragraph with classname and span with id.
</span>
</p>
</div>

The first bit of text gets its green colour because it’s inside the div.

The second bit of text becomes red because it’s inside something with class=”special”, which overrides the previous color property. It also gets the additional background-color.

The third bit of text is inside all three elements, but the properties it gets from its id overrule any other conflicting style properties it gets from the other elements. Note that it has also inherited the background-color from the special paragraph, because the id’s styles don’t specify a background.


Problems in the old way to style content

Monday, April 14th, 2008

In the olden days, when you wanted to change the font of your HTML, you would have to use a <font> tag, like <font face=”Times”>…

If you wanted to set the background colour and border width of a table and the amount of padding in each cell, you’d put <table border=”1″ bgcolor=”silver” cellpadding=”3″ cellspacing=”0″>

There were a few serious problems with this type of approach:

  1. Because your styles are embedded into your content, it’s laborious to write.
  2. You have to repeat the same styles over and over again, wherever they appear on a page and across your whole site.
  3. It’s also lots of work to change the style of your site, as each page and every piece of styling has to be edited one-by-one.
  4. It adds extra size to every file.
  5. The same styles are seen by everyone, no matter whether they’re viewing on a monitor, listening to the content, browsing on a PDA, or printing the content.

There are lots of advantages of working with html by keeping separate css file , including:

  1. Reduced filesize - each CSS definition is written only once
  2. Reduced bandwidth - web browsers will remember (cache) the contents of CSS file, so don’t need to download the file again with each new page that uses it
  3. Easier updating - you only have to change styles in one place for them to change site-wide
  4. Separates the work of styling and creating content - you can create all your HTML pages first, applying sensible semantic markup, class names and IDs, and then do all your design work later on your style sheets.


A brief CSS 2.1 tutorial for HTML

Monday, April 14th, 2008

This section is non-normative.

In this tutorial, we show how easy it can be to design simple style sheets. For this tutorial, you will need to know a little HTML and some basic desktop publishing terminology.

We begin with a small HTML document:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
  <HEAD>
  <TITLE>Bach's home page</TITLE>
  </HEAD>
  <BODY>
    <H1>Bach's home page</H1>
    <P>Johann Sebastian Bach was a prolific composer.
  </BODY>
</HTML>

To set the text color of the H1 elements to red, you can write the following CSS rules:

  h1 { color: red }

A CSS rule consists of two main parts: selector(’h1′) and declaration (’color: red’). In HTML, element names are case-insensitive so ‘h1′ works just as well as ‘H1′. The declaration has two parts: property (’color’) and value (’red’). While the example above tries to influence only one of the properties needed for rendering an HTML document, it qualifies as a style sheet on its own. Combined with other style sheets (one fundamental feature of CSS is that style sheets are combined) it will determine the final presentation of the document.

The HTML 4 specification defines how style sheet rules may be specified for HTML documents: either within the HTML document, or via an external style sheet. To put the style sheet into the document, use the STYLE element:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
  <HEAD>
  <TITLE>Bach's home page</TITLE>
  <STYLE type="text/css">
    h1 { color: red }
  </STYLE>
  </HEAD>
  <BODY>
    <H1>Bach's home page</H1>
    <P>Johann Sebastian Bach was a prolific composer.
  </BODY>
</HTML>

For maximum flexibility, we recommend that authors specify external style sheets; they may be changed without modifying the source HTML document, and they may be shared among several documents. To link to an external style sheet, you can use the LINK element:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
  <HEAD>
  <TITLE>Bach's home page</TITLE>
  <LINK rel="stylesheet" href="bach.css" type="text/css">
  </HEAD>
  <BODY>
    <H1>Bach's home page</H1>
    <P>Johann Sebastian Bach was a prolific composer.
  </BODY>
</HTML>

The LINK element specifies:

  • the type of link: to a “stylesheet”.
  • the location of the style sheet via the “href” attribute.
  • the type of style sheet being linked: “text/css”.

To show the close relationship between a style sheet and the structured markup, we continue to use the STYLE element in this tutorial. Let’s add more colors:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
  <HEAD>
  <TITLE>Bach's home page</TITLE>
  <STYLE type="text/css">
    body { color: black; background: white }
    h1 { color: red; background: white }
  </STYLE>
  </HEAD>
  <BODY>
    <H1>Bach's home page</H1>
    <P>Johann Sebastian Bach was a prolific composer.
  </BODY>
</HTML>

The style sheet now contains four rules: the first two set the color and background of the BODY element (it’s a good idea to set the text color and background color together), while the last two set the color and the background of the H1 element. Since no color has been specified for the P element, it will inherit the color from its parent element, namely BODY. The H1 element is also a child element of BODY but the second rule overrides the inherited value. In CSS there are often such conflicts between different values, and this specification describes how to resolve them.

CSS 2.1 has more than 90 properties, including ‘color’. Let’s look at some of the others:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
  <HEAD>
  <TITLE>Bach's home page</TITLE>
  <STYLE type="text/css">
    body {
      font-family: "Gill Sans", sans-serif;
      font-size: 12pt;
      margin: 3em;
    }
  </STYLE>
  </HEAD>
  <BODY>
    <H1>Bach's home page</H1>
    <P>Johann Sebastian Bach was a prolific composer.
  </BODY>
</HTML>

The first thing to notice is that several declarations are grouped within a block enclosed by curly braces ({…}), and separated by semicolons, though the last declaration may also be followed by a semicolon.

The first declaration on the BODY element sets the font family to “Gill Sans”. If that font isn’t available, the user agent (often referred to as a “browser”) will use the ’sans-serif’ font family which is one of five generic font families which all users agents know. Child elements of BODY will inherit the value of the ‘font-family’ property.

The second declaration sets the font size of the BODY element to 12 points. The “point” unit is commonly used in print-based typography to indicate font sizes and other length values. It’s an example of an absolute unit which does not scale relative to the environment.

The third declaration uses a relative unit which scales with regard to its surroundings. The “em” unit refers to the font size of the element. In this case the result is that the margins around the BODY element are three times wider than the font size.