1、文字超出部分顯示省略號

單行文字的溢位顯示省略號(一定要有寬度)

 p{
width:200rpx;
overflow: hidden;
text-overflow:ellipsis;
white-space: nowrap;
}

多行文字溢位顯示省略號

p {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}

2、中英文自動換行

word-break:break-all;只對英文起作用,以字母作為換行依據word-wrap:break-word; 只對英文起作用,以單詞作為換行依據white-space:pre-wrap; 只對中文起作用,強制換行white-space:nowrap; 強制不換行,都起作用

p{
word-wrap: break-word;
white-space: normal;
word-break: break-all;
}
//不換行
.wrap {
white-space:nowrap;
}
//自動換行
.wrap {
word-wrap: break-word;
word-break: normal;
}
//強制換行
.wrap {
word-break:break-all;
}

3、文字陰影

text-shadow 為網頁字型新增陰影,通過對text-shadow屬性設定相關的屬性值。屬性與值的說明如下:text-shadow: [X-offset,Y-offset,Blur,Color];

X-offset:指陰影居於字型水平偏移的位置。
Y-offset:指陰影居於字型垂直偏移的位置。
Blur:指陰影的模糊值。
color:指陰影的顏色;

h1{
text-shadow: 5px 5px 5px #FF0000;
}

4、設定placeholder的字型樣式

input::-webkit-input-placeholder { /* Chrome/Opera/Safari */
color: red;
}
input::-moz-placeholder { /* Firefox 19+ */
color: red;
}
input:-ms-input-placeholder { /* IE 10+ */
color: red;
}
input:-moz-placeholder { /* Firefox 18- */
color: red;
}

5、不固定高寬 div 垂直居中的方法

方法一:偽元素和 inline-block / vertical-align(相容 IE8)

.box-wrap:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; //微調整空格
}
.box {
display: inline-block;
vertical-align: middle;
}

方法二:flex(不相容 ie8 以下)

.box-wrap {
height: 300px;
justify-content:center;
align-items:center;
display:flex;
background-color:#666;
}

方法三:transform(不相容 ie8 以下)

 .box-wrap {
width:100%;
height:300px;
background:rgba(0,0,0,0.7);
position:relative;
}
.box{
position:absolute;
left:50%;
top:50%;
transform:translateX(-50%) translateY(-50%);
-webkit-transform:translateX(-50%) translateY(-50%);
}

方法四:設定 margin:auto(該方法得嚴格意義上的非固定寬高,而是 50%的父級的寬高。)

.box-wrap {
position: relative;
width:100%;
height:300px;
background-color:#f00;
}
.box-content{
position: absolute;
top:0;
left:0;
bottom:0;
right:0;
width:50%;
height:50%;
margin:auto;
background-color:#ff0;
}

6、解決IOS頁面滑動卡頓

body,html{
-webkit-overflow-scrolling: touch;
}

7、設定滾動條樣式

.test::-webkit-scrollbar{
/*滾動條整體樣式*/
width : 10px; /*高寬分別對應橫豎滾動條的尺寸*/
height: 1px;
}
.test::-webkit-scrollbar-thumb {
/*滾動條裡面小方塊*/
border-radius : 10px;
background-color: skyblue;
background-image: -webkit-linear-gradient(
45deg,
rgba(255, 255, 255, 0.2) 25%,
transparent 25%,
transparent 50%,
rgba(255, 255, 255, 0.2) 50%,
rgba(255, 255, 255, 0.2) 75%,
transparent 75%,
transparent
);
}
.test::-webkit-scrollbar-track {
/*滾動條裡面軌道*/
box-shadow : inset 0 0 5px rgba(0, 0, 0, 0.2);
background : #ededed;
border-radius: 10px;
}

8、實現隱藏滾動條同時又可以滾動

.demo::-webkit-scrollbar {
display: none; /* Chrome Safari */
} .demo {
scrollbar-width: none; /* firefox */
-ms-overflow-style: none; /* IE 10+ */
overflow-x: hidden;
overflow-y: auto;
}

9、css 繪製三角形(通過透明(transparent)來控制三角方向)

div {
width: 0;
height: 0;
border-width: 0 40px 40px;
border-style: solid;
border-color: transparent transparent red;
}

實現帶邊框的三角形:

<div id="blue"><div>

#blue {
position:relative;
width: 0;
height: 0;
border-width: 0 40px 40px;
border-style: solid;
border-color: transparent transparent blue;
}
#blue:after {
content: "";
position: absolute;
top: 1px;
left: -38px;
border-width: 0 38px 38px;
border-style: solid;
border-color: transparent transparent yellow;
}

注: 如果想繪製右直角三角,則將左 border 設定為 0;如果想繪製左直角三角,將右 border 設定為 0 即可(其它情況同理)。

10、Table表格邊框合併

table,tr,td{
border: 1px solid #666;
}
table{
border-collapse: collapse;
}

11、css 選取第 n 個標籤元素

first-child first-child 表示選擇列表中的第一個標籤。
last-child last-child 表示選擇列表中的最後一個標籤
nth-child(3) 表示選擇列表中的第 3 個標籤
nth-child(2n) 這個表示選擇列表中的偶數標籤
nth-child(2n-1) 這個表示選擇列表中的奇數標籤
nth-child(n+3) 這個表示選擇列表中的標籤從第 3 個開始到最後。
nth-child(-n+3) 這個表示選擇列表中的標籤從 0 到 3,即小於 3 的標籤。
nth-last-child(3) 這個表示選擇列表中的倒數第 3 個標籤。

12、移動端軟鍵盤變為搜尋方式

預設情況下軟鍵盤上該鍵位為前往或者確認等文字,要使其變為搜尋文字,需要在 input 上加上 type 宣告:

<form action="#">
<input type="search" placeholder="請輸入..." name="search" />
</form>

需要一個 form 標籤套起來,並且設定 action 屬性,這樣寫完之後輸入法的右下角就會自動變成搜尋,同時,使用了 search 型別後,搜尋框上會預設自帶刪除按鈕。

13、onerror 處理圖片異常

使用 onerror 異常處理時,若 onerror 的圖片也出現問題,則圖片顯示會陷入死迴圈,所以要在賦值異常圖片之後,將地址置空

<img onerror="this.src='url;this.onerror=null'" />

14、背景圖片的大小

.bg-img{
background:url(../img/find_pw_on_2.png) no-repeat center center !important;
background-size: 27px auto !important;
/*background-size: 100% 100%;*/
/*background-size: 50px 100px*/
}

15、文字之間的間距

單詞text-indent抬頭距離,letter-spacing字與字間距

p{
text-indent:10px;//單詞抬頭距離
letter-spacing:10px;//間距
}