wsj lab:: CSS - width settings part 1

In the old time, I used to put a value such as 99% or 98% for the width in CSS in order to get the layout look like the one you are reading right now. I don’t remember the actual reason why I did that, but it somehow looks dumb when I just need a simple frame around my content block. Why it looks dumb? Because I always need the time to try and see whether 99% or 98% is better to fit the window width so the horizontal scrollbar won’t show up.
Therefore, I decide to dig in a little bit and see what it is actually going on with this width thing…
» 翻中文
  1. When you have !DOCTYPE element in your XHTML document,
    the standards-compliance in IE will be turned on,
    i.e. The layout you define in CSS will look closer (... I am not saying "the same" here) to what it shown on Firefox.
  2. Since the standard-compliance is turned on, the values, especially the percentage ones,
    you set for the width of container object such as div will be calculated differently.
  3. sample1a.html:
    This sample shows you how the 100% of width is calculated with !DOCTYPE element is presented
  4. sample1b.html:
    This one shows you how the same settings looks differently on IE with !DOCType element is NOT presented.
    And you won't be find anything difference between this two samples on Firefox.
  5. In conclusion, I will probably skip the width definition and work on the body’s padding property instead
    when the only thing I want is a frame around my content block.
[x]
我以前在做你現在看到這樣內容周圍要有一個框的版面時,想到的總是將這個區塊的寬度設定為 99% 或是 98%。 我不記得當時這樣做的確實原因了,不過現在看起來感覺有點蠢。為什麼呢? 因為每次為了要把那不必要的橫向捲軸拿掉,總是需要花時間去試到底寬度該設 99% 還是 98%。
所以,我決定稍微鑽進去看看這個東西的問題到底出在哪裡...
  1. XHTML 文件裏有 !DOCTYPE 定義時,IE 裡符合標準模式的定義功能會被打開, 也就是說你在 CSS 裡設定的版面配置看起來會比較接近 Firefox (... 在這裡,我不敢說會完全一樣)
  2. 因為符合標準設定的模式被打開了,所以區塊寬度的計算也就會用不同的基準做。
  3. sample1b.html:
    這個範例讓你看看當 !DOCTYPE 存在時,寬度設 100% 時的狀況
  4. sample1b.html:
    這個讓你看看當 !DOCTYPE 被拿掉時,同樣的文件在 IE 上會有什麼樣不同的變化
  5. 結論:將來我再做類似這樣需要框的版面時應該會考慮用 body 的 padding 來代替那些區塊的寬度設定