Wednesday, 22 September 2010

CSS Common Mistakes for Beginners

I frequently encounter  people doing mistakes when they write CSS and here I will try to point out some of them.

1.  Duplicate properties when possible

When you want to style particular element let's say with border -

  • border-left:1px solid #000;
  • border-right:1px solid #000;
  • border-top:1px solid #000;
  • border-bottom:1px solid #FFF;

you can duplicate properties this way -

  • border:1px solid #000;
  • border-bottom:1px solid #FFF;

first, your browser will apply first property where all sides of the border will be same color - #f00, and then second property. But browser does these operations so fast that you can't notice these changes, in result you get the same style but you write less code.

2. Redundant parameters and units

Let's look at the following code -
  • margin:10px 5px 10px 5px;
it could be written like this -
  • margin: 10px 5px;

Many people write units next to 0 parameters like this -
  • margin: 10px 0px 5px 0px:
when they could write it this way -
  • margin: 10px 0 5px 0;

3. Hash symbol missing

When you assign a colour to  parameter don't forget about hash symbol -
  • color:ffffff;
It should be:
  • color:#ffffff;
      Or even:
  • color:rgb(111,111,111);

4. Don't repeat unnecessary parameters 
  • border-top:1px solid #00f;
  • border-right:1px solid #00f;
  • border-bottom:1px solid #00f;
  • border-left:1px solid #00f;
Why not just write this?
  • border:1px solid #00f;

5. Don't repeat styles

If you find yourself writing code like this -
  • p {color:#FFF;}
  • h1 {color:#FFF;}
  • h4 {clolor:#FFF;}
You can save your time just by grouping these elements -
  • p, h1, h4 {color:#FFF;} 

6. Use short color declarations

Hex numbers that repeat like -

  •  color:#ffffff;
  •  background-color:#000000;
  •  border:1px solid #ee66aa;

can be condensed to -

  • color:#fff;
  • background-color:#000;
  • border:1px solid #e6a;

But it doesn't work this though

  • color:#ac34b5;

This is another way to condense your code and keep things short and easy to look at.

7. Don't use height parameter unless it's necessary

Once I worked on my friends stylesheet and found that he used height parameter on all <divs>  This was a nightmare to fix just a tiny bit.
Use margin: and padding: parameters instead, that gives you much more flexibility what you can do with an element

8. Don't use inline CSS

Always and always separate HTML and CSS otherwise when it comes to changes you wanna make to design, you will regret that you even started that thing.

So remember!

  • wrong: <a href="somewhere.html" style="float: right; color: rgb(51, 51, 51);">link</a>
  • right:    <a href="somewhere.html" class="link_style">link</a>

Tuesday, 21 September 2010

About CSS (Style Cascading Sheets)


     Cascading Style Sheets (CSS) is a style sheet language used to describe the look and formatting of a document written in a markup language. Its most common application is to style web pages written in HTML and XHTML.

     CSS is designed primarily to enable the separation of document content (written in HTML or a similar markup language) from document presentation, including elements such as the layout, colors, and fonts. This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, enable multiple pages to share formatting, and reduce complexity and repetition in the structural content (such as by allowing for tableless web design). 

    CSS can also allow the same markup page to be presented in different styles for different rendering methods, such as on-screen, in print, by voice (when read out by a speech-based browser or screen reader) and on Braille-based, tactile devices. While the author of a document typically links that document to a CSS style sheet, readers can use a different style sheet, perhaps one on their own computer, to override the one the author has specified.

     CSS specifies a priority scheme to determine which style rules apply if more than one rule matches against a particular element. In this so-called cascade, priorities or weights are calculated and assigned to rules, so that the results are predictable.

Levels (versions)

     CSS has various levels and profiles. Each level of CSS builds upon the last, typically adding new features and typically denoted as CSS1, CSS2, and CSS3. Profiles are typically a subset of one or more levels of CSS built for a particular device or user interface. Currently there are profiles for mobile devices, printers, and television sets. Profiles should not be confused with media types, which were added in CSS2.

     CSS level 1 (1996, 1999) contains properties for fonts, margins, colors, etc., that nearly all profiles of CSS need.

     CSS level 2 revision 1 (“CSS 2.1”) contains all of CSS level 1 and adds absolutely positioned elements, automatic numbering, page breaks, right to left text and other things. At this moment, September 2009, it is a Candidate W3C Recommendation.

     CSS level 3 is under development. It includes all of level 2 and extends it with new selectors, fancy borders and backgrounds, vertical text, user interaction, speech and much more.