Set - 5

Question 1 :

How do I centre my page?

Answer :

This is very easy. If we take the code in the last question and change it to this :
CSS
body, html {
height:100%;
}
body {
margin:0;
padding:0;
}
#wrap {
position:relative;
width:780px;
margin:auto; min-height:100%;
}
* html #wrap {
height:100%;
}

you get a page that fits an 800x600 resolution screen without a horizontal scrollbar, which will be centered at higher resolutions.


Question 2 :

Which style specification method should be used? Why?

Answer :

The answer to this one is tricky. The short answer is: "it depends." The long answer is, however, another story.

If you are planning on using more than one style specification method in your document, you must also worry about Cascading Order of Style methods (see question 11.) If you are going to use only one method, then some guidelines about the nature of each method need to be kept in mind. The answer to this question is also very much related to the advantages and disadvantages to using each of them (next question.) 

Method 1: External Style Sheets (The LINK [-->Index DOT Html] element)
This method should be used if you want to apply the same style to multiple documents. Each document can reference the stand-alone style sheet and use the styles contained within. Using this method, the appearance of many documents can be controlled using a single or small number of style sheets. This can save a LOT of time for an author. 

Method 2: Embedded Style Sheets (The Style [-->Index DOT Html] element)
The syntax used with Method 2 is the same as that for Method 1. This method is a happy medium between External Style Sheets and Inline Styles (see below.). It should be used in place of Method 1 if you only want to specify styles for a single document. This method should also be used when you want to specify a style for multiple tag types at once or the list of style definitions is of larger size. Method 3: Inline Styles (STYLE attribute to HTML elements)

If you only have to apply style to one or a few elements in a single document, your best bet will often be an Inline Style. This method attaches a style definition within the HTML element it is modifying.


Question 3 :

Justified Text?

Answer :

You redefine the <p> tag like:

p {text-align: justify;}

and that renders all <p>s with justified text.

Another possibility is to define a class, like:

.just {text-align: justify;}

and then you style the paragraphs in question like:

<td class="just">text </td>

Note that NN 4.xx has problems with the inheritance of styles; that some NN4.xx browsers have a funny way to interpret "justify"; and that you have to have at least one blank space between the last character and the </p> tag, because otherwise NN 4.xx likes to justify even a three word half-sentence... also, some browsers do a pretty ugly job of rendering justified text, adding spaces between words, instead of spaces between letters, as with word-processing programs.


Question 4 :

Colored Horizontal Rule?

Answer :

You can apply styles to Horizontal Rules <HR> in IE without problems, but NN4.xx can only render the silvery HR. But there is a way around it:

.rule {border-top-width: 1px;
border-top-style: solid;
border-color: #FF0000; 
margin: 0px 2%;}

that, applied to a div, should give you a red HR in NN4.xx and IE, with a 2% gap on the left and right side.

CSSharky Logo
On this page is an Example of a coloured 'Horizontal Rule'.
Update:
Thanks to Matt Del Vecchio here is an improved format for the Horizontal Rule:

hr { height:0px; 
border:0px; 
border-top:1px solid #ff1493; }

....this works in both IE and Netscape. It tells the browser to not render the hr rule itself, and then sets a 1px border, which looks just how most folks want to render the hr rule. It uses the <hr> element and that is better than writing your own class as all devices will know what to do with an <hr> tag.


Question 5 :

Do URL's have quotes or not?

Answer :

Double or single quotes in URLs are optional. The tree following examples are equally valid:

BODY {background: url(pics/wave.png) blue}
BODY {background: url("pics/wave.png") blue}
BODY {background: url('pics/wave.png') blue}


Question 6 :

To what are partial URLs relative? 

Answer :

Partial URLs are relative to the source of the style sheet. The style sheet source can either be linked or embedded. To which source partial URLs are relative to depends on their occurrence. 
If a partial URL occurs in a linked style sheet then it is relative to the linked style sheet. The URL of the linked style sheet is the URL of the directory where the sheet is kept. 
If a partial URL occurs in an embedded style sheet then it is relative to the embedded style sheet. The URL of the embedded style sheet is the URL of the HTML document in which the sheet is embedded. 
Note that Navigator 4.x treats partial URLs as being relative to the HTML document, regardless of the place where the partial URL occurs. This is a serious bug which forces most authors to use absolute URLs in their CSS.


Question 7 :

What's the difference between 'class' and 'id'?

Answer :

As a person, you may have an ID card - a passport, a driving license or whatever - which identifies you as a unique individual. It's the same with CSS. If you want to apply style to one element use 'id' (e.g. <div id="myid">). In the stylesheet, you identify an 'id' with a '#' ie. '#myid'... 
As a person, if you are in a class, you are one of many. It's the same with CSS. If you want to apply the same style to more than one element, use 'class' (e.g. <div class="myclass">). In the stylesheet, you identify a 'class' with a '.' ie. '.myclass'... 
If id's are more restrictive than classes, then why not just litter your page with classes? Well, I think the main thing is that it's simply wrong. You don't put headings in 'p' tags - you use 'h1', 'h2', etc. You don't (or shouldn't) make a list by writing asterisks or the little divider bar ( | ) - you use list tags ('ol'/'ul' + 'li') . You don't say that your footer is part of a class of elements called 'footer' - that's just stupid - you can't have more than one footer - it can't be a class. Of course, practically, the effect is about the same - the rules are applied - but that's not the point - it's semantically wrong to do it that way... However, if you try to give more than one element the same id, you will have problems - so don't do it. 
An element may have an id and a class, but that's usually not necessary. You can also give an element two classes if you need to - like this : class="class1 class2". It can be very useful. Needless to say, you can't give an element two id's. 
Another difference is to do with power. You can give an element an id and a class, but if any of the properties of the two conflict, the id style will win. Ids are more powerful than classes. 
One more useful thing about id's is that they can be used as a link reference. Many people still think that you need named anchors to make links within a page, but that's simply not true - in fact, the name attribute is deprecated in XHTML except for in forms. One example of using id's as link references is this page. There are no named anchors on this page - the questions at the top of the page link to the id's of the divs that the answers are in.


Question 8 :

I made a 10px-high div, but IE makes it 20px high... 

Answer :

Yeah 
This problem sometimes comes up when you make a div just to contain the bottom border of a box, or something like that. In this situation, there's no text in the div, but IE won't let the height of the div be smaller than the line-height (which usually depends on the font-size). The answer is to set the font-size to zero.
CSS

#thediv {
    font-size:0;
}


Question 9 :

Why is it my ':hover' declaration for links does not work ?

Answer :

Assuming you have already checked that your style sheet declarations do conform to correct CSS syntax, it could be that you have overlooked the importance of a correct order of style declarations for links. 
The CSS2 specification makes this following note on the importance of placing the dynamic pseudo-classes ':hover' and ':active' in correct positions in a list of style declarations. 
Note that the 'a:hover' must be placed after the 'a:link' and 'a:visited' rules, since otherwise the cascading rules will hide the 'color' property of the 'a:hover' rule.

Similarly, because 'a:active' is placed after 'a:hover', the active color will apply when the user both activates and hovers over the 'a' element.


Question 10 :

Why does Netscape lose my styles ?

Answer :

Netscape 4.x has poor support for CSS. Having said that, the following points should be noted. 
Invalid HTML will almost certainly cause Netscape to ignore your CSS suggestions at some point. You will find that valid HTML is your best friend, but for Netscape to work properly you must ensure that all elements in your markup which permit closing tags are explicitly closed. 
Check and correct your CSS suggestions for the very same reason, Netscape 4.x is in fact doing "the right thing", as per CSS specs (as opposed to MSIE) when it ignores style rules with errors. 
Netscape 4.x has what's called an "inheritance problem" into its TABLE element. It can be argued that NS is all within its right to behave as it does in this case, but since the workaround is quite simple it's easy enough to just use it and be done with it. 
Let's say you want your TABLE content to "look the same" as your BODY content? "Redundant" styling comes to your help as in e.g. BODY, TABLE, TH, TD { /* insert your styles here */ } 
On a generic level, Netscape 4.x likes to have style rules applied directly to the elements where they are needed. You can never really trust the inheritance principle to work correctly at any level in Netscape 4.x.


Question 11 :

Which font names are available on all platforms ?

Answer :

The simple answer is "None" which is why CSS offers five generic font names as 'serif', 'sans-serif', 'cursive', 'fantasy' and 'monospace'. Never put any of these generic font names in quotes.

A CSS aware browser should make a suitable choice from the available fonts in response to each of those generic names. 
Specifying any other font name in a www environment comes out as a suggestion only, that may or may not be acknowledged by a browser. 
The problem with using names of specific fonts is that there is little point in naming fonts that few users will have, so you're down to listing a few mass-market font names. This will then override any superior selection that a minority of discerning readers may have made for themselves. 
Note also that fonts may differ in their character repertoire, but this is often not evident from the font name itself: by selecting an inappropriate font name, you might prevent internationalized content from displaying correctly for a proportion of users.


Question 12 :

What is Extensible Stylesheet Language (XSL)?

Answer :

XSL is a proposed styling language for formatting XML (eXtensible Markup Language) documents. The proposal was submitted to the W3C by Microsoft, Inso, and ArborText.


Question 13 :

Document Style Semantics and Specification Language (DSSSL)? 

Answer :

Document Style Semantics and Specification Language is an international standard, an expression language, a styling language for associating processing (formatting and transformation) with SGML documents, for example XML.


Question 14 :

How do I place two paragraphs next to each other? 

Answer :

There are several ways to accomplish this effect, although each has its own benefits and drawbacks. We start with the simplest method of positioning two paragraphs next to each other.

<DIV style="float: left; width: 50%">Paragraph 1</DIV>
<DIV style="float: left; width: 50%">Paragraph 2</DIV>

Trickier is this example, which relies on positioning but does not suffer the vertical-overlap problems which plague many other positioning solutions. The problem is that it relies on an incorrect positioning implementation, and will break down dramatically in conformant browsers.

<P>
<SPAN STYLE="position: relative; left: 50%; width: 50%">
<SPAN STYLE="position: absolute; left: -100%; width: 100%">
Paragraph 1</SPAN>
Paragraph 2</SPAN>
</P>

If floating is not sufficient to your purposes, or you cannot accept display variances in older browsers, then it may be best to fall back to table-based solutions.


Question 15 :

I want my page fonts to look the same everywhere as in&hellip;

a) Why are my font sizes different in different browsers ?
b) Why are my font sizes different on different platforms ?

Answer :

These questions represent the tip of the iceberg of a large topic about which whole essays have been written and a wide range of different views are held. 
The WWW was originally devised to present the same content in different presentation situations and for a wide range of readers: on that basis, "looking the same" is not a design criterion, indeed different presentations would be expected to look different. 
Some would have it that this original aim is no longer relevant, and that the purpose of web design is now to factor out the differences between display situations and put the author in control of the details of the presentation. Others point out that CSS was designed to give the reader a substantial amount of joint control over this process, and that this is desirable, for example to accommodate users with different visual acuity. 
Reading of textual matter on a computer screen is quite a delicate business, what with the relatively coarse pixel structure of a computer display; even with a close knowledge of the display details, it isn't possible to achieve the detailed control that would be possible, say, on a printer. Whatever one's aims, the practical truth is that many of the efforts made to guarantee the precise result on the screen have seriously counterproductive side effects in a www situation. 
The CSS specifications themselves recommend that authors should not use absolute size units in a situation where the properties of the display are unknown. There's a lot to be said for flexible design, that in an appropriate situation looks the way you had in mind, but still successfully conveys content and message in a wide range of other browsing situations. 
And so, before looking at the technical detail of what can be specified, it's strongly suggested that you read some of those essays on web design, and reach your own conclusions as to the strengths and weaknesses of the medium, and how you can best exploit the strengths in a web environment, without falling foul of the weaknesses.


Question 16 :

When is auto different from 0 in margin properties? 

Answer :

In vertical margins, auto is always equal to 0. In horizontal margins, auto is only equal to 0 if the width property is also auto. Here are three examples, assume that there is a <P> that is a child of<BODY>:

Example 1: auto value on the width.

BODY {width: 30em;}
P {width: auto; margin-left: auto; margin-right: auto;}

Since the width property is auto, the auto values of the two margins will be ignored. The result is a P that is 30em wide, with no margins.

Example 2: two auto margins

BODY {width: 30em;}
P {width: 20em; margin-left: auto; margin-right: auto;}

The P will be 20em wide and the remaining 10em will be divided between the two margins. Paragraphs will be indented 5em at both sides.

Example 3: one auto margin

BODY {width: 30em;}
P {width: 20em; margin-left: 2em; margin-right: auto;}

In this case, paragraphs are 20em wide and are indented 2em on the left side. Since the total width available is 30em, that means the right margin will be 8em. 
Note that the default value of width is auto, so setting one or both margins to auto is only useful if you set the width to something other than auto at the same time.


Question 17 :

How does a simple CSS style rule look ?

Answer :

P { font-family: serif; font-size: 1.2em; }
Here we see a rule with a 'selector' P that has been given two style declarations, i.e. two 'property:value' pairs.
'font-family' and 'font-size' are properties of the content of element P , and these properties are assigned the values of 'serif' and '1.2em' respectively.
A colon ':' is the value assignment symbol in CSS, so using an equal sign '=' instead is an error and is required by the CSS specification to be ignored. Any browser that appears to honor this style is behaving improperly.
For length values a 'unit' is always needed and there shall never be any space between a number and its length unit.
A value given as e.g. '1.2 em' is an error and is required by the CSS specification to be ignored. Any browser that appears to honor this style is behaving improperly.
A semicolon ';' between declarations is required but it's also good "rule of thumb" to put a ';' even after the last declaration.
Finally, curly braces '{&hellip;}' group one or more declarations into a final CSS rule.


Question 18 :

Why are there gaps above and below my form in IE?

Answer :

A lot of the time, when you find gaps that you can't account for, they are due the default styles of different browsers - especially the margins and padding. IE gives forms some margins above and below forms while Firefox doesn't. It's like with lists - you'll find bigger padding and margins for lists in IE than in Firefox. Paragraph margins are different, as are the margins on heading tags (h1,h2, etc).

A good way to not get caught out by these problems is to set all margins and padding to zero at the top of your style sheet and then add them as and when you feel the a need for them, in that way, any margins and padding will be the same in different browsers.

CSS
* {
margin:0;
padding:0;
}


Question 19 :

If one were to set Text and Link colors using a style sheet, should one also define the background colors for these elements as well? 

Answer :

It is generally true that you should give background or background-color a value, but not necessarily a color value. E.g., if the document has a background image, you would "highlight" all links if you give them a background color.

body { background-image: url(light-texture.png) #FFF; color: #000 }
a:link, a:visited, a:active { color: #00F; background-color: transparent; }

By setting the background-image explicitly to transparent, you lower the risk of another rule in the cascade giving links a background that would highlight them.


Question 20 :

How do you override the underlining of hyperlinks? 

Answer :

CSS has the ability to explicitly control the status of underlining for an element - even for hyperlinks. The correct way to do this in an external or document-level style sheet is:
A { text-decoration: none }
and within an anchor element as:
<a HREF="example.htm" STYLE="text-decoration: none">link text</a>

Note: The underlining of hyperlinks is a long-standing visual convention that assists in the visual identification of active hyperlink areas. Many users expect to see hyperlinks underlined and may be confused and/or irritated if they are not used. User-defined style sheets address this user need by allowing the user to have final control over this feature. Unfortunately, wide support for this ability does not yet exist.


Question 21 :

How do you show which page you're on (in a menu)?

Answer :

If PHP is not available to you, you could use the cascade. Put an id in your body tags and an id in each of your 'a' tags for the links.
Let's say on page one you have this:
CSS
<body id="page1">
....
<a id="page1link" href="page1.htm">page one</a>
...
</body>

In your CSS, you can have something like this:
CSS
#page1 a#page1link {
color:purple;


Question 22 :

How can I specify two different sets of link colors?

Answer :

By classifying each set of links and then attaching desired color to each set.
CSS:

A.set1:link {color: some_color; background: some_background_color}
A.set1:visited {color: some_color; background: some_background_color}
A.set1:active {color: some_color; background: some_background_color}

A.set2:link {color: some_color; background: some_background_color}
A.set2:visited {color: some_color; background: some_background_color}
A.set2:active {color: some_color; background: some_background_color}

You can name set1 and set2 any way you like as long as the names are made up of letters a-z, A-Z, digits 0-9, period, hyphen, escaped characters, Unicode characters 161-255, as well as any Unicode character as a numeric code. 
Note: to avoid conflict with user's settings a background property (background color) should also be specified together with the color property (foreground color).


Question 23 :

How can I place multiple blocks next to each other?

Answer :

In theory, the following will produce 4 "columns":
<DIV style="float: left; width: 25%;">Block 1</DIV>
<DIV style="float: left; width: 25%;">Block 2</DIV>
<DIV style="float: left; width: 25%;">Block 3</DIV>
<DIV style="float: left; width: 25%;">Block 4</DIV>

Each "column" will occupy 25% of the screen. This relies on a correct implementation of float, which cannot be said of many legacy browsers. If you cannot accept display variances in older browsers, then it may be best to fall back to table-based solutions. 

2. By making the block an inline element and then use text-align property

<DIV STYLE="text-align: center">
<TABLE STYLE="display: inline">
...
</TABLE>
</DIV>

This technique depends on the incorrect implementation of text-align behavior in older browsers. It will likely cease to work in future CSS-conformant browsers, and eventually it will probably not be a viable solution.