Code Guide by @mdo, Translated by @SpencerAung

Standards for developing flexible, durable, and sustainable HTML and CSS.

Table of contents

HTML

CSS

Golden rule

ဒီအများလက်ခံထားတဲ့ (သို့) ကိုယ်ပိုင် စည်းမျည်းတွေကို အမြဲသုံးဖို့ အားပေးပါရစေ။ သေးသေးလေးပဲဖြစ်ဖြစ် အကြီးကြီးပဲဖြစ်ဖြစ် အမှားပါခဲ့ရင် အသိပေးဖို့ တောင်းဆိုပါတယ်။ ဒီ guide မှာ ထပ်ထည့်ချင်ရင် သို့ ကူညီချင်ရင် ကျေးဇူးပြုပြီး Github မှာ issue ဖွင့်ပေးပါ။ ဘာသာပြန်တာနဲ့ ပတ်သက်ပြီး ကူညီချင်ရင်တော့ ဒီမှာ issue ဖွင့်ပေးနိုင်ပါတယ်

ပါဝင်တဲ့လူ(contributor) ဘယ်လောက်များများ code လိုင်းကြောင်းတိုင်းက တစ်ယောက်တည်း ရေးထားသလို ဖြစ်နေပါစေ။

HTML

Syntax

<!DOCTYPE html>
<html>
  <head>
    <title>Page title</title>
  </head>
  <body>
    <img src="images/company-logo.png" alt="Company">
    <h1 class="hello-world">Hello, world!</h1>
  </body>
</html>

HTML5 doctype

HTML page တိုင်းမှာ doctype နဲ့စပြီး တတ်နိုင်သမျှ browser အားလုံးမှာ စံအဖြစ်နဲ့ တသမတ်တည်း ပေါ်အောင်လုပ်ပါ။

<!DOCTYPE html>
<html>
  <head>
  </head>
</html>

Language attribute

From the HTML5 spec:

Root html element မှာ lang attribute ထည့်ရေးခြင်းအားဖြင့် document ရဲ့ ဘာသာစကားကို သတ်မှတ်ဖို့ တိုက်တွန်းပါတယ်။ ထိုသို့လုပ်ခြင်းအားဖြင့် အသံထွက်ဖတ်တဲ့ စနစ်တွေက ဘယ်ဘာသာနဲ့ အသံထွက်ရမလဲဆိုတာ၊ ဘာသာပြန်စနစ်တွေကလည်း ဘယ်လို စည်းမျဥ်းသုံးရမလဲဆိုတာ အစရှိသဖြင့် သိနိုင်ပါတယ်။

Spec ထဲမှာ lang attribute အကြောင်း ဆက်လက်ဖတ်ရှုနိုင်ပါတယ်။

ဘာသာစကား အတိုကောက် code စာရင်းကိုတော့ Sitepoint မှာ သွားရောက်ကြည့်ရှုနိုင်ပါတယ်။

<html lang="en-us">
  <!-- ... -->
</html>

IE compatibility mode

Page ကို ဘယ် IE version သုံးပြီး ပြရမလဲ ဆိုတာ <meta> tag နဲ့ ဖော်ပြခွင့်ကို Internet Explorer မှာ ခွင့်ပြုထားပါတယ်။ တခြားအကြောင်းအရာမရှိရင် IE ကို နောက်ဆုံးပေါ် edge modeသုံးဖို့ လမ်းညွန်စေချင်ပါတယ်။

For more information, read this awesome Stack Overflow article.

ပိုပြီးသိချင်ရင် ဒီ အမိုက်စား Stack Overflow ဆောင်းပါးမှာသာ ဖတ်ကြည့်ပါ

<meta http-equiv="X-UA-Compatible" content="IE=Edge">

Character encoding

Content တွေ ကောင်းကောင်းမွန်မွန်ပေါ်အောင် အလွယ်တကူ character encoding ကို သီးသန့်ကြေငြာပြီး စိတ်ချလက်ချသုံးနိုင်ပါတယ်။ အဲ့လိုဆိုရင် HTML ထဲမှာ character entities တွေ မသုံးလည်းရပါတယ်။ သူတို့ encoding က documentနဲ့ ညီရင်ပေါ့ (ပုံမှန်အားဖြင့် UTF-8 ပါ)။

<head>
  <meta charset="UTF-8">
</head>

CSS and JavaScript includes

HTML5 spec အရ၊ CSS နဲ့ Javascript files တွေထည့်တဲ့ အခါမှာ ပုံမှန်အားဖြင့် type ကို ကြေငြာပေးစရာ မလိုပါဘူး။ သူတို့တွေအတွက် text/css နဲ့ text/javascript တွေက default တန်ဖိုးတွေ မို့လို့ပါ။

HTML5 spec links

<!-- External CSS -->
<link rel="stylesheet" href="code-guide.css">

<!-- In-document CSS -->
<style>
  /* ... */
</style>

<!-- JavaScript -->
<script src="code-guide.js"></script>

Practicality over purity

HTML စံနှုန်းနဲ့ လုပ်ထုံးတွေကို လိုက်နာဖို့ ကြိုးစားပါ။ ဒါပေမဲ့ လက်တွေ့မှာ အသုံးမဝင်တာတွေကိုတော့ လိုက်လုပ်စရာမလိုပါဘူး။ တတ်နိုင်သမျှ markup နည်းနည်းနဲ့ ရှင်းရှင်းလင်းလင်း ရေးပါ။

Attribute order

ဖတ်ရတာလွယ်အောင် HTML attributes တွေကို အစဥ်လိုက် သတ်သတ်မှတ်မှတ် ရေးပါ။

Class တွေက ပြန်သုံးလို့ ကောင်းတဲ့ component တွေဖြစ်တဲ့အတွက် သူတို့က အရင်လာရပါမယ်။ Id တွေက အတိအကျသုံးရတဲ့ အရာမို့လို့ လျော့သုံးသင့်ပါတယ် (ဥပမာ၊ in-page bookmarks)။ ဒါကြောင့် သူတို့က ဒုတိယ အနေနဲ့ လာပါမယ်။

<a class="..." id="..." data-toggle="modal" href="#">
  Example link
</a>

<input class="form-control" type="text">

<img src="..." alt="...">

Boolean attributes

Boolean attribute ဆိုတာ value သတ်မှတ်ပေးစရာမလိုတဲ့အရာပါ။ XHTML မှာ value ထည့်ပေးဖို့လိုပေမဲ့ HTML5 မှာတော့ မလိုပါဘူး။

ဒီ့ထက် ပိုဖတ်ချင်ရင်တော့ WhatWG ရဲ့ on boolean attributes ဆိုတဲ့ အပိုင်းကို ကြည့်ပါ:

Element တစ်ခုမှာ boolean attribute ပါရင် true value လို့ သတ်မှတ်ပါတယ်။ မပါရင်တော့ false value လို့ မှတ်ယူပါတယ်။

မလိုအပ်ပေမဲ့ attribute value ကို ထည့်မှရမယ်ဆိုရင်တော့ ဒီ WhatWG guideline အတိုင်းလိုက်လုပ်ပါ။

Attribute ထည့်မယ်ဆိုရင် သူ့ value သည် string အလွတ် သို့မဟုတ် [...] attribute ရဲ့ အမည် (အစနဲ့ အဆုံးမှာ မမြင်ရတဲ့ whitespace တွေမပါပဲ) ဖြစ်ရပါမယ်။

တိုတိုပြောရရင် value တစ်ခုမှ မထည့်ပါနဲ့။

<input type="text" disabled>

<input type="checkbox" value="1" checked>

<select>
  <option value="1" selected>1</option>
</select>

Reducing markup

HTML ရေးရင် မလိုအပ်တဲ့ paraent element တွေကို ဖြစ်နိုင်သမျှ လျော့သုံးပါ။ အဲ့လို HTML နည်းကောင်းလုပ်ဖို့က အကြီမ်ကြိမ် ပြန်စစ် ပြန်ပြင်ဖို့ လိုပါလိမ့်မယ်။ အောက်ပါ ဥပမာကို ကြည့်ပါ။

<!-- Not so great -->
<span class="avatar">
  <img src="...">
</span>

<!-- Better -->
<img class="avatar" src="...">

JavaScript generated markup

Javascript file ထဲမှာ markup ရောရေးထားရင် ရှာရခက်၊ ပြင်ရခက်၊ ပြီးတော့ performance လည်း လျော့နည်းစေပါတယ်။ တတ်နိုင်သမျှ ရှောင်ပါ။

CSS

Syntax

ဒီမှာ သုံးသွားတဲ့ အခေါ်အဝေါ်တွေနဲ့ ပတ်သက်ပြီး မေးစရာရှိမယ်ဆိုရင် Wikipedia the Cascading Style Sheets ဆောင်းပါးရဲ့ syntax အပိုင်း ကိုကြည့်ပါ။

/* Bad CSS */
.selector, .selector-secondary, .selector[type=text] {
  padding:15px;
  margin:0px 0px 15px;
  background-color:rgba(0, 0, 0, 0.5);
  box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF
}

/* Good CSS */
.selector,
.selector-secondary,
.selector[type="text"] {
  padding: 15px;
  margin-bottom: 15px;
  background-color: rgba(0,0,0,.5);
  box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff;
}

Declaration order

သတ်ဆိုင်တဲ့ property declarations တွေကို အောက်ပါ အစီအစဥ်အတိုင်း ပေါင်းရေးသင့်ပါတယ်။

  1. Positioning
  2. Box model
  3. Typographic
  4. Visual

Positioning က ဘာကြောင့် အရင်လာရလည်း ဆိုတော့ element ကို document ရဲ့ ပုံမှန် flow ကနေ ဖယ်ထုတ်ပစ်နိုင်ပြီး box model နဲ့ ဆိုင်တဲ့ style တွေကို ပြင်ရေးလိုက်နိုင်လို့ပဲ ဖြစ်ပါတယ်။ အဲ့ဒါပြီးရင်တော့ component နေရာချပုံနဲ့ dimensions နဲ့ ဆိုင်တဲ့ Box model လာပါမယ်။

ကျန်တာတွေအကုန်လုံးက component အတွင်းမှာဖြစ်တဲ့အရာတွေ၊​ တစ်နည်းအားဖြင့် အရှေ့က အပိုင်းနှစ်ခုကို သက်ရောက်မှု မရှိတာကြောင့် နောက်ဆုံးမှာ ထားတာ ဖြစ်ပါတယ်။

Property တွေနဲ့ သူတို့ အစီအစဥ် အပြည့်အစုံကို Recessမှာ သွားရောက်ကြည့်ရှုနိုင်ပါတယ်။

.declaration-order {
  /* Positioning */
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 100;

  /* Box-model */
  display: block;
  float: right;
  width: 100px;
  height: 100px;

  /* Typography */
  font: normal 13px "Helvetica Neue", sans-serif;
  line-height: 1.5;
  color: #333;
  text-align: center;

  /* Visual */
  background-color: #f5f5f5;
  border: 1px solid #e5e5e5;
  border-radius: 3px;

  /* Misc */
  opacity: 1;
}

@import မသုံးပါနှင့်

<link> နဲ့ ယှဥ်ရင် @import က ပိုပြီး နှေးပါတယ်။ Page request တွေလည်း ပိုလုပ်ပါတယ်။ မမျှော်လင့်ထားတဲ့ ပြဿနာတွေလည်း ဖြစ်တတ်ပါတယ်။ သူ့ကို ရှောင်ပြီး တစ်ခြားနည်းလမ်း သုံးပါ -

ပိုသိချင်ရင်တော့ Steve Souders ရဲ့ ဒီ ဆောင်းပါးကို ဖတ်ကြည့်ပါ။

<!-- Use link elements -->
<link rel="stylesheet" href="core.css">

<!-- Avoid @imports -->
<style>
  @import url("more.css");
</style>

Media query placement

ဖြစ်နိုင်သမျှ media queriey တွေကို သူတို့နဲ့ ဆိုင်တဲ့ rule set အနီးမှာ ထားပေးပါ။ အားလုံးစုပြီး stylesheet တစ်ခုထဲမှာ ထည့်တာမျိုး၊ document အဆုံးမှာ ထည့်တာမျိုး မလုပ်ပါနဲ့။ အဲ့လိုလုပ်ရင် နောင်မှာ မမြင်ပဲ မေ့ကျန်ခဲ့နိုင်ပါတယ်။ အသုံးများတဲ့ ပုံစံကို ဒီမှာ ကြည့်ပါ။

.element { ... }
.element-avatar { ... }
.element-selected { ... }

@media (min-width: 480px) {
  .element { ...}
  .element-avatar { ... }
  .element-selected { ... }
}

Prefixed properties

Vendor prefixed property တွေသုံးတဲ့ အခါမှာ multi-line edit လုပ်ရလွယ်အောင် property တစ်ခုစီကို value အလိုက် ဒေါင်လိုက် စီပေးပါ။

In Textmate, use Text → Edit Each Line in Selection (⌃⌘A). In Sublime Text 2, use Selection → Add Previous Line (⌃⇧↑) and Selection → Add Next Line (⌃⇧↓).

/* Prefixed properties */
.selector {
  -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
          box-shadow: 0 1px 2px rgba(0,0,0,.15);
}

Single declarations

Declaration တစ်ခုပဲပါတဲ့ အခါမှာတော့ ဖတ်ရလွယ်၊ ပြင်ရမြန်အောင် rule set မှာ တစ်လိုင်းမဆင်းလို့ရပါတယ်။ တစ်ခုထက်ပိုတဲ့ declaration ဆိုရင်တော့ တစ်လိုင်းစီခွဲပေးပါ။

ဒီနေရာမှာ အဓိကအချက်ကတော့ error ကို ရှာနိုင်ဖို့ဖြစ်ပါတယ်။ ဥပမာ၊ CSS validator (အမှားရှာပေးတဲ့ program)က လိုင်း 183 မှာ ရေးပုံမှားနေတယ်လို့ ပြောတယ်ဆိုပါစို့။ Declaration တစ်ခုထဲဆိုရင် ရှာမတွေ့နိုင်စရာ အကြောင်းမရှိပါဘူး။ Declaration တွေများရင်တော့ လိုင်းသက်သက်စီ ခွဲထားတာက စိတ်မရှုပ်ရပါဘူး။

/* Single declarations on one line */
.span1 { width: 60px; }
.span2 { width: 140px; }
.span3 { width: 220px; }

/* Multiple declarations, one per line */
.sprite {
  display: inline-block;
  width: 16px;
  height: 15px;
  background-image: url(../img/sprite.png);
}
.icon           { background-position: 0 0; }
.icon-home      { background-position: 0 -20px; }
.icon-account   { background-position: 0 -40px; }

Shorthand notation

တမင်ကြေငြာဖို့လိုတဲ့ အခါကလွဲပြီး value အားလုံးကို အတိုကောက်ရေးတာကို လျော့ဖို့ ကြိုးစားပါ။ အတိုကောက်အဖြစ်နဲ့ အသုံးလွန်တဲ့ property တွေကတော့

တို့ပဲ ဖြစ်ပါတယ်။

အတိုကောက် ဖော်ပြလို့ရတဲ့ value တိုင်းကို မကြာခဏ သတ်မှတ်ပေးစရာမလိုပါဘူး။ ဥပမာအနေနဲ့၊ HTML heading မှာ top နဲ့ bottom margin ပဲ သတ်မှတ်ကြပါတယ်။ ဒါကြောင့် လိုအပ်တဲ့အခါမှသာ အဲ့ဒီ value နှစ်ခုကို ပြင်ရေးပါ။ အတိုကောက် property တွေကို အလွန်အကျွံသုံးခြင်းအားဖြင့် မလိုအပ်ပဲ ပြင်ရေးမိတာ(overwrite)၊ မရည်ရွယ်တဲ့ အကျိုးသက်ရောက်မှုတွေဖြစ်ပြီး အမှားများတဲ့ code ဖြစ်စေတတ်ပါတယ်။

အတိုကောက် ရေးပုံရေးနည်းနဲ့ သူတို့ ဘယ်လိုအလုပ်လုပ်လဲဆိုတာ သိချင်တဲ့ လူတွေအတွက် Mozilla Developer Network မှာshorthand properties ဆိုတဲ့ အရမ်းကောင်းတဲ့ ဆောင်းပါးတစ်ပုဒ်ရှိပါတယ်။

/* Bad example */
.element {
  margin: 0 0 10px;
  background: red;
  background: url("image.jpg");
  border-radius: 3px 3px 0 0;
}

/* Good example */
.element {
  margin-bottom: 10px;
  background-color: red;
  background-image: url("image.jpg");
  border-top-left-radius: 3px;
  border-top-right-radius: 3px;
}

Nesting in Less and Sass

မလိုအပ်ပဲ nesting လုပ်ခြင်းကို ရှောင်ပါ။ Nesting လုပ်လို့ရတယ်ဆိုတိုင်း အမြဲမလုပ်သင့်ပါ။ Parent scope အတွက် style ရေးဖို့လိုတာမျိုး၊ element အရေအတွက်များများကို nest လုပ်ဖို့လိုတာမျိုးမှသာ nesting လုပ်ဖို့ စဥ်းစားပါ။

ဆက်လက် ဖတ်ရန်၊

// Without nesting
.table > thead > tr > th {  }
.table > thead > tr > td {  }

// With nesting
.table > thead > tr {
  > th {  }
  > td {  }
}

Operators in Less and Sass

သင်္ချာ အတွက်အချက်ရေးတဲ့အခါ ဖတ်ရလွယ်အောင် တန်ဖိုး၊ ကိန်းဂဏန်း၊ သင်္ကေတတွေကြားမှာ space တစ်ခုခံပြီး ကွင်းစ၊ ကွင်းပိတ်နဲ့ ထည့်ရေးပေးပါ။

// Bad example
.element {
  margin: 10px 0 @variable*2 10px;
}

// Good example
.element {
  margin: 10px 0 (@variable * 2) 10px;
}

Comments

Code ကို ရေးသား ထိန်းသိမ်းပြုပြင်သူများမှာ လူတွေပဲ ဖြစ်ပါတယ်။ အဲ့ဒါကြောင့် မိမိ code ကို သူများ နားလည်လွယ်အောင် comment များနှင့် ရည်ရွယ်ချက်ကို ရှင်းလင်းအောင် ရေးသားပေးပါ။ ကောင်းမွန်တဲ့ code မှာ အကြောင်းအရာနဲ့ ရည်ရွယ်ချက်ကို comment များမှာ ဖော်ပြထားပါတယ်။ Component သို့ class name ကို ဒီအတိုင်းပြန်မရေးပါနဲ့။

Comment များရင် ဝါကျ အပြည့်အစုံနဲ့ ရေးပြီး မှတ်စုကြမ်းများကို စကားစု အတိုများဖြင့် ရေးပါ။

/* Bad example */
/* Modal header */
.modal-header {
  ...
}

/* Good example */
/* Wrapping element for .modal-title and .modal-close */
.modal-header {
  ...
}

Class names

Sass နှင့် Less variable အမည်များ သတ်မှတ်ရာတွင်လည်း ဤစည်းမျဥ်းများကို အသုံးပြုနိုင်ပါသည်။

/* Bad example */
.t { ... }
.red { ... }
.header { ... }

/* Good example */
.tweet { ... }
.important { ... }
.tweet-header { ... }

Selectors

ဆက်လက် ဖတ်ရှုရန်၊

/* Bad example */
span { ... }
.page-container #stream .stream-item .tweet .tweet-header .username { ... }
.avatar { ... }

/* Good example */
.avatar { ... }
.tweet-header .username { ... }
.tweet .avatar { ... }

Organization

/*
 * Component section heading
 */

.element { ... }


/*
 * Component section heading
 *
 * Sometimes you need to include optional context for the entire component. Do that up here if it's important enough.
 */

.element { ... }

/* Contextual sub-component or modifer */
.element-heading { ... }

Editor preferences

Dirty diffs နဲ့ အဖြစ်များတဲ့ code အလွဲတွေကို ရှောင်ရှားရန် သင့် editor ကို အောက်ပါ setting များ သတ်မှတ်ပေးပါ၊

Project ရဲ့ .editorconfig file မှာ စောစောက အချက်တွေကို ထည့်ရေးကြည့်ပါ။ ဥပမာအားဖြင့် Bootstrap မှာ ဘယ်လိုလုပ်ထားလဲဆိုတာကို ကြည့်ပါ။. EditorConfig အကြောင်း ဒီမှာ ဆက်လေ့လာနိုင်ပါတယ်။