天天看點

20個很有很有很有用的CSS技巧

1.placeholder的顔色值設定

input::-webkit-input-placeholder{color:#AAAAAA;}
input:focus::-webkit-input-placeholder{color:#EEEEEE;}      

2.雙飛翼布局

<style type="text/css">.box-main,.box-left,.box-right{float:left;height: 50px;}
    .box{padding: 0 300px;min-width: 400px;}
    .box-main{background: #03a9f4;width: 100%;}
    .box-left{background: #00bcd4;width: 300px;margin-left: -100%;position: relative;left: -300px;}
    .box-right{background: #00bcd4;width: 300px;margin-left: -300px;position: relative;right: -300px;}</style>  

<div class="box">
    <div class="box-main">主體</div>
    <div class="box-left">左側</div>
    <div class="box-right">右側</div>
</div>      

3. 黑白圖像

//這段代碼會讓你的彩色照片顯示為黑白照片
img.desaturate {
    filter: grayscale(100%);
    -webkit-filter: grayscale(100%);
    -moz-filter: grayscale(100%);
    -ms-filter: grayscale(100%);
    -o-filter: grayscale(100%);
}      

4.禁止ios 長按時不觸發系統的菜單,禁止ios&android長按時下載下傳圖檔

.css{-webkit-touch-callout: none}      

5.禁止ios和android使用者選中文字

.css{-webkit-user-select:none}      

6. 使用 :not() 在菜單上應用/取消應用邊框

//先給每一個li菜單項添加邊框
.nav li {
  border-right: 1px solid #666;
}
//然後再除去最後一個元素
.nav li:last-child {
  border-right: none;
}

//直接使用 :not() 僞類來應用元素
.nav li:not(:last-child) {
  border-right: 1px solid #666;
}      

7. 頁面頂部陰影

//下面這個簡單的 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;
}      

8. 給 body 添加行高

//不需要分别添加 line-height 到每個p,h标記等。隻要添加到 body 即可:
body {
  line-height: 1;
}      

9. 所有一切都垂直居中

//注:在IE11中要小心flexbox。
html, body {
  height: 100%;
  margin: 0;
}

body {
  -webkit-align-items: center;  
  -ms-flex-align: center;  
  align-items: center;
  display: -webkit-flex;
  display: flex;
}      

10. 逗号分隔的清單

//讓HTML清單項看上去像一個真正的,用逗号分隔的清單(如下圖):
ul > li:not(:last-child)::after {
  content: ",";
}      
20個很有很有很有用的CSS技巧

11. 使用負的 nth-child 選擇項目

//在CSS中使用負的 nth-child 選擇項目1到項目n。
li {
  display: none;
}

li:nth-child(-n+3) {
  display: block;
}      
20個很有很有很有用的CSS技巧

12. 對圖示使用 SVG

//SVG對所有的分辨率類型都具有良好的擴充性 并支援所有浏覽器都回歸到IE9了。
.logo {
  background: url("logo.svg");
}      

13. 優化顯示文本

//有時,字型并不能在所有裝置上都達到最佳的顯示,是以可以讓裝置浏覽器來幫助你:
html {
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}      

14. 旋轉動畫

//loading圖的旋轉動畫
.loading{
  animation: changehovertree 1.5s linear infinite;
  -webkit-animation: changehovertree 1.5s linear infinite;
}
@-webkit-keyframes {
  0% {-webkit-transform:rotate(0)}
  50% {-webkit-transform:rotate(180deg)}
  100% {-webkit-transform:rotate(360deg)}
}
@keyframes {
  0% {transform:rotate(0)}
  50% {transform:rotate(180deg)}
  100% {transform:rotate(360deg)}
}      

15. 繼承 box-sizing

//讓 box-sizing 繼承 html:
html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
}      

16. 文本溢出

/*單行文本溢出*/  
.ellipsis{
    white-space:nowrap  ;
    text-overflow:ellipsis;
    overflow:hidden
}  

/*多行文本溢出*/  
.ellipsis{
    display: -webkit-box!important;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: break-all;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;      //展示3行 
}      

17.CSS 寫出三角形

//利用border來寫三角形代碼,并且相容IE6.

/* 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;
}      

18.文本漸變

h2 {
  display: inline-block;
  color: green;
  font-size: 30px;
  background-image: -webkit-gradient(linear, 0 0, 0 bottom, from(rgba(0, 128, 0, 1)), to(rgba(51, 51, 51, 1)));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}      
20個很有很有很有用的CSS技巧

19. 禁用滑鼠事件

//連結如果設定了下面的樣式就無法點選了
.disabled { 
  pointer-events: none; 
}      
.blur {
   color: transparent;
   text-shadow: 0 0 5px rgba(0,0,0,0.5);
}