CSS: advice{councils} and receptions, a part 1
As I widely use CSS on an extent 2kh years, to me some receptions which do{make} CSS by more effective tool were remembered and solve specific problems. I would like to share my liked CSS-prijomami and to explain some the most often mistakes which are made by newbies. If you have operational experience with CSS is the fastest on your way already there was this advice{councils} and receptions but who knows, suddenly there will be a couple new.
From the very beginning it was planned to write all in one clause{article}, but advice{councils} became more and consequently I have divided{shared} all into two parts for easier mastering this material.
If value is not equal 0 always specify a unit of measurements
To not specify a unit of measurements for value of length - rather often mistake among beginning{starting} to study CSS. It is possible to avoid it in HTML, but in CSS all values of length should have a unit of measurements. There are two exceptions: values line-height and 0 (zero). Zamete, that after value the unit of measurements necessarily should follow - it is not necessary to insert an empty place (blank) between them.
There is no necessity to specify a unit of measurements for 0 (zero). The zero of pixels is equaled to zero of centimeters or any other unit of measurements. Not looking at it, very much often there is something on similarity padding:0px when it is possible to write padding:0 and the effect will be same.
There is nothing bad in adding to zero a unit of measurements, simply it is superfluous volume and, as to me, looks not accurately.
Remember sensitivity the register
When CSS it is used together with XHTML, names of elements and selectors become sensitive to the register. To avoid the problems connected to it, I recommend to use always in CSS-selectors the bottom register for names of elements.
Values of attributes class and id are sensitive to the register in HTML and XHTML, therefore avoid the mixed register in names of attributes class and id. If for any reason you use the mixed register, check up identity of the register in CSS twice.
Specify colors
This advice{council} is in mentioned before clause{article}, but I use time of it so much, that I shall be repeated here: when color in CSS is underlined in a hexadecimal notation (hexadecimal colour notation) and when color will consist of three pairs hexadecimal numbers then you can specify colors more effectively by removal{distance} of each second: *000 it is identical *000000, *369 it is identical *336699.
Also remember a sign on number - a symbol "*" before a code of color.
One more advice{council} on color - you can specify safe colors for the Worldnet, using figures which are multiplied on 3 for red, green and dark blue values: 0, 3, 6, 9, C and F. *99c is safe color, *98c is not.
Exclude types of elements for selectors class and id
When you write selectors which specify an element with the certain values class and id you can clean{remove} type of an element before a point (the selector of a class) or before * (id-selector).
So instead of writing:
div*content {/* declarations */}
fieldset.details {/* declarations */}
You can write:
*content {/* declarations */}
.details {/* declarations */}
And to save a little bajtov for each of selectors.
In particular it is useful for selectors id as in the document they should be unique, that reduces risk of the conflict CSS-rules with each other. Names of classes, on the other hand, can be used in the document any quantity{amount} of times, and different types of elements can be appropriated{given} for the same classname (or several names). Differently to designate types of elements with an identical classname, it will be necessary for you to specify type of an element in the selector.
Know, that the above-stated CSS-rules are not identical. If without type of an element one rule is specified in the selector, and another with type of an element in the selector then that rule which uses type of an element, will have more important specificity.
Default values
/
Often you can not specify value of property, leaning{basing} on a default value of this property. Especially it is important for taking into account, when you use the reduced properties as to any not specified property the default value corresponding to this property is appropriated{given}.
One of the widespread default values are 0 for padding (the truth is some exceptions), and transparent for background-color.
Recognizing that in different browsers there are differences between default values, some people prefer to use in the beginning of a CSS-file global reset of empty space by zeroing properties margin and padding for all elements.
* {
margin:0;
padding:0;
}
Do not specify anew inherited values
Values of many properties are inherited by any descendant of an element for whom property is underlined. Related properties color and font are the vivid example of such properties.
Know, that some properties can be rewritten by specific cascade styles of user agents (PA) of browsers, for example values of a browser by default. Therefore, using the following rule, you cannot make all headings low-fat:
body {font-weight:normal;}
The predetermined rules of a browser are more specific because of the cascade which is described further.
Take advantage of the cascade
The cascade allows you to use some rules for properties of an element. You can redefine the same property or define{determine} additional properties. We shall say, you have following code of page:
<p class = "update"> Update: Lorem ipsum dolor set </p>
In CSS, you can use different rules to specify the properties suitable to all elements p and what are specific to elements p with a class update:
p {
margin:1em 0;
font-size:1em;
color: * 333;
}
.update {
font-weight:bold;
color: * 600;
}
Both rules will be combined for elements p with a class update. The class of the selector is more important, than type of the selector, therefore, when there is a conflict, the properties determined in the second rule will be used. In this case color.
It is more than information on how CSS-rules are counted up is possible to find in Calculation of specificity of the selector in specification CSS 2.1.
Some names of classes
You can appropriate{give} some names of classes to one element. It allows you to write some rules which define{determine} different properties, and to apply them only necessarily. Let's assume, that you write a code of gallery of pictures which contains author's and not author's a picture. Still there can be a special offer for some pictures. A code for three pictures the following:
<img src = "foo.gif" class = "special" alt = ""/>
<img src = "bar.gif" class = "royaltyfree" alt = ""/>
<img src = "baz.gif" class = " royaltyfree special " alt = ""/>
That author's a picture differed from the others, you can create a rule for elements which class will be called royaltyfree and if you want that a picture with the special offer were a little allocated you can create a class special.
Further CSS can look on similarity of it:
.royaltyfree {border:2px solid *666;}
.special {
padding:2px;
background: * ff0;
}
Any picture with a class special will have around of itself space in 2 pixels and a back background of yellow color. A picture with a class royaltyfree will have an edge in 2 pixels, and a picture with both classes will have an edge, space and a yellow background.
It is possible to not stop on it is only an example. Also try to use semanticheski correct names of classes which describe that does{makes} an element, instead of how he looks.
Use selectors of descendants
To not use selectors of descendants is one of the most widespread examples of inefficient use CSS whom I see from newbies. Selectors of descendants can help to do without to you many attributes of a class, and to make your CSS-selectors more effective. For an example it is possible to take the following structure of a code:
<div id = "subnav">
<ul>
<li class = "subnavitem"> <a href = "*" class = "subnavitem"> Item 1 </a> </li>
<li class = "subnavitemselected"> <a href = "*" class = "subnavitemselected"> Item 1 </a> </li>
<li class = "subnavitem"> <a href = "*" class = "subnavitem"> Item 1 </a> </li>
</ul>
</div>
This code can be accompanied by following CSS:
div*subnav ul {/* Designations of style */}
div*subnav ul li.subnavitem {/* Designations of style */}
div*subnav ul li.subnavitem a.subnavitem {/* Designations of style */}
div*subnav ul li.subnavitemselected {/* Designations of style */}
div*subnav ul li.subnavitemselected a.subnavitemselected {/* Designations of style */}
You can replace the above-stated examples with the following code:
<ul id = "subnav">
<li> <a href = "*"> Item 1 </li>
<li class = "sel"> <a href = "*"> Item 1 </a> </li>
<i> <a href = "*"> Item 1 </a> </li>
<ul>
And it CSS:
*subnav {/* Designations of style */}
*subnav li {/* Designations of style */}
*subnav a {/* Designations of style */}
*subnav .sel {/* Designations of style */}
*subnav .sel a {/* Designations of style */}
Your code should be as it is possible more purely{cleanly}. It will allow to make a code of page and CSS more effective and legible.
Avoid inverted commas in links
To save a couple bajtov here and there, I do not conclude the link for pictures of a back background in inverted commas. Inverted commas are not obligatory. There are data that in some browsers (in most cases Internet Explorer on the Macintosh) there are problems when links are quoted.

|