DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
CSS 實例教學:用清單與邊框做出精緻列表選單
編輯:CSS詳解     

這是一個相當實用的小技巧,教你如何運用清單的邊框樣式與背景圖像,做出別緻的列表選單。核心訣竅在於利用清單的「相對定位」來靈活調整整齊的邊框位置。這個技巧非常靈活,你能輕鬆改變邊框與背景的配置,甚至還能讓瀏覽器的字體自由縮放而不跑版。

一、觀察 HTML 結構

先仔細看看下面的 HTML,搭配示意圖,能幫助你更清楚每段程式碼的作用:

<ul>
  <li><strong>CSS Design</strong> <em>250<sup>95</sup></em></li>
</ul>

二、關鍵的定位技巧

做法是:先給 <li> 設 position: relative,並加上一條底邊框;接著給 <strong> 與 <em> 設 position: absolute,並用負值把它們移到邊框「之下」。特別注意:這裡的間距要用相對單位來控制,才能配合字體縮放。

.menu {
  width: 500px;
  list-style: none;
  margin: 0 0 2em;
  padding: 0;
  font: 150%/100% Arial, Helvetica, sans-serif;
}
.menu li {
  clear: both;
  margin: 0;
  padding: 0 0 1.8em 0;
  position: relative;
  border-bottom: dotted 2px #999;
}
.menu strong {
  background: #fff;
  padding: 0 10px 0 0;
  font-weight: normal;
  position: absolute;
  bottom: -.3em;
  left: 0;
}
.menu em {
  background: #fff;
  padding: 0 0 0 5px;
  font: 110%/100% Georgia, "Times New Roman", Times, serif;
  position: absolute;
  bottom: -.2em;
  right: 0;
}
.menu sup {
  font-size: 60%;
  color: #666;
  margin-left: 3px;
}

三、調整清單項目的樣式

接著針對 li 設定邊框與內距:

li {
  border-bottom: dashed 1px #000;
  padding: 0 0 2.3em 0;
}

四、用背景圖片取代邊框

除了實線或虛線邊框,你也可以用背景圖片來製作分隔線,做出更豐富的質感:

li {
  background: url(images/circle.gif) repeat-x left bottom;
}

五、處理舊版 IE 的相容問題

如果你仍在使用 IE6 以下版本,或是有 bug 的 IE6,可能會發現版面顯示異常。解決方式很簡單——替 <li> 加上 clearfix:

/* clearfix */
.menu li:after {
  content: ".";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}
.menu li {display: inline-block;}
/* Hides from IE-Mac \*/
* html .menu li {height: 1%;}
.menu li {display: block;}
/* End hide from IE-Mac */

透過相對定位搭配絕對定位的巧妙組合,你就能把文字、數值與分隔線安排得恰到好處,做出兼具美感與彈性的列表式選單。

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