Tuesday, 14 May 2013

Tips for Writing Better And More Comprehensive CSS

CSS language is not very difficult to master, but if you are using it for a large project, it can be very difficult to manage if you do not follow a defined approach while writing CSS code.
Here are few tips that will help you write better and easy to manage CSS code.

Doctype
Initially we need to use those long Doctypes that were almost impossible to remember.
Now we just need to use <!DOCTYPE html> on the top of your document, we have a much cleaner and better solution.
Nevertheless, some people still forgets to specify it. This is mandatory for a validated and organized HTML document.

ID and Classes

An ID is a unique identifier which allows us to target a specific element on the page and since it is unique it can only be used once in a page.
On the other hand, we have classes which allow us to do exactly the opposite. Classes are used when you have the same element more than once on a page.

How not to do it:
<div id="block">
   <div id="btn"></div>
   <div id="btn"></div>
</div>

How you should do it:
<div id="block">
   <div class="btn"></div>
   <div class="btn"></div>
</div>

Do not use inline styling

Inline styling is a very common practice and unfortunately a bad one.
It has nothing to do with invalid code or bad markup, but with organized code and structure.
This can become a problem in case where you have 30 pages and you need to remove an inline style you have applied to the same div on every single page. For this task it would take your lot of precious time.
If we are using class in the same case then by just doing change at one place it will get reflect where ever it is used, which is pretty fast and effective.

How not to do it:
<div style="width: 100%; background: #fff;"></div>

How you should do it:
<div id="wrap"></div>

Ove use of divs and CSS Classes

While working on your project, you know how to use divs, ids, and classes.
Inline styling is not your thing (fortunately) and you love to create styles and apply them everywhere.
That's great, but don't write more than you have to.
Having a div with an unordered list inside and a class applied to each li element is unnecessary.

How not to do it:
<div id="navigation">
   <ul>
      <li class="left"></li>
      <li class="left"></li>
      <li class="left"></li>
   </ul>
</div>

How you should do it
<ul id="navigation">
   <li></li>
   <li></li>
   <li></li>
</ul>

And in your style sheet
#navigation li { float: left; }

Browser Resolution

According to the W3C's statistics, 13.8% of internet users have a 1024×768 screen resolution, and 85.1% use a bigger screen resolution.
So the question arises is that "What resolution we should design for?" Besides, though 13.8% seems to be a fairly low number, it still represents millions of internet users.

How you should do it

Consider everyone's needs, and especially your target audience.

Block and Inline elements

Differentiating block from inline elements can be a a delicate matter for beginners.
A block element is displayed on a new line taking by default 100% width of the containing element, like divs (<div>) or paragraphs (<p>).
An inline element is displayed with no line breaks meaning that it starts on the same line, taking only his own width, like span (<span>) or image (<img>).
You can also change the way an element is displayed, this means that you can change an inline element to be display as a block and vice versa.

How you should do it
span { display: block; }


Use comments to organize your code

This is something purely optional but I highly recommend its use. It not only helps you find the section or element your are looking for, but also makes your life easier when you need to know which div your </div> is closing.

How you should do it
<!-- Begin #header -->
<div id="header">
<!-- End #header -->
</div>

Stylesheet:
/* --------------------------------------------------------------
Header
-------------------------------------------------------------- */
#header { background: #fff; }


Cross-Browser Compatibility

When you decide to make something, you should place yourself in the role of the end-user and imagine that even today some of them still use browsers like IE6.
A page in renders differently in Firefox than in Chrome or Internet Explorer.
There are some useful tools available in market today to check how your page renders in different browsers. You can make use of it.


Don’t use heading tags randomly

Heading tags are not just there to make it pretty, they establish the importance of your content which makes them valuable for SEO.
There are six Heading tags: h1, h2, h3, h4, h5, and h6.
H1 is the most important, so you should use it for your web page or business name only.
The rest of the tags should be used according to title or content importance.
Also, you don't need to have heading tags everywhere on your document.

How not to do it:
<h6>Post title</h6>
<h1>Text content</h1>

How you should do it:
<h2>Post title</h2>
<p>Text content</p>

Use absolute position only when you have to

When you're starting out, you can easily become addicted to the use of absolute positioning.
This is because it is an easy way of placing elements, however this property should not be used excessively.
Since elements with absolute position lose their normal flow, it is almost impossible to align them with other sections on the page.

You simply can't tell a normal element to be on the left side of an element with absolute position.

No comments:

Post a Comment