前端筆記:幫助你美化網站的10+實用 CSS3 技巧


  CSS3 規范讓前端開發人員能夠創建出各種復雜的視覺效果,使網站更好看,更能夠吸引用戶訪問。這篇文章中,我收集了一組實用的 CSS3 技巧,能夠幫助你美化您的網站,並給它一個更專業的外觀和感覺。

黑白圖像

下面的 CSS 代碼能夠把彩色圖像轉變成黑白風格:

img.desaturate { 
    filter: grayscale(100%);
    -webkit-filter: grayscale(100%);
    -moz-filter: grayscale(100%);
    -ms-filter: grayscale(100%);
    -o-filter: grayscale(100%);
}

  在線演示

 

頁面頂部陰影

下面這個簡單的 CSS3 代碼片段可以給網頁加上漂亮的頂部陰影效果:

body:before {
          content: "";
          position: fixed;
          top: -10px;
          left: 0;
          width: 100%;
          height: 10px;

          -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
          -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
          box-shadow: 0px 0px 10px rgba(0,0,0,.8);

          z-index: 100;
}

  在線演示

 

檢測鼠標雙擊

  不管您相信與否,使用 CSS 就能夠檢測出元素是否被雙擊:

  HTML:

<div class="test3">
  <span><input type="text" value=" " readonly="true" />
  <a href="http://google.com">Double click me</a></span>
</div>

  CSS:

.test3 span {
  position: relative;
}
.test3 span a { 
  position: relative;
  z-index: 2; 
}
.test3 span a:hover, .test3 span a:active { 
  z-index: 4; 
}
.test3 span input { 
  background: transparent; 
  border: 0; 
  cursor: pointer; 
  position: absolute; 
  top: -1px; 
  left: 0; 
  width: 101%;  /* Hacky */
  height: 301%; /* Hacky */
  z-index: 3; 
}
.test3 span input:focus { 
  background: transparent; 
  border: 0; 
  z-index: 1; 
}

  在線演示

 

CSS 實現三角形

這其實是一個古老的技巧,不需要用到 CSS3 新特性就能實現:

/* create an arrow that points up */
div.arrow-up {
  width:0px; 
  height:0px; 
  border-left:5px solid transparent;  /* left arrow slant */
  border-right:5px solid transparent; /* right arrow slant */
  border-bottom:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}

/* create an arrow that points down */
div.arrow-down {
  width:0px; 
  height:0px; 
  border-left:5px solid transparent;
  border-right:5px solid transparent;
  border-top:5px solid #2f2f2f;
  font-size:0px;
  line-height:0px;
}

/* create an arrow that points left */
div.arrow-left {
  width:0px; 
  height:0px; 
  border-bottom:5px solid transparent;  /* left arrow slant */
  border-top:5px solid transparent; /* right arrow slant */
  border-right:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}

/* create an arrow that points right */
div.arrow-right {
  width:0px; 
  height:0px; 
  border-bottom:5px solid transparent;  /* left arrow slant */
  border-top:5px solid transparent; /* right arrow slant */
  border-left:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}

  在線演示

 

CSS3 calc() 的使用

calc() 用法類似於函數,能夠給元素設置動態的值:

/* basic calc */
.simpleBlock {
  width: calc(100% - 100px);
}

/* calc in calc */
.complexBlock {
  width: calc(100% - 50% / 3);
  padding: 5px calc(3% - 2px);
  margin-left: calc(10% + 10px);
}

  在線演示

 

文本漸變

文本漸變效果很流行,使用 CSS3 能夠很簡單就實現:

h2[data-text] {
  position: relative;
}
h2[data-text]::after {
  content: attr(data-text);
  z-index: 10;
  color: #e3e3e3;
  position: absolute;
  top: 0;
  left: 0;
  -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,1)), to(rgba(0,0,0,0)));

  在線演示

 

禁用鼠標事件

CSS3 新增的 pointer-events 讓你能夠禁用元素的鼠標事件,例如,一個連接如果設置了下面的樣式就無法點擊了。

.disabled { pointer-events: none; }

  點擊試試

 

盒子效果

下面的代碼可以實現一個漂亮的盒子效果:

p {
  padding: 5px 10px;
  margin: 10px;
  background: #ff0030;
  color: #fff;
  font-size: 21px;
  line-height: 1.3em;
  border: 2px dashed #fff;
  border-radius: 3px;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  -moz-box-shadow: 0 0 0 4px #ff0030, 2px 1px 4px 4px rgba(10,10,0,.5);
  -webkit-box-shadow: 0 0 0 4px #ff0030, 2px 1px 4px 4px rgba(10,10,0,.5);
  box-shadow: 0 0 0 4px #ff0030, 2px 1px 6px 4px rgba(10,10,0,.5);
  text-shadow: -1px -1px #aa3030;
}

  在線演示

 

自定義滾動條

過去一直都只有 IE 才能設置滾動條樣式,現在好了,Webkit 也提供了設置滾動條的屬性:

::-webkit-scrollbar {
  width: 12px;
}

::-webkit-scrollbar-track {
  background: none;
}

::-webkit-scrollbar-thumb {
  background: -webkit-linear-gradient(left, #547c90, #002640);
  border: 1px solid #333;
  box-shadow: inset 1px 0 0 rgba(255, 255, 255, 0.4);
}

  在線演示

 

模糊文本

簡單但很漂亮的文本模糊效果,簡單又好看!

.blur {
   color: transparent;
   text-shadow: 0 0 5px rgba(0,0,0,0.5);
}

  在線演示

 

圓角絲帶效果

這段代碼有點長,但是圓角絲帶效果很奇特!

<div class="wrapper">
       <div class="ribbon-wrapper-green"><div class="ribbon-green">NEWS</div></div>
</div>

  CSS:

.wrapper {
  margin: 50px auto;
  width: 280px;
  height: 370px;
  background: white;
  border-radius: 10px;
  -webkit-box-shadow: 0px 0px 8px rgba(0,0,0,0.3);
  -moz-box-shadow:    0px 0px 8px rgba(0,0,0,0.3);
  box-shadow:         0px 0px 8px rgba(0,0,0,0.3);
  position: relative;
  z-index: 90;
}

.ribbon-wrapper-green {
  width: 85px;
  height: 88px;
  overflow: hidden;
  position: absolute;
  top: -3px;
  right: -3px;
}

.ribbon-green {
  font: bold 15px Sans-Serif;
  color: #333;
  text-align: center;
  text-shadow: rgba(255,255,255,0.5) 0px 1px 0px;
  -webkit-transform: rotate(45deg);
  -moz-transform:    rotate(45deg);
  -ms-transform:     rotate(45deg);
  -o-transform:      rotate(45deg);
  position: relative;
  padding: 7px 0;
  left: -5px;
  top: 15px;
  width: 120px;
  background-color: #BFDC7A;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#BFDC7A), to(#8EBF45)); 
  background-image: -webkit-linear-gradient(top, #BFDC7A, #8EBF45); 
  background-image:    -moz-linear-gradient(top, #BFDC7A, #8EBF45); 
  background-image:     -ms-linear-gradient(top, #BFDC7A, #8EBF45); 
  background-image:      -o-linear-gradient(top, #BFDC7A, #8EBF45); 
  color: #6a6340;
  -webkit-box-shadow: 0px 0px 3px rgba(0,0,0,0.3);
  -moz-box-shadow:    0px 0px 3px rgba(0,0,0,0.3);
  box-shadow:         0px 0px 3px rgba(0,0,0,0.3);
}

.ribbon-green:before, .ribbon-green:after {
  content: "";
  border-top:   3px solid #6e8900;   
  border-left:  3px solid transparent;
  border-right: 3px solid transparent;
  position:absolute;
  bottom: -3px;
}

.ribbon-green:before {
  left: 0;
}
.ribbon-green:after {
  right: 0;
}

  在線演示

您可能感興趣的相關文章

 

本文鏈接:分享10個幫助你美化網站的 CSS3 技巧【附源碼】

編譯來源:夢想天空 ◆ 關注前端開發技術 ◆ 分享網頁設計資源


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM