CSS3執行個體教程:使用border

來源:互聯網
上載者:User

文章簡介:熟練使用border-radius.

這個執行個體的目的:熟練使用border-radius

涉及的屬性:border-radius 、linear-gradient 、 box-shadow提示:“:before” “:after”,IE對after、before是不支援的,請在firefox、opera、chrome下試調!要求瀏覽器:firefox、opera、chrome效果圖:

先看下大致的步驟:1.定義class,繪製一個矩形;2.用border-radius屬性進行對其圓角處理;3.使用pseudo元素建立蜷縮角;4.建立條文漸層的效果。詳細步驟第一步:定義class,繪製一個矩形:

<a class="docIcon" href="#">Document Icon</a>

這裡要注意下:“display“預設屬性值是“inline”,所以我們要使用“block”這個屬性值以保證其正確顯示:

  1. .docIcon {
  2. background: #eee;
  3. background: -webkit-linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);
  4. background: -moz-linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);
  5. background: -o-linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);
  6. background: -ms-linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);
  7. background: linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);
  8. border: 1px solid #ccc;
  9. display: block;
  10. width: 40px;
  11. height: 56px;
  12. position: relative;
  13. margin: 42px auto;
  14. }

這裡的linear-gradient是漸層效果設定。下面加上陰影製作效果,使用“box-shadow ”屬性來實現:

  1. .docIcon
  2. {
  3. ...
  4. -webkit-box-shadow:inset rgba(255,255,255,0.8) 0 1px 1px;
  5. -moz-box-shadow:inset rgba(255,255,255,0.8) 0 1px 1px;
  6. box-shadow:inset rgba(255,255,255,0.8) 0 1px 1px;
  7. text-indent:-9999em;
  8. }

這裡的box-shadow是陰影製作效果設定。到目前為止,我們完成了下面的效果,如圖:



第二部:用border-radius屬性進行對其圓角處理:Border-radius的定義、用法請點擊此處瞭解。代碼:
  1. .docIcon
  2. {
  3. ...
  4. -webkit-border-radius:3px 15px 3px 3px;
  5. -moz-border-radius:3px 15px 3px 3px;
  6. border-radius:3px 15px 3px 3px;
  7. }

如圖:

Ps:這裡要著重提示下:before是一個偽類別選取器,僅支援firefox、opera、chrome。大家可能對下面倆步不太瞭解,這個沒關係,我會針對它們另外寫一篇詳細的文章。在這個執行個體當中,大家只要知道我們用到過":before"":after"這個倆個偽類別選取器即可。第三部:蜷縮角

首先,添加“:before”,我們需要建立一個15px的矩形並應用背景漸層:
  1. .docIcon:before {
  2. content: "";
  3. display: block;
  4. position: absolute;
  5. top: 0;
  6. right: 0;
  7. width: 15px;
  8. height: 15px;
  9. background: #ccc;
  10. background: -webkit-linear-gradient(45deg, #fff 0, #eee 50%, #ccc 100%);
  11. background: -moz-linear-gradient(45deg, #fff 0, #eee 50%, #ccc 100%);
  12. background: -o-linear-gradient(45deg, #fff 0, #eee 50%, #ccc 100%);
  13. background: -ms-linear-gradient(45deg, #fff 0, #eee 50%, #ccc 100%);
  14. background: linear-gradient(45deg, #fff 0, #eee 50%, #ccc 100%);
  15. -webkit-box-shadow: rgba(0,0,0,0.05) -1px 1px 1px, inset white 0 0 1px;
  16. -moz-box-shadow: rgba(0,0,0,0.05) -1px 1px 1px, inset white 0 0 1px;
  17. box-shadow: rgba(0,0,0,0.05) -1px 1px 1px, inset white 0 0 1px;
  18. border-bottom: 1px solid #ccc;
  19. border-left: 1px solid #ccc;
  20. }

接下來我們要實現右上方圓角的效果,我們使用相同的方法進行對齊:

...

-webkit-border-radius:3px 15px 3px 3px;

-moz-border-radius:3px 15px 3px 3px;

border-radius:3px 15px 3px 3px;

下面是我們目前實現的效果:


第四步:添加條文漸層效果:

 

下面我們使用“:after”來實現效果,我們出示高度定位0,0。我們應用整體寬度的60%,再加上

margin-left和margin-right各20%(也就是100%):

  1. .docIcon:after
  2. {
  3. content:"";
  4. display:block;
  5. position:absolute;
  6. left:0;
  7. top:0;
  8. width:60%;
  9. margin:22px 20% 0;
  10. height:15px;
  11. }


用CSS3實現多條線漸層:

  1. .docIcon:after
  2. {
  3. ...
  4. background:#ccc;
  5. background: -webkit-linear-gradient(top, #ccc 0, #ccc 20%, #fff 20%, #fff 40%, #ccc 40%, #ccc 60%, #fff 60%, #fff 80%, #ccc 80%, #ccc 100%);
  6. background: -moz-linear-gradient(top, #ccc 0, #ccc 20%, #fff 20%, #fff 40%, #ccc 40%, #ccc 60%, #fff 60%, #fff 80%, #ccc 80%, #ccc 100%);
  7. background: -o-linear-gradient(top, #ccc 0, #ccc 20%, #fff 20%, #fff 40%, #ccc 40%, #ccc 60%, #fff 60%, #fff 80%, #ccc 80%, #ccc 100%);
  8. background: -ms-linear-gradient(top, #ccc 0, #ccc 20%, #fff 20%, #fff 40%, #ccc 40%, #ccc 60%, #fff 60%, #fff 80%, #ccc 80%, #ccc 100%);
  9. background:linear-gradient(top, #ccc 0, #ccc 20%, #fff 20%, #fff 40%, #ccc 40%, #ccc 60%, #fff 60%, #fff 80%, #ccc 80%, #ccc 100%);
  10. }



最後結果:



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.