HTML CSS Best Practices


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

HTML CSS Best Practices

  1. 1. HTML & CSS Best Practices Presenter: Đặng Minh Tuấn aka. ohisee from:
  2. 2. About Presenter - Who am I? Fullname: Đặng Minh Tuấn Worked for: • 2010: - • 2008 - 2010: Aprotrain Aptech - • 2008: NAISCORP - • 2003 - 2008: Many IT Companies Trained HTML, CSS in: • NAISCORP - • Vietsoftware International - • VNEXT - • VietNamBiz - • Hanoi PHP Day 2008, 2009, 2010 Programming: • Clientside o HTML o CSS o Javascript • Serverside o PHP o MySQL
  3. 3. About HTML, CSS - What is this? If a browser is a worker and a web page is a house then contents are bricks, HTML elements are divisions, and CSS is paint.
  4. 4. About HTML, CSS - How and Who? How they grow up? Who has been "playing" with them? • Web Designer > CSSer > Javascripter > Serversider o
  5. 5. About this Presentation - It is ... ... for one who • know what is HTML, CSS • and has been working with HTML, CSS daily and want to make better code ... not for one who • don't know or don't care about HTML, CSS • or still working with HTML, CSS (and may get thing done) but don't want to make better code
  6. 6. About this Presentation - legends Wrong = Something you should not do in almost cases. Acceptable = Something you should not do in some cases Right = Something you should do in almost cases.
  7. 7. 10 Rules of Ss – Skinable – Sensible – Simple – Semantic – Speed – Standard – Safe – Structural – Study – Smart About this Presentation - structure
  8. 8. Ready?
  9. 9. 1. Skinable
  10. 10. Skinable - definition Skinable = Changeable and Flexible Skin (Style)
  11. 11. Skinable - examples Wrong <h1><u><i>Heading</i></u></h1> Right <style> h1 { font-style: italic; text-decoration: underline; } </style> <h1>Heading</h1>
  12. 12. Skinable - examples Wrong <p> <img src="image.jpg" align="left"> ... </p> Right <style> .thumbnail { float: left; } </style> <p> <img src="image.jpg" class="thumbnail"> ... </p>
  13. 13. Skinable - examples Wrong <table> <tr> <td bgcolor=red width=500 height=50 colspan=2> ... </td> </tr> <tr> <td bgcolor=red width=100 height=300> ... </td> <td bgcolor=red width=400 height=300> ... </td> </tr> <tr> <td bgcolor=red width=500 height=50 colspan=2> ... </td> </tr> </table> Right <link rel="stylesheet" href="style.css"> <div id="header"> <div id="sidebar"> <div id="content"> <div id="footer">
  14. 14. Skinable - examples Wrong <ul style="padding-left: 5px" > Right CSS (styles.css) #menu { padding-left: 5px; } HTML (index.php) <ul id="menu">
  15. 15. Skinable - best practices • Separate Data (Structure) from Style (Presentation) and Behavior (Themeable) o Don't use HTML/XHTML Deprecated Tags and Attributes and avoid presentational Tags. Use CSS instead   Link Note: <center> can be replaced by : margin: 0 auto; width: ... and text-align: ... • Take a (careful) look at: CSS Zen garden - o One HTML Structure create Many Beautiful and different Interfaces by using many different CSS files. • TEAM NOTE: o Train this rule first!
  16. 16. 2. Sensible
  17. 17. Sensible - definition Sensible = understandable • Somebody need to understand your code • You need to understand ... your code after a long time
  18. 18. Sensible - examples Wrong <div id="xxx"> <p class="abc"> <div id="middle-left-and-then- a-little-lower"> Right <div id="header"> <div id="left_column"> <div id="sidebar"> <p class="first"> <p class="intro"> <div class="section">
  19. 19. Sensible - best practices • Give id and class right spelling and understandable names. • Know some kinds of naming - naming-convention-in-css/ o Prefer structural name: describe what elements are  header  sidebar o to styling (presentational) name: describe how element look like  right_column  red_link • Comment (in HTML, CSS) when needed o to explain the reason (why?) • TEAM NOTE: o Build a Naming Convention for common Web UI elements 
  20. 20. 3. Simple
  21. 21. Simple - definition Simpler = Shorter • Less code to type • Less code to read • Less data to send
  22. 22. Simple - examples Wrong <ul> <li class="active_menu_item" <li class="menu_item"> <li class="menu_item"> <li class="menu_item"> <li class="menu_item"> <li class="menu_item"> <li class="menu_item"> </ul> Right <ul id="menu"> <li class="active"> <li> <li> <li> <li> <li> <li> </ul>
  23. 23. Simple - examples Wrong <div> <div> <div> <h1> <div>heading</div><br></h1> <div> <div> paragraph<br> <div> paragraph </div> <br><br> </div> </div> </div> </div> </div> Right <div> <h1>heading</h1> <p> paragraph </p> <p> paragraph </p> </div>
  24. 24. Simple - best practices • Naming short enough to keep it sensible • Good CSS skills enough to: o know how to use CSS Selector   html selector  id selector  class selector  descendant selector o avoid classitis o use CSS Shorthand o keep as little as nested elements as possible. o know how to solve issues by adding more CSS instead of HTML • TEAM NOTE: o require and train better CSS skill for team member o train this rule
  25. 25. 4. Semantic
  26. 26. Semantic - definition Semantic = Software can understand your code. so your web is accessible for all people. = Search engine can understand your code so your web has better SEO Warning: Semantic is sometimes not simple!
  27. 27. Semantic - examples Wrong <div class="heading1"> <div class="heading2"> <div class="paragraph"> Right <h1> <h2> <p> Right (but complex or not good supported) <abbr class="dtstart" title="2001-01-15T14:00:00+06:00">2</abbr>- <abbr class="dtend" title="2001-01-15T16:00:00+06:00">4</abbr> <section> Acceptable <p>2-4pm</p> <div class="section">
  28. 28. Semantic - best practices • Use Right Tags for Right Jobs. Don't misuse any element. Don't create a tag soup. o div (divitis?) o table (tableless layout?) o ul, li (divless?) • Learn elements semantic o HTML References  o Semantically styling breadcrumbs  o Learn some others' data formart Microformat, Microdata, RDF, etc., • TEAM NOTE: o Build a Common Semantic List
  29. 29. 5. Speed
  30. 30. Speed for the Web Faster = Users load your web faster = Users feel more comfortable = Your web can satisfy more users What affect web speed? • Request time • Server processing time • Data Transfer time • Web Rendering time • Client processing time Warning: Faster is not always better
  31. 31. Speed - examples Wrong * { margin: 0; padding: 0; } Right: use a good CSS reset file
  32. 32. Speed - best practices Learn how the browser request and render your web • Web Performance Best Practices — speed/page-speed/docs/rules_intro.html • Best Practices for Speeding Up Your Web Site - • Some Tools - • TEAM NOTE: o Hire a Clientside Ninja Note: Speed has its cost • Learning about performances is hard • Implement what you learn is even harder • "Speedy" code is usually complex
  33. 33. 6. Standard
  34. 34. Standard - definition Web standards is a general term for the formal standards and other technical specifications that define and describe aspects of the World Wide Web. In recent years, the term has been more frequently associated with the trend of endorsing a set of standardized best practices for building web sites, and a philosophy of web design and development that includes those methods Create & develope Web Standards Fighting for Web Standards Teach
  35. 35. Standard - examples Wrong <div> <p> example paragraph </div> </p> Right <div> <p> example paragraph </p> </div>
  36. 36. Standard - best practices • Read the Specification o World Wide Web Consortium (W3C) - o Learn DTD - • Learn about the mission of fighting for standard o • Know how to use online and offline validator o online HTML, XHTML validator - o online CSS validator - o Firefox Addon HTML validator - US/firefox/addon/249/ • TEAM NOTE: o Validation is a must in training progress. Good News: Browsers are more and more supporting Web Standards Bad News: Good things (?) can break standards
  37. 37. 7. Safe
  38. 38. Safe (fallback) - definition Everything can be in trouble but If we care enough, troubles can become less troubles or become good chances!
  39. 39. Safe Fallback - examples Wrong <img src="hoctudau.png" /> #header { background: url(header.png); color: white; } Right <img src="hoctudau.png" alt="" title="" /> #header { background: black url(header.png); color: white; }
  40. 40. Safe Fallback - best practices • Follow Web Standard o use alt in img element • Think about the failures o image doesn't work? o CSS doesn't work? Some CSS rules don't work? o JS doesn't work? Some JS functions don't work? o frame, iframe don't work? o Flash doesn't work? • Bulletproof o Ebook: Bulletproof Web Design: Improving flexibility and protecting against worst-case scenarios with XHTML and CSS • TEAM NOTE: o train this rule.
  41. 41. 8. Structural
  42. 42. Structural - definition Structural = good organized
  43. 43. Structural - examples Wrong (development phase) style.css john.css Right (development phase) reset.css debug.css layout.css
  44. 44. Structural - best practices • Separate 2 phases: development and deployment (production) o minified, combined css (js) files in deployment phase • Separate 2 types of files structure in development phase o top down  debug.css  reset.css  layout.css  typo.css o bottom up  reset.css  layout.css  menu.css - menu_debug.css  infobox.css - infobox_debug.css • TEAM NOTE: o Study: OOCSS and CSS Frameworks o Build a CSS Libary
  45. 45. 9. Study
  46. 46. Study - definition Study = Explore, Seach, Ask, Learn, Store, Bookmark, Remember by Heart
  47. 47. Study - examples Right google: IE6 common bug • cheatsheet-how-to-fix-25-internet-explorer-6-bugs
  48. 48. Study - best practices • Learn when and how to google o Learn English o Learn to generalize (expand) and specialize (restric) search results  double text vs IE6 double text vs IE6 common bugs • Bookmark useful link o • Read useful Webs and Blogs o • TEAM NOTE: o share bookmark and experience between team member
  49. 49. 10. Smart
  50. 50. Smart - definition Smart people follow the targets, not the rules! Sometimes we should break the rules! Warning: We have to learn to follow the rules before learning to break them!
  51. 51. Smart - examples Wrong Try to make a perfect quality web when lacking of resources Right Think about what you want, but do what you can. Sometimes you should make an art, but sometimes you need money first.
  52. 52. Smart - best practices • Thinking about your target before thinking about the other rules o Sometimes you have to break some rules to follow other (more important) rules  "kill" speed to "save" simple  or "kill" simple to "save" speed  kill "semantic" to save "simple" and "time" • When needed, you can use anything o Table Layout  Softwares: Dreamweaver, Photoshop o HTML Frameworks  Baker Ebook Framework o HTML Generator, Editor  Zen Coding (not Zend) o CSS Frameworks  YUI, Blue Prints, 960gs o CSS Generators  SASS, LESS • TEAM NOTE: Look ahead and prepare for future
  53. 53. Want to learn more? Google: CSS best practices • Be a CSS Team Player: CSS Best Practices for Team-Based Development o • 9 CSS Best Practices You Need to Know o • 30 CSS Best Practices for Beginners o beginners/ Google: HTML best practices • 20 HTML Best Practices You Should Follow o Note again: Follow your targets and apply others' rules smartly
  54. 54. Good HTML, CSS Showcases • Deliver WOW Through Service o wow-through-service • Chris Shiflett: Blog o • Webkit CSS Transformers Top Trumps o • CSS Media Queries Sidebar o • The Official Site of Bill Gates - The Gates Notes o • About | Mozilla Messaging o • Simon Collison | Colly | The Celebrated Miscellany o • World Wide Web Consortium (W3C) o
  55. 55. Good HTML, CSS Showcases Web Applications • Wordpress • VBB 4.x
  56. 56. The End Q&A Sponsored by Avarage Score of HTML, CSS Code of some famous websites in Rule of Ss Scale? This slide is available online at: