Below is an example of a box within a page,
Remember with CSS every element on a page is considered to be a box of which this would be just one.
| Left Border Left Margin Left Padding |
Page Top Margin
|
Right Border Right Margin Right Padding |
Think of the box as a single cell table.
Unless you use a Border or Background attribute you will not be able to see the box.
Here are a few examples.
This box is defined by using a DIV element.
I have made the box width 400 pixels, if the text is longer than the box width the height is adjusted so the box width stays the same.
I have now put a border round the box so you can see more clearly.
Here is the same box without the border but with a background colour
And now with padding.
Using position:absolute you can place your box with its contents anywhere on your page using any of the many effects that are available with css or scripts.
Note:
Internet Explorer adds the border and padding sizes to the inside of the box.
Netscape adds the border and padding sizes to the outside of the box.
Style definitions consist of three parts:
Together the above three are known as a rule.
<style>
selector{property:value}
</style>
Multiple selectors are specified on a separate line within the opening and closing <STYLE> tags, followed by the properties and values that are to be assigned to that selector.
<style>
selector{font-size:25px}
selector2{font-size:25px;color:red}
</style>
Standard HTML tags are used as simple selectors as opposed to the more complicated Generic and Contextual selectors.
For this example I am going to use the paragraph tag <P> as a (simple) selector.
So P is the selector
font-size: is the property
And 25px is the value
<style>
P{font-size:25px}
</style>
Note the opening { and closing } braces
This would make all text using the paragraph tag, <P>, 25 pixels in size
If you want to apply a different style to specific paragraphs using the above method would not be sufficient.
The option here would be to use a generic selector, this could then be used with any HTML tag even of the same type.
Class selectors allow you to apply different styles to HTML elements that use the same tag
Create a generic selector for the class alone which is applied to any HTML element carrying that class attribute.
To do this, using the above as an example, change the P for a name, such as enlarge.
Because this can be used with any tag a dot is placed before the selector name in the style definition.
<style>
.enlarge{font-size:25px}.
</style>
This text has now been enlarge and is achieved by typing the following;
Because I used a generic selector for the above display I was able to use the same selector with the <dir> tag that I used to display the "<P class=enlarge>" line.
<P class=enlarge>This text has now been enlarge and is achieved by typing the following;
<dir class=enlarge><P class=enlarge></dir>
The normal <P> tag is not effected now unless you include the "class=enlarge" selector.
I have given the simplest example using just the font-size attribute but there is a multitude of attributes that can be used giving you more control of your web page displays than you get from standard HTML.
If you want to use more than one attribute in a definition then you separate them with the semicolon ";"
So to enlarge a section of text and change its colour to red you would have something like this;
<style>
.bigred{font-size:25px;color:#ff0000}
</style>
To show the above display I typed:
To apply style definitions with greater precision you use Contextual Selectors.
This would apply a style only when set conditions are met and would eliminate the need to add Class attributes to all html elements.
This is achieved by using a particular HTML element as a selector and specifying that the style only be applied when that element is contained within another specific HTML element.
The definition would be;
<style>P A{color:blue}</style>
And the HTML would be;
<P><A href="#null">Link</a>
A simple selector can have different classes, thus allowing the same element to have different styles. For example, an author may wish to display code in a different color depending on its language:
code.html { color: #191970 }
code.css { color: #4b0082 }
The above example has created two classes, css and html for use with HTML's CODE element. The CLASS attribute is used in HTML to indicate the class of an element, e.g.,
<P CLASS=warning>Only one class is allowed per selector. For example, code.html.proprietary is invalid.</p>
Classes may also be declared without an associated element:
.note { font-size: small }
In this case, the note class may be used with any element.
A good practice is to name classes according to their function rather than their appearance. The note class in the above example could have been named small, but this name would become meaningless if the author decided to change the style of the class so that it no longer had a small font size.
ID selectors are individually assigned for the purpose of defining on a per-element basis. This selector type should only be used sparingly due to its inherent limitations. An ID selector is assigned by using the indicator "#" to precede a name. For example, an ID selector could be assigned as such:
#svp94O { text-indent: 3em }
This would be referenced in HTML by the ID attribute:
<P ID=svp94O>Text indented 3em</P>
Contextual selectors are merely strings of two or more simple selectors separated by white space. These selectors can be assigned normal properties and, due to the rules of cascading order, they will take precedence over simple selectors. For example, the contextual selector in
P EM { background: yellow }
is P EM. This rule says that emphasized text within a paragraph should have a yellow background; emphasized text in a heading would be unaffected.
A property is assigned to a selector in order to manipulate its style. Examples of properties include color, margin, and font.
The declaration value is an assignment that a property receives. For example, the property color could receive the value red.
In order to decrease repetitious statements within style sheets, grouping of selectors and declarations is allowed. For example, all of the headings in a document could be given identical declarations through a grouping:
H1, H2, H3, H4, H5, H6 {
color: red;
font-family: sans-serif }