Friday, September 25, 2009

Best practices of CSS beginners

After researching and developing some part of my project I am in agreement with some researchers and web developers that CSS practices help us to avoid design faux pas easily. I have tried to enumerate some of those, as seen below:-

1. The code should be compact and legible. This is important for the future, as it is easier to maintain and development is quicker, when the developer wishes to enhance the functionality of the page and is not incapacitated by seeing the irrelevant and scattered code written. The code or CSS rules can be written ‘All on one line’ or ‘Each style gets its own line’. And to me the latter appeals more as for example:

1. .someDiv {

2. background: red;

3. padding: 2em;

4. border: 1px solid black;

5. }

Where ‘.someDiv’ is a class name and the CSS rules are enclosed between the curly brackets respectively.

2. Another advantage of creating legible and compact code is it remains consistent throughout the webpage no matter how many pages are developed for the website. Later the developer can start using their own “sub-language” of CSS allowing them to write the code easily and speedily.

3. Another CSS tool that speeds up production is CSS framework. This is particularly important as it not only increases production but also helps avoid common mistake. And gain best suited browser-compatibility. Also following it one can develop a clean well-structured and complete code. Some developers scoff at this tool but it is a great help. However, CSS framework is a great tool to use for developers, who know how to use them intelligently. But for beginners it can be quite confusing.

4. In order to avoid browser inconsistencies in terms of height and width, font-sizes, margins etc, it is a good practice to reset these. This one can do by using CSS rules of reset or having popular reset tools such as MeyerWeb, Yahoo’s developer reset incorporated. But I used my own reset CSS rules. The reset allows the layout to appear consistent across all browsers.

5. It is a good to organize the Stylesheet in a Top-down framework and to comment each section appropriately. I followed this approach for the layout of my pages. An example is as follows:

1. Generic classes (body, a, p, h1, etc.)

2. #header /***header***/

3. #nav-menu

4. #main-content /***main content****/

6. It is a good practice to combine elements that share common properties instead of re-writing same code repeatedly also in this way elements can have their unique properties as and when deemed fit.

1. h1, h2, h3 {font-family: tahoma, color: #333}

and

h1 {size: 2.1em}

7. It is advantageous to create the HTML document first and then decide the Layout style in CSS. Writing the HTML document first visualizes the entire page and allows one to consider CSS holistically, top-down manner.

8.            Use the correct doctype (Document type declaration) as that matter as to whether or not HTML document and CSS will render in the browser properly. The doctype informs the validator what version of (X)HTML is used and appears atop in every webpage. I have used the doctype 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> this is compliant with XHTML 1.0 transitional version across various browser platforms.

9. CSS can be shrunk by using shorthand when crafting elements like padding, margin, and font, i.e., one can combine styles in one line as for example.

1. #crayon {

2. margin-left: 5px;

3. margin-right: 7px;

4. margin-top: 8px;

5. }

It can all be stated in one line like

1. #crayon {

2. margin: 8px 7px 0px 5px; // top, rightright, bottombottom, and left values, respectively.

3. }

10. It is imperative to understand the difference between Block and Inline elements to be able to force the correct CSS rule for the (X)HTML element. Generally all Block elements like p, h1, h2 and h3 span the entire width of the available space but Inline elements take only as much space as they need, and do not force a new line after usage. Some block tags are div, h1...h6, p, ul, li, table, blockquote, pre, form and common inline tags are span, a, strong, em, img, br, input, abbr, acronym.

11. In order to maintain consistency, increase legibility and avoid repetition a handy tool is that CSS rules must be written in an alphabetic order.

1. #cotton-candy {

2. color: #fff;

3. float: left;

4. font-weight:

5. height: 200px;

6. margin: 0;

7. padding: 0;

8. width: 150px;

9. }

12. CSS compressors remove line breaks, white spaces and combine elements. It results in increased upload speeds and reduced file size, although performance is enhanced legibility of CSS file in terms of reading and editing it later is compromised. Two good industry tools to shrink CSS are CSS Optimizer and CSS Compressor.

13. To center layouts on browser designers must use ‘margin: 0 auto;’ //top, bottombottom – and left, rightright values, respectively under div, paragraph or other elements in your layout.

14. A must have tool for all developers is ‘Firebug’. This is a FF extension and helps to inspect, modify and edit CSS in real time. Firebug also helps to debug JavaScript, inspect HTML and find errors.

15. It is recommended for the novice designers to use text-transform to format the site. This property in header style is shown as: text-transform: lowercase; Then all the letters in the header will be lowercase by default.

16. It is a good idea to check for errors in the CSS or HTML code by using CSS Validator (http://jigsaw.w3.org/css-validator/) and markup (http://validator.w3.org/) and then rectify them quickly. The validators have spot errors normally as missing a closing div tag or a semicolon (“;”) in CSS property.

17. Many different web browsing systems (laptop, mobile, PDA) show that ems (em) are increasingly becoming the default for font size measurements, because of their increase affinity and flexibility to scalability. Whereas pixels (px) are a static way to define font sizes. So, many designers are defaulting to pixel based layouts because of browser zooming capacity.

18. It is worthy to use list items to structure format and it is also easy to modify the structure of the CSS property (I used listing option in getting the feedback input). List help in creating navigation menu and things of the sort. Novice designers land up using div for each element in the list as they are not aware how to utilize the list properly.

19. Another mistake that beginners make is adding extra selectors to CSS elements. This is easy to do but it clutters and complicates the CSS style sheet. An example of this is: body #container .someclass ul li {....}. Here.someclass li would have worked.

20.          Because of heterogeneity in rendering properties browsers render pages differently. And an important difference between them is how rendering conforms to margin and padding. In order to limit this variance developers can use a simple code with global reset like * {margin:0; padding:0;} (I used this in my layout CSS file to conform to all browsers while rendering my page).  

21. Another good option is to develop the layout code separately (multiple stylesheets) at start and then combine them into one file. This reduces the number of HTTP requests to one. Also the entire file is cached at the user’s computer, so, data viewing and editing is faster.

22. Another practice that can be indulged in but after some proficiency is migrating to Object Oriented CSS (OOCSS). This could be bit of a challenge for the beginner to grasp instantaneously. But the principles of OOCSS are of separating different aspects of CSS into more logical sections, organizing CSS rules into more modular and reusable.

23. When checking for error while debugging with XHTML validator developers need to note the missing close div tags.

<1311:6502>

Reference:-

2005, ‘CSS Shorthand Guide’, viewed 25 September 2009, http://www.dustindiaz.com/css-shorthand/

2007, ‘CSS Frameworks + CSS Reset: Design From Scratch’, Smashing Magazine, viewed 26 September 2009, http://www.smashingmagazine.com/2007/09/21/css-frameworks-css-reset-design-from-scratch/

Dubost, K. 2008, ‘My Web site is standard! And yours?’ in W3C quality assurance, viewed 26 September 2009, http://www.w3.org/QA/2002/04/Web-Quality

Stansberry, G. 2009, ‘30 CSS Best Practices for Beginners’ in webdesigner depot, viewed 25 September 2009, http://net.tutsplus.com/tutorials/html-css-techniques/30-css-best-practices-for-beginners/

Weakley, A. 2009, DMT videos: lectures and tutorials for Spring Semester 2009 week5/6

Zeldman, J. 2002, ‘Fix Your Site With the Right DOCTYPE!’, A list Apart, viewed 25 September 2009, http://www.alistapart.com/articles/doctype/


Tuesday, September 22, 2009

HTML Best practices for beginners:-

Here I would be a little more direct than the best practices for CSS of beginners as we are all aware of the intricacies of an HTML document. And the HTML document needs to integrate with the backend-tools to manage a client request and produce a seamless response displayed to the user.

1. It is absolutely essential that we avoid any lose code while coding the request. Earlier browser versions allowed the li tag to be used with any wrapper like ul/ol tag. Also the closing li tag was left off and the syntax was something like:

1. li>Some text here.

2. li>Some new text here.

3. li>You get the idea.

But as per the present standards of W3C the syntax should be like:-

1. ul>

2. li>Some text here.

3. li>Some new text here.

4. li>You get the idea.

5. /ul>

2. It is necessary to declare the Correct Doctype so that the browser can interpret the correct markup. I chose the DOCTYPE: . Although it is believed that browsers revert back to regular HTML when interpreting it. So, for reason the correct Doctype would be HTML 4.01 strict. Which can be coded as follows: .

3. It is not advisable to include inline styles in the HTML doc itself, as that may lead to error in coding. Once the page is completely coded only then styling begins.

4. All External CSS files should be placed in the head tag of the document. The main benefit of this is that the webpage loads faster. As for example:

1. head>

2. title>My Favorites Kinds of Corn

3. link rel="stylesheet" type="text/css" media="screen" href="path/to/file.css" />

4. link rel="stylesheet" type="text/css" media="screen" href="path/to/anotherFile.css" />

5. /head>

5. The JavaScript files whose only purpose is to add functionality as for example triggering a response after a button is clicked then those JS files can be placed at the bottom before closing the HTML body element. As for example:

1. p>And now you know my favorite kinds of corn.

2. script type="text/javascript" src="path/to/file.js">

3. script type="text/javascript" src="path/to/anotherFile.js">

4. /body>

5. /html>

6. It was a common practice in the old days of late 1990s to place JavaScript within the HTML document for creating simple image galleries. This is redundant instead a good practice is to create external JS file and use “addEventListener/attachEvent” to listen for your desired event. Or use jQuery and use the “click” method. As for example:

1. $('a#moreCornInfoLink').click(function() {

2. alert('Want to learn more about corn?');

3. });

7. Several times shabby markup causes a strange whitespace issue on the page. A coherent way of developing HTML and CSS code is to continuously validate it by downloading the Web Developer Toolbar and use the “Validate HTML” and “Validate CSS” options.

8. Firebug is a real biggie and a must have for all beginners. It is the best plugin for website development. It provides incredible JavaScript Debugging and pinpoints what elements in the document are inheriting some extra features that one is unaware of.

9. Also after downloading Firebug developers need to realize its full potentiality and not just be stuck with around 10-20% capability of Firebug.

10. In order to maintain homogeneity across all HTML documents constituting web pages its important to write all HTML tags in lowercase. As for example

1. div>

2. p>Here's an interesting fact about corn.

3. /div>

Only the DOCTYPE tag is in uppercase.

11. Generally the H1 title header is reserved for article heading while creating blogs. Experts feel this practice increases SEO.

12. The Firebug plugin is synonymous to ySlow (from Yahoo). When executed it runs a compiler which marks the area where the site requires service and improvement.

13. Generally navigation (#nav) can be wrapped with ul tag containing a list of items. This is congenial to semantics and is a good coding practice.

1. ul id="nav">

2. li>a href="#">Home

3. li>a href="#">About

4. li>a href="#">Contact

5. /ul>

14. Generally the IE browser less than IE 7 are more orthodox and need some special treatment when the user wishes to render the HTML and CSS code on it. So, in order to solve this developer need to create some specific code to run files smoothly on browsers less than IE 7, which is as follows:

1. !--[if lt IE 7]>

2. link rel="stylesheet" type="text/css" media="screen" href="path/to/ie.css" />

3. ![endif]-->

15. In order to develop a fantastic code good code editor is required. Generally for this purpose we are tutored in class to use Notepad++ to view and develop the code on PC. Other noteworthy editors for PC are: InType, E-Text Editor, Aptana and Dreamweaver CS4. Certain code editors for Mac users are: Coda, TextMate, Espresso, Aptana and Dreamweaver CS4.

16. It is advisable that developers use Compressors after completion of coding. This reduces bandwidth requirement by 25%, which could be pretty substantial. Popular CSS compressors are: CSS Optimiser and CSS Compressor. Available JS compressors are JavaScript Compressor and JS Compressor.

17. The new developers like me suffer from divtis, which is categorized as an instinct to wrap the elements under unnecessary div tags. This is inefficient. The idea to developing good markup is to edit such unnecessary tags.

18. All images need “Alt” attribute. This is significant for SEO validation and accessibility. As for example: ”A

19. A good tactic for developing coherent code is to learn from the leaders by viewing the page source (source code). Developers need to view the source and study the Head tag for the name of the script (JavaScript) producing a novel effect. And then search it. A good search engine in this regard is Google.

20. Developers need to style all elements of the HTML document appropriately. Using special style for elements like ul, ol, p, h1-h6, blockquotes, etc yields rich dividends.

21. Web developers need to heavily network in order to gain access into coherent web design especially for the back-end and front-end as well. So, a good networking tool that user can follow is Twitter.

22. Photoshop is a good design or image editing software for beginners of HTML and CSS to learn. Also it is good to know the keyboard shortcuts of PS.

23. It is good to know each HTML tag although heaps of them won’t be encountered regularly. But this should not stop the beginners for learning the uncommon tags of HTML, like abbr, cite. These tags are very rare but are used to abbreviate and cite/reference in blogs etc.

24. It is important to create a reset file. It saves agony and frustration. One may use the following one or download a popular one, like Eric Meyer’s.

1. html, body, div, span,

2. h1, h2, h3, h4, h5, h6, p, blockquote, pre,

3. a, abbr, acronym, address, big, cite, code,

4. img, ins, kbd, q, s, samp,

5. small, strike, strong,

6. dl, dt, dd, ol, ul, li,

7. fieldset, form, label, legend,

8. table, caption, tbody, tfoot, thead, tr, th, td {

9. margin: 0;

10. padding: 0;

11. border: 0;

12. outline: 0;

13. font-size: 100%;

14. vertical-align: baselinebaseline;

15. background: transparent;

16. }

17. body {

18. line-height: 1;

19. }

20. ol, ul {

21. list-style: none;

22. }

23. blockquote, q {

24. quotes: none;

25. }

26. blockquote:before, blockquote:after,

27. q:before, q:after {

28. content: '';

29. content: none;

30. }

31.

32. table {

33. border-collapse: collapse;

34. border-spacing: 0;

35. }

25. Another mistake I make is not aligning my elements in anyway. This is a very common error of a beginner. But it is necessary to align elements as best as possible.

26. Experts feel that after gaining good design practice in HTML, CSS and Photoshop the beginners must transition from making PSD layout to converting that laout to a working website.

27. Last but not least beginners in their first year of learning are not to view CSS frameworks, as those are for experienced developers who want to save themselves a bit of time.

<1364:6886>

References:

Johns, M., ‘JavaScript Closures 101- they're not magic’, viewed 21 September 2009, http://wsabstract.com/javatutors/closures.shtml

Way, J., ‘30 HTML Best Practices for Beginners’, Webdesigner depot, viewed 22 September 2009, http://net.tutsplus.com/tutorials/html-css-techniques/30-html-best-practices-for-beginners/

Weakley, A. 2009, Video tutorial week 3-5, UTS spring semester 2009

Note: For all the HTML elements I Have removed the beginning tag (less than sign) in order to publish the blog in the blogger as otherwise it is playing truant.