DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
CSS 實戰技巧與經驗總結(二):30 個常見問題一次破解
編輯:CSS詳解     

以下依主題整理日常開發中反覆遇到的 CSS 難題與解法。

版面與定位

讓元素充滿整個頁面

html, body { height: 100%; margin: 0; }
#test { height: 100%; }

讓元素距窗口上右下左各 10px

html, body { height: 100%; margin: 0; }
#test {
  position: absolute;
  top: 10px; right: 10px; bottom: 10px; left: 10px;
}

让整个页面水平居中

body { text-align: center; }
#test { width: 960px; margin: 0 auto; text-align: left; }

把 body 的 text-align 設為 center,可以讓連 IE5.5 都能正確居中;再用 margin: 0 auto 完成標準模式下的居中,並在容器內把文字對齊還原為 left。

已知高度的容器水平垂直居中

#test {
  position: absolute;
  top: 50%; left: 50%;
  width: 200px; height: 200px;
  margin: -100px 0 0 -100px;
}

原理是先移到中心,再用負 margin 補回自身尺寸的一半。

未知尺寸的圖片在已知寬高容器內居中

#test {
  display: table-cell;
  width: 200px; height: 200px;
  text-align: center; vertical-align: middle;
  *display: block; *position: relative;   /* IE6/7 hack */
}
#test p { *position: absolute; *top: 50%; *left: 50%; margin: 0; }
#test p img { *position: relative; *top: -50%; *left: -50%; vertical-align: middle; }

現代方案可直接用 flex:display:flex; align-items:center; justify-content:center;。

單行文字在容器內垂直居中

#test { height: 25px; line-height: 25px; }

讓行高與容器高度相等即可。

浮動與清除

容器背景色顯示不出來、高度無法自適應

絕大多數情況是子元素浮動後脫離了文件流,父容器高度塌陷所致。除錯時第一時間就該檢查是否有未清除的浮動。

三種清除浮動的方法

/* 方法一:在浮動元素的下一個兄弟元素上清除 */
#test { clear: both; }

/* 方法二:在父元素上觸發 BFC */
#test { display: block; zoom: 1; overflow: hidden; }

/* 方法三:偽元素清除(推薦) */
#test { zoom: 1; }
#test::after {
  content: '';
  display: block;
  clear: both;
  visibility: hidden;
  height: 0;
}

相鄰 div 的 margin 為何只生效一個?

.box1 { margin: 10px 0; }
.box2 { margin: 20px 0; }

實際間距是 20px,而非 30px,這稱為外邊距合併(margin collapse),取兩者中的最大值。父子元素之間同樣會發生。幾點注意事項:

  • 只發生在區塊級元素上。
  • 浮動元素不會與相鄰元素合併。
  • 絕對定位元素不會與相鄰元素合併。
  • 行內區塊級元素之間不會合併。
  • 根元素之間(如 html 與 body)不會合併。
  • 設定了非 visible 的 overflow 的區塊元素,不會與其子元素合併。

圖片下方為何多出幾像素空白?

img { display: block; }        /* 方法一 */
img { vertical-align: top; }   /* 方法二 */
#test { font-size: 0; line-height: 0; }  /* 方法三,設在父元素上 */

原因是圖片預設為行內元素,會依基線對齊而在下方留出預留空間。

透明度

容器透明、內容不透明

/* 方法一:容器與內容並列,用負 margin 疊上去 */
.outer { width: 200px; height: 200px; background: #000; filter: alpha(opacity=20); opacity: .2; }
.inner { width: 200px; height: 200px; margin-top: -200px; }

/* 方法二:現代瀏覽器直接用 rgba,舊 IE 讓子元素相對定位 */
.outer {
  width: 200px; height: 200px;
  background: rgba(0, 0, 0, .2);
  background: #000\9;
  filter: alpha(opacity=20)\9;
}
.outer .inner { position: relative\9; }

關鍵觀念:opacity 會連帶影響所有子元素,因此若只想讓背景半透明,優先用 rgba() 指定背景色。

表格

做出 1px 細邊框的表格

/* 方法一:合併相鄰邊框 */
#test { border-collapse: collapse; border: 1px solid #ddd; }
#test th, #test td { border: 1px solid #ddd; }

/* 方法二:用間距加背景色模擬 */
#test { border-spacing: 1px; background: #ddd; }
#test tr { background: #fff; }

IE7 及更早版本不支援 border-spacing,可用 table 的 cellspacing 屬性替代。

文字與行高

讓行距始終維持 n 倍字級

body { line-height: 1.5; }

注意不要加上單位,寫成倍數才能讓行高隨字級自動縮放。

文字溢出不換行、強制單行顯示

#test { width: 150px; white-space: nowrap; }

文字溢出顯示省略號

#test {
  width: 150px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

三個屬性缺一不可:先強制單行,再截斷溢出內容,最後指定截斷處顯示省略號。

連續長字串自動換行

#test { width: 150px; word-wrap: break-word; }

盒模型

標準模式與怪異模式(Quirks mode)下的寬度計算不同:

標準模式:element width = width + padding + border
怪異模式:element width = width

如今可用 CSS3 的 box-sizing: border-box 主動指定想要哪一種計算方式。

以圖換字的四種思路

思路一:文字負縮進

.test {
  width: 200px; height: 50px;
  text-indent: -9999px;
  background: #eee url(icon.png) no-repeat;
}

結構簡潔,但缺點明顯:負值隨情境而異不易抽成通用樣式;元素為連結時非 IE 瀏覽器的虛線框會不完整;設為行內或行內區塊時跨瀏覽器差異較大。

思路二:隱藏文字

.test { width: 200px; height: 50px; background: #eee url(icon.png) no-repeat; }
.test span { visibility: hidden; }   /* 或 display: none */

相容性最好、最容易抽成通用類別,代價是 HTML 結構多一層。

思路三:把文字擠出容器

.test {
  overflow: hidden;
  width: 200px; height: 0; padding-top: 50px;
  background: #eee url(icon.png) no-repeat;
}

結構簡潔,但 padding 數值不易通用,且要兼顧 IE5.5 及更早版本還得加 hack。

思路四:超小字級+文字全透明(推薦)

.test {
  overflow: hidden;
  width: 200px; height: 50px;
  font-size: 0; line-height: 0;
  color: rgba(0, 0, 0, 0);
  background: #eee url(icon.png) no-repeat;
}

結構簡單、易於複用,是目前最實用的做法。不過在 SEO 與無障礙角度上,更推薦使用 CSS 的 background 搭配 HTML5 的 <img> + alt,或善用 clip-path 這類標準手法。

超連結與游標

去掉超連結的虛線框

a { outline: none; }

讓連結的 four states 顏色各異且 hover 仍有效

a:link    { color: #03c; }
a:visited { color: #666; }
a:hover   { color: #f30; }
a:active  { color: #c30; }

順序必須是 LoVe HAte(link-visited-hover-active),這個口訣正好幫你記住順序不能顛倒。

游標顯示為手型且相容各瀏覽器

#test { cursor: pointer; }

寫成 hand 只有 IE 與 Opera 支援,且屬非標準值。

表單與清單

文字與輸入框垂直對齊

input { vertical-align: middle; }

讓行內元素可以設定寬高

span { display: block; width: 200px; height: 100px; }

只要把行內元素轉為區塊級或行內區塊級即可,除了 display,float 與 position 同樣能達到效果。

標準模式下 IE 無法設定捲軸顏色

html {
  scrollbar-3dlight-color: #999;
  scrollbar-darkshadow-color: #999;
  scrollbar-highlight-color: #fff;
  scrollbar-shadow-color: #eee;
  scrollbar-arrow-color: #000;
  scrollbar-face-color: #ddd;
  scrollbar-track-color: #eee;
  scrollbar-base-color: #ddd;
}

把原本寫在 body 上的捲軸樣式移到 html 選擇器即可。

讓 list-style-image 的項目符號精準定位

別用 list-style-image,改用 background-image 搭配 background-position 定位,控制力更精確。

一元素套用多組規則

.a { color: #f00; }
.b { background: #eee; }
.c { background: #ccc; }

<div class="a b c">測試</div>

多個 class 之間以空格分隔,且只有 class 支援這種寫法,id 不行。

幾個跨瀏覽器的偽元素限制

在撰寫當時的測試環境(IE6–IE10、Firefox 6.0、Chrome 13.0、Safari 5.1、Opera 11.51)中觀察到:

  • :before/:after 加在 <input> 上,除 Opera 外皆無效果,而 Opera 的表現也與預期不同。
  • :before/:after 無法定義 transition 與 animation,只有 Firefox 支援。

原因是偽元素作用在取代型元素(如 input、img)上並不符合規範。若效果是必要需求,請改用真實元素,或把樣式掛在 input 的外層容器上。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved