注意:以下前綴兼容性寫法注釋
-o-:Opera
-ms://IE10
-moz://火狐
-webkit://Safari 4-5, Chrome 1-9
29. 移動端1px邊框
.border-1px {
position: relative;
&:after {
content: '';
display: block;
position: absolute;
left: 0;
right: 0;
bottom: 0;
border-top: 1px solid red;
}
}
@media (-webkit-min-device-pixel-ratio: 1.5) {
.border-1px {
&::after {
transform: scaleY(.7);
}
}
}
@media (-webkit-min-device-pixel-ratio: 2) {
.border-1px {
&::after {
transform: scaleY(.5);
}
}
}
28. 使用div實現(xiàn)input效果(實現(xiàn)一段可編輯的段落)
<div contenteditable="true">這是一個可編輯的段落底瓣。</div>
// 只需要加contenteditable="true"屬性就可以使div內(nèi)容可編輯
27. select內(nèi)容居中顯示
select{
text-align: center;
text-align-last: center;
}
26. nth-last-child偽類選擇最后四個元素
div:nth-last-child(-n+4){
border-bottom: 2px solid red;
}
25. 禁止點擊事件/鼠標(biāo)事件“穿透”
div * {
pointer-events: none; /*鏈接啊翻具,點擊事件啊,都沒有效果了*/
}
除了指示該元素不是鼠標(biāo)事件的目標(biāo)之外棘劣,值"none"表示鼠標(biāo)事件“穿透”該元素并且指定該元素“下面”的任何元素。
24. 用來控制元素在移動設(shè)備上使用滾動回彈效果
.main{
-webkit-overflow-scrolling: touch;
}
23. 文字漸變效果
.text-gradient{
background-image: linear-gradient(135deg, deeppink, deepskyblue);
-webkit-background-clip: text;
color: transparent;
}
22. IOS鍵盤字母輸入村生,默認首字母大寫诀艰,解決方案
<input type="text" autocapitalize="off" />
21. select 下拉選擇設(shè)置右對齊
select option {
direction: rtl;
}
20. 去除 iOS移動端 input颂砸,textarea輸入框上方內(nèi)陰影樣式
CSS代碼:
input, textarea, select{
appearance: none;
-moz-appearance: none; /*Firefox */
-webkit-appearance: none; /*Safari 和 Chrome*/
}
appearance屬性,用來移除原生控件樣式谣沸。亦可以用來去除 select 默認樣式右側(cè)的下拉小三角形刷钢。
19. 自定義滾動條樣式
整體部分 ::-webkit-scrollbar
兩端按鈕 ::-webkit-scrollbar-button
外層軌道 ::-webkit-scrollbar-track
內(nèi)層軌道 ::-webkit-scrollbar-track-piece
滾動滑塊 ::-webkit-scrollbar-thumb
邊角 ::-webkit-scrollbar-corner
邊角拖動塊的樣式 ::-webkit-resizer
橫向滾動條
CSS代碼:
.scroll-horizontal::-webkit-scrollbar{
height: 10px;
}
.scroll-horizontal::-webkit-scrollbar-button{
display: block;
width: 5px;
border: 5px solid transparent;
}
.scroll-horizontal::-webkit-scrollbar-button:start:decrement{
border-right-color: red;
}
.scroll-horizontal::-webkit-scrollbar-button:end:increment{
border-left-color: red;
}
.scroll-horizontal::-webkit-scrollbar-button:end:decrement{
display: none;
}
.scroll-horizontal::-webkit-scrollbar-button:start:increment{
display: none;
}
.scroll-horizontal::-webkit-scrollbar-thumb{
background-color: green;
border-radius: 30px;
}
.scroll-horizontal::-webkit-scrollbar-track-piece{
/* background-color: #0898b2; */
border-radius: 30px;
}
.scroll-horizontal::-webkit-scrollbar-track{
border: 1px solid #721f1f;
border-radius: 30px;
margin: 0 5px;
}
豎向滾動條
CSS代碼:
.scroll-vertical::-webkit-scrollbar{
width: 10px;
}
.scroll-vertical::-webkit-scrollbar-button{
display: block;
height: 10px;
border: 5px solid transparent;
}
.scroll-vertical::-webkit-scrollbar-button:end:increment{
border-top-color: #1166DF;
}
.scroll-vertical::-webkit-scrollbar-button:start:decrement {
border-bottom-color: #1166DF;
}
.scroll-vertical::-webkit-scrollbar-button:end:decrement,
.scroll-vertical::-webkit-scrollbar-button:start:increment{
display: none;
}
.scroll-vertical::-webkit-scrollbar-track-piece{
border-radius: 30px;
background-color: #0b97b0;
margin: 5px auto;
}
.scroll-vertical::-webkit-scrollbar-thumb{
border-radius: 30px;
background-color: green;
}
18. 實現(xiàn)文字豎向排版
實現(xiàn)文字豎向排版,最簡單的方法是設(shè)置較小的寬度乳附,然后高度自適應(yīng)内地。
p{
width: 25px;
line-height: 18px;
height: auto;
font-size: 12px;
padding: 8px 5px;
}
但是當(dāng)想要多列展示時赋除,設(shè)置寬度就不能實現(xiàn)我們想要的效果了阱缓,此時可以使用
writing-mode 屬性。
p{
height: 210px;
line-height: 30px;
text-align: justify;
writing-mode: vertical-lr; //從左向右
writing-mode: tb-lr; //IE從左向右
//writing-mode: vertical-rl; -- 從右向左
//writing-mode: tb-rl; -- 從右向左
}
17. 實現(xiàn)按比例計算淺紅到深紅的顏色值
設(shè)置 rgb举农,固定 r 值為 ff, 讓 g = b荆针,從 ff 漸變到 0
16. calc 可以使我們不用考慮元素的寬度值,直接交給瀏覽器去計算
當(dāng)我們遇到左側(cè)寬度值固定颁糟,右側(cè)寬度值不定的時候航背,使用 calc 去設(shè)置元素的寬度是最好的解決辦法。
在IE9+滚停、FireFox4.0+沃粗、Chrome19+、Safari6+都有較好的支持
支持 " + " 键畴、" - " 最盅、 " * " 突雪、" / " (運算符前后要加空格)
支持 " px " 、 " % " 涡贱、 " em " 咏删、 " rem " 等單位 (可混合使用)
CSS代碼:
div .left{
width: 200px; //左側(cè)寬度固定
}
div .right{
width: calc(100% - 200px);
width: -moz-calc(100% - 200px);
width: -webkit-calc(100% - 200px);
}
15. CSS實現(xiàn)圖片自動按比例等比例縮小并垂直水平居中,圖片不變形
CSS代碼:
div{
position: relative;
width: 270px;
height: 200px;
border: 1px solid #e2e2e2;
}
div img{
position: absolute;
display: block;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
max-width: 270px;
max-height: 200px;
width:expression(this.width > 270? "270px" : this.width); //當(dāng)?shù)桶姹綢E瀏覽器 不支持max-width屬性時執(zhí)行該語句
height:expression(this.height > 200? "200px" : this.height);
}
14. 多行文本溢出顯示省略號
CSS代碼:
.box{
width: 100%;
overflow: hidden;
display: -webkit-box; //將對象作為彈性伸縮盒子模型顯示 *必須結(jié)合的屬性*
-webkit-box-orient: vertical; //設(shè)置伸縮盒對象的子元素的排列方式 *必須結(jié)合的屬性*
-webkit-line-clamp: 3; //用來限制在一個塊元素中顯示的文本的行數(shù)
word-break: break-all; //讓瀏覽器實現(xiàn)在任意位置的換行 *break-all為允許在單詞內(nèi)換行*
}
13. 單行文本溢出顯示省略號
CSS代碼:
a{
overflow: hidden; //超出隱藏
display: block;
max-width: 116px;
text-overflow: ellipsis; //當(dāng)文本內(nèi)容溢出時顯示省略號
white-space: nowrap; //文本在同一行上不會換行问词,直到遇到 <br> 標(biāo)簽為止
height: 40px;
line-height: 40px;
background-color: #f8f8f8;
padding: 0 10px;
}
主要代碼:
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
text-overflow: clip | ellipsis
clip: 不顯示省略號(...)
ellipsis: 當(dāng)文本內(nèi)容溢出時顯示省略號(...)
12. 左右兩邊 div 的高度自適應(yīng)相等
需要將 margin 和 padding 的數(shù)值設(shè)置的越大越好
需要給父元素設(shè)置 overflow: hidden;
html代碼結(jié)構(gòu):
<div class="w c pagecont">
<div class="l pageleft"></div>
<div class="l pageright"></div>
</div>
CSS代碼:
.c{
overflow: hidden;
zoom: 1;
}
.pageleft,
.pageright {
padding: 30px;
margin-bottom: -10000px;
padding-bottom: 10030px;
}
11. CSS實現(xiàn)文本兩端對齊
CSS代碼:
div{
text-align: justify;
text-justify: distribute-all-lines; //ie6-8
text-align-last: justify; //一個塊或行的最后一行對齊方式
-moz-text-align-last: justify;
-webkit-text-align-last: justify;
}
10. IE6瀏覽器常見兼容問題
// 橫向雙倍margin
IE6中設(shè)置元素浮動后督函,會出現(xiàn)橫向雙倍margin值。--添加 _display: inline;
// 3px bug
浮動塊元素處于同一行時激挪,有默認的3px間距辰狡。--設(shè)置非浮動元素浮動。
// 透明度
opacity: 0.6; --filter: alpha(opacity = 60);
9. PC端調(diào)用QQ實現(xiàn)在線聊天
點擊圖標(biāo)或者文字可以直接調(diào)用QQ實現(xiàn)在線聊天
<a target="_self"><img border="0" src="http://wpa.qq.com/pa?p=2:80080088:51" title="在線客服" /></a>
//修改兩處QQ號即可
<a href="tencent://message/?uin=80080088&Site=Sambow&Menu=yes">80080088</a>
//修改一處QQ號即可
8. CSS3 Gradient 漸變
CSS3 Gradient 分為線性漸變和徑向漸變垄分。
html代碼:
<div class = "gradient" style = "width: 120px; height: 80px; "></div>
CSS代碼:
background: #e6e6e6; //當(dāng)瀏覽器不支持背景漸變時該語句將會被執(zhí)行
background: -o-linear-gradient(top, #fdfdfd, #e6e6e6);
background: -moz-linear-gradient(top, #fdfdfd, #e6e6e6);
background: -webkit-linear-gradient(top, #fdfdfd, #e6e6e6); //最新發(fā)布語法
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#fdfdfd), #e6e6e6); //老式語法
示例:
background: -webkit-linear-gradient(left top, #ccc, green, yellow, red); //線性漸變
background: -webkit-radial-gradient(center, red, blue, #f96, green); //徑向漸變
//不設(shè)置位置宛篇,將會均勻分布
background: -webkit-linear-gradient(top, #ccc 5%, #F95 30%, red); //線性漸變
background: -webkit-radial-gradient(center, red 5%, blue 10%, #f96 40%, green 100%); //徑向漸變
//設(shè)置位置
background: -webkit-repeating-linear-gradient(-45deg, #f96, #f96 3px, green 3px, green 8px); //線性重復(fù)漸變
background: -webkit-repeating-radial-gradient(red, #f96 2px, green 2px, green 8px); //徑向重復(fù)漸變
共有三個參數(shù),第一個參數(shù)表示線性漸變的方向薄湿,top是從上到下叫倍,left是從左到右,如果定義成left top 或者角度(-45deg)豺瘤,那就是從左上角到右下角吆倦。第二個參數(shù)是起點顏色,第三個參數(shù)是終點顏色坐求〔显螅可以在它們之間插入更多的參數(shù),表示多種顏色的漸變瞻赶。
IE依靠濾鏡實現(xiàn)漸變赛糟。
startColorstr表示起點的顏色,endColorstr表示終點顏色砸逊。GradientType表示漸變類型璧南,0表示垂直漸變,1表示水平漸變师逸。
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr=#f96, endColorstr=#ccc); //IE9
-ms-filter: "progid:DXImageTransform.Microsoft.gradient (GradientType=0, startColorstr=#f96, endColorstr=#ccc)"; //IE8+
實例 (實現(xiàn)鼠標(biāo)移上時出現(xiàn)黑心圓點效果)
CSS代碼:
span{
display: block;
float: left;
width: 13px;
height: 13px;
border-radius: 50%;
border: 1px solid #ccc;
font-size: 0;
background: #fff;
margin: 0 5px;
}
span:hover{
background: #676767;
background: -o-radial-gradient(center, #676767 26%, #fff 44%, #fff 100%);
background: -moz-radial-gradient(center, #676767 26%, #fff 44%, #fff 100%);
background: -webkit-radial-gradient(center, #676767 26%, #fff 44%, #fff 100%);
}
7. CSS3 columns多列布局
div{
//把div元素中的文本劃分為4列
-moz-columns: 4;
-webkit-columns: 4;
columns: 4;
//將div元素中的文本分為三列司倚,并列間30px的間隔
-moz-column-gap: 30px;
-webkit-column-gap: 30px;
column-gap: 30px;
//規(guī)定列之間的寬度、樣式和顏色
-moz-column-rule: 1px outset #fff;
-webkit-column-rule: 1px outset #fff;
column-rule: 1px outset #fff;
}
6. 設(shè)置表單file控件上傳文件時可以選擇多個文件
<input type='file' multiple='true'> //設(shè)置multiple屬性值為true
5. CSS實現(xiàn)一行水平居中篓像,多行左對齊效果
html代碼:
<p class='text'><span>一行水平居中动知,多行左對齊。</span></p>
<p class='text'><span>一行水平居中员辩,多行左對齊盒粮。一行水平居中,多行左對齊奠滑。一行水平居中丹皱,多行左對齊妒穴。</span></p>
css代碼:
.text {text-align: center;}
.text span {display: inline-block; text-align: left;}
4. 空白會被保留
css代碼:
white-space: pre;
3. 修改input輸入框中光標(biāo)的顏色不改變字體的顏色
input{
color: #fff;
caret-color: red;
}
2. 修改input 輸入框中 placeholder 默認字體樣式
//webkit內(nèi)核的瀏覽器
input::-webkit-input-placeholder {
color: #c2c6ce;
}
//Firefox版本4-18
input:-moz-placeholder {
color: #c2c6ce;
}
//Firefox版本19+
input::-moz-placeholder {
color: #c2c6ce;
}
//IE瀏覽器
input:-ms-input-placeholder {
color: #c2c6ce;
}
1. 去除瀏覽器中 input button 等標(biāo)簽獲得焦點時的帶顏色邊框
CSS代碼:
input, textarea, button{
outline:none;
}