CSS3開發:常用的20個css

來源:互聯網
上載者:User
1. 黑白映像

這段代碼會讓你的彩色照片顯示為黑白照片,是不是很酷。

img.desaturate {
    filter: grayscale(100%);
    -webkit-filter: grayscale(100%);
    -moz-filter: grayscale(100%);
    -ms-filter: grayscale(100%);
    -o-filter: grayscale(100%);
}
2. 使用 :not() 在菜單上應用/取消應用邊框

先給每一個功能表項目添加邊框

/* add border */
.nav li {
  border-right: 1px solid #666;
}

……然後再除去最後一個元素……

// remove border /
 
  
.nav li:last-child {
  border-right: none;
}

……可以直接使用 :not() 偽類來應用元素:

.nav li:not(:last-child) {
  border-right: 1px solid #666;
}

這樣代碼就乾淨,易讀,易於理解了。

當然,如果你的新元素有兄弟元素的話,也可以使用通用的兄弟選擇符(~):

..nav li:first-child ~ li {
 
  
  border-left: 1px solid #666;
}
3. 頁面頂部陰影

下面這個簡單的 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;
}
4. 給 body 添加行高

你不需要分別添加 line-height 到每個p,h標記等。只要添加到 body 即可:

body {
  line-height: 1;
}

這樣文本元素就可以很容易地從 body 繼承。 5. 所有一切都垂直置中

要將所有元素垂直置中,太簡單了:

html, body {
  height: 100%;
  margin: 0;
}
 
  
body {
  -webkit-align-items: center;  
  -ms-flex-align: center;  
  align-items: center;
  display: -webkit-flex;
  display: flex;
}

看,是不是很簡單。

注意:在IE11中要小心flexbox。 6. 逗號分隔的列表

讓html清單項目看上去像一個真正的,用逗號分隔的列表:

ul > li:not(:last-child)::after {
相關文章

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.