DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
CSS3 border-radius 實作圓角邊框(含舊瀏覽器相容寫法)
編輯:布局實例     

圓角在過去得靠切圖拼湊,CSS3 之後一個屬性就能搞定。以下是完整示範。

核心四行

-moz-border-radius: 32px;
-webkit-border-radius: 32px;
border-radius: 32px;
behavior: url(border-radius.htc);

前兩行是 Firefox 與 WebKit 的私有前綴,第三行是標準屬性,第四行 behavior 則是引入 htc 行為檔,用來讓 IE 也能理解圓角。

完整程式碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body {
  background-color: #fff;
  font: normal 11pt Trebuchet MS, Arial, sans-serif;
}
.box1 {
  background-color: #f0f0f0;
  width: 533px;
  height: 50px;
  margin: 0 auto 50px auto;
  padding: 20px;
  border: 1px solid #d7d7d7;
  -moz-border-radius: 11px;
  -webkit-border-radius: 11px;
  border-radius: 10px;
}
.box2 {
  background: transparent url(ashera.jpg) no-repeat top left;
  width: 420px;
  height: 220px;
  margin: 0 auto 35px auto;
  padding: 30px;
  color: #fff;
  font-weight: bold;
  border: 11px solid #35b70e;
  -moz-border-radius: 32px;
  -webkit-border-radius: 32px;
  border-radius: 32px;
  behavior: url(border-radius.htc);
}
.box3 {
  background-color: #ddd;
  width: 210px;
  height: 30px;
  padding: 20px;
  position: absolute;
  top: 5px;
  left: 5px;
  -moz-border-radius: 15px;
  -webkit-border-radius: 15px;
  border-radius: 15px;
  behavior: url(border-radius.htc);
}
.rel {
  margin: 50px 0 0 33px;
  padding: 25px;
  position: relative;
  z-index: inherit;
  zoom: 1; /* For IE6 */
}
</style>
</head>
<body>
<p>Not just divs, but any element*</p>
<div class="rel">
<div class="box1">
11 lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet
</div>
<div class="box2">
22 this cat is a hybrid of domestic and wild breeds :)
</div>
<div class="box3">
33 this box is absolutely positioned
</div>
</div>
</body>
</html>

幾個實用補充

私有前綴如今已無必要,現代瀏覽器全面支援標準的 border-radius,behavior 那行也可以移除。若仍需支援 IE8,可保留 htc 方案,但要注意它對背景裁切與 border 樣式的支援有限。

想要只圓某一個角,可以用四值寫法或單角屬性:

border-radius: 10px 0 10px 0;          /* 左上 右上 右下 左下 */
border-top-left-radius: 15px;           /* 只圓左上角 */

若數值超過元素尺寸的一半,瀏覽器會自動按比例縮放,因此寫 border-radius: 50% 就能在正方形元素上做出正圓,在長方形上做出膠囊形——這也是頭像框最常用的一招。

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