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;只對英文起作用屋休,以字母作為換行依據(jù)
word-wrap:break-word; 只對英文起作用坞古,以單詞作為換行依據(jù)
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 為網(wǎng)頁字體添加陰影,通過對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、實現(xiàn)隱藏滾動條同時又可以滾動
.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 繪制三角形
div {
width: 0;
height: 0;
border-width: 0 40px 40px;
border-style: solid;
border-color: transparent transparent red;
}
效果如下:
實現(xiàn)帶邊框的三角形:
<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) 這個表示選擇列表中的偶數(shù)標簽
nth-child(2n-1) 這個表示選擇列表中的奇數(shù)標簽
nth-child(n+3) 這個表示選擇列表中的標簽從第 3 個開始到最后。
nth-child(-n+3) 這個表示選擇列表中的標簽從 0 到 3荐糜,即小于 3 的標簽巷怜。
nth-last-child(3) 這個表示選擇列表中的倒數(shù)第 3 個標簽葛超。
使用方法:
li:first-child{}
12、移動端軟鍵盤變?yōu)樗阉鞣绞?/strong>
默認情況下軟鍵盤上該鍵位為前往或者確認等文字延塑,要使其變?yōu)樗阉魑淖中逭牛枰?input 上加上 type 聲明:
<form action="#">
<input type="search" placeholder="請輸入..." name="search" />
</form>
需要一個 form 標簽套起來,并且設置 action 屬性,這樣寫完之后輸入法的右下角就會自動變成搜索,同時,使用了 search 類型后关带,搜索框上會默認自帶刪除按鈕侥涵。
13、onerror 處理圖片異常
使用 onerror 異常處理時宋雏,若 onerror 的圖片也出現(xiàn)問題芜飘,則圖片顯示會陷入死循環(huán),所以要在賦值異常圖片之后磨总,將地址置空
<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;//間距
}
16馆纳、元素占滿整個屏幕
heigth如果使用100%裙戏,會根據(jù)父級的高度來決定,所以使用100vh單位厕诡。
.dom{
width:100%;
height:100vh;
}
17、CSS實現(xiàn)文本兩端對齊
.wrap {
text-align: justify;
text-justify: distribute-all-lines; //ie6-8
text-align-last: justify; //一個塊或行的最后一行對齊方式
-moz-text-align-last: justify;
-webkit-text-align-last: justify;
}
18营勤、實現(xiàn)文字豎向排版
// 單列展示時
.wrap {
width: 25px;
line-height: 18px;
height: auto;
font-size: 12px;
padding: 8px 5px;
word-wrap: break-word;/*英文的時候需要加上這句灵嫌,自動換行*/
}
// 多列展示時
.wrap {
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; -- 從右向左
}
19、使元素鼠標事件失效
.wrap {
// 如果按tab能選中該元素葛作,如button寿羞,然后按回車還是能執(zhí)行對應的事件,如click赂蠢。
pointer-events: none;
cursor: default;
}
20绪穆、禁止用戶選擇
.wrap {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
21、使用硬件加速
在瀏覽器中用css開啟硬件加速虱岂,使GPU (Graphics Processing Unit) 發(fā)揮功能玖院,從而提升性能。硬件加速在移動端尤其有用第岖,因為它可以有效的減少資源的利用难菌。
目前主流瀏覽器會檢測到頁面中某個DOM元素應用了某些CSS規(guī)則時就會開啟,最顯著的特征的元素的3D變換蔑滓。如果不使用3D變形郊酒,我們可以通過下面方式來開啟:
.wrap {
transform: translateZ(0);
}
22遇绞、頁面動畫出現(xiàn)閃爍問題
在 Chrome and Safari中,當我們使用CSS transforms 或者 animations時可能會有頁面閃爍的效果燎窘,下面的代碼可以修復此情況:
.cube {
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-perspective: 1000;
perspective: 1000;
/* Other transform properties here */
}
在webkit內核的瀏覽器中摹闽,另一個行之有效的方法是
.cube {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
/* Other transform properties here */
}
23、字母大小寫轉換
p {text-transform: uppercase} // 將所有字母變成大寫字母
p {text-transform: lowercase} // 將所有字母變成小寫字母
p {text-transform: capitalize} // 首字母大寫
p {font-variant: small-caps} // 將字體變成小型的大寫字母
24褐健、將一個容器設為透明
.wrap {
filter:alpha(opacity=50);
-moz-opacity:0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
}
25付鹿、消除transition閃屏
.wrap {
-webkit-transform-style: preserve-3d;
-webkit-backface-visibility: hidden;
-webkit-perspective: 1000;
}
26、識別字符串里的 '\n' 并換行
一般在富文本中返回換行符不是
的標簽铝量,而且\n倘屹。不使用正則轉換的情況下,可通過下面樣式實現(xiàn)換行慢叨。
body {
white-space: pre-line;
}
27纽匙、移除a標簽被點鏈接的邊框
a {
outline: none;//或者outline: 0
text-decoration:none; //取消默認下劃線
}
28拍谐、CSS顯示鏈接之后的URL
<a >有課前端網(wǎng)</a>
<style>
a:after {content: " (" attr(href) ")";}
</style>
29烛缔、select內容居中顯示、下拉內容右對齊
select{
text-align: center;
text-align-last: center;
}
select option {
direction: rtl;
}
30轩拨、修改input輸入框中光標的顏色不改變字體的顏色
input{
color: #fff;
caret-color: red;
}
31践瓷、子元素固定寬度 父元素寬度被撐開
// 父元素下的子元素是行內元素
.wrap {
white-space: nowrap;
}
// 若父元素下的子元素是塊級元素
.wrap {
white-space: nowrap; // 子元素不被換行
display: inline-block;
}
32、讓div里的圖片和文字同時上下居中
這里不使用flex布局的情況亡蓉。通過vertival-align
.wrap {
height: 100,
line-height: 100
}
img {
vertival-align:middle
}
// vertical-align css的屬性vertical-align用來指定行內元素(inline)或表格單元格(table-cell)元素的垂直對齊方式晕翠。只對行內元素、表格單元格元素生效砍濒,不能用它垂直對齊塊級元素
// vertical-align:baseline/top/middle/bottom/sub/text-top;
33淋肾、實現(xiàn)寬高等比例自適應矩形
.scale {
width: 100%;
padding-bottom: 56.25%;
height: 0;
position: relative;
}
.item {
position: absolute;
width: 100%;
height: 100%;
background-color: 499e56;
}
<div class="scale">
<div class="item">
這里是所有子元素的容器
</div>
</div>
34、transfrom的rotate屬性在span標簽下失效
span {
display: inline-block
}
35爸邢、CSS加載動畫
主要是通過css旋轉動畫的實現(xiàn):
.dom{
-webkit-animation:circle 1s infinite linear;
}
@keyframes circle{
0%{ transform: rotate(0deg); }
100%{ transform: rotate(360deg); }
}
實現(xiàn)如下效果:
<div class="loader"></div>
<style>
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 80px;
height: 80px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
}
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
36樊卓、文字漸變效果實現(xiàn)
<div class="text_signature " >fly63前端網(wǎng),一個免費學習前端知識的網(wǎng)站</div>
<style>
.text_signature {
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-image: linear-gradient(to right, #ec2239, #40a4e2,#ea96f5);
width: 320px;
}
</style>
37杠河、好看的邊框陰影
<div class="text_shadow"></div>
<style>
.text_shadow{
width:500px;
height:100px;
box-shadow: 0px 0px 13px 1px rgba(51, 51, 51, 0.1);
}
</style>
38碌尔、好看的背景漸變
<div class="text_gradient"></div>
<style>
.text_gradient{
width:500px;
height:100px;
background: linear-gradient(25deg, rgb(79, 107, 208), rgb(98, 141, 185), rgb(102, 175, 161), rgb(92, 210, 133)) rgb(182, 228, 253);
}
</style>
39、實現(xiàn)立體字的效果
<div class="text_solid">有課前端網(wǎng)-web前端技術學習平臺</div>
<style>
.text_solid{
font-size: 32px;
text-align: center;
font-weight: bold;
line-height:100px;
text-transform:uppercase;
position: relative;
background-color: #333;
color:#fff;
text-shadow:
0px 1px 0px #c0c0c0,
0px 2px 0px #b0b0b0,
0px 3px 0px #a0a0a0,
0px 4px 0px #909090,
0px 5px 10px rgba(0, 0, 0, 0.6);
}
</style>
40券敌、全屏背景圖片的實現(xiàn)
.swper{
background-image: url(./img/bg.jpg);
width:100%;
height:100%;//父級高不為100%請使用100vh
zoom: 1;
background-color: #fff;
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-o-background-size: cover;
background-position: center 0;
}
41唾戚、實現(xiàn)文字描邊的2種方法
方式一:
.stroke {
-webkit-text-stroke: 1px greenyellow;
text-stroke: 1px greenyellow;
}
方式二:
.stroke {
text-shadow:#000 1px 0 0,#000 0 1px 0,#000 -1px 0 0,#000 0 -1px 0;
-webkit-text-shadow:#000 1px 0 0,#000 0 1px 0,#000 -1px 0 0,#000 0 -1px 0;
-moz-text-shadow:#000 1px 0 0,#000 0 1px 0,#000 -1px 0 0,#000 0 -1px 0;
*filter: Glow(color=#000, strength=1);
}
42、元素透明度的實現(xiàn)
.dom{
opacity:0.4;
filter:alpha(opacity=40); /* IE8 及其更早版本 */
}
使用rgba()設置顏色透明度陪白。
.demo{
background:rgba(255,0,0,1);
}
說明:RGBA 是代表Red(紅色) Green(綠色) Blue(藍色)和 Alpha(不透明度)三個單詞的縮寫颈走。
43、解決1px邊框變粗問題
.dom{
height: 1px;
background: #dbdbdb;
transform:scaleY(0.5);
}
Ps:出現(xiàn)1px變粗的原因咱士,比如在2倍屏時1px的像素實際對應2個物理像素立由。
44照激、CSS不同單位的運算
css自己也能夠進行簡單的運算砾跃,主要是用到了calc這個函數(shù)轿衔。實現(xiàn)不同單位的加減運算:
.div{
width: calc(100% - 50px);
}
45腮鞍、CSS實現(xiàn)文字模糊效果
.vague_text{
color: transparent;
text-shadow: #111 0 0 5px;
}
46、通過濾鏡讓圖標變灰
一張彩色的圖片就能實現(xiàn)鼠標移入變彩色道盏,移出變灰的效果而柑。
<a href='' class='icon'><img src='01.jpg' /></a>
<style>
.icon{
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-ms-filter: grayscale(100%);
-o-filter: grayscale(100%);
filter: grayscale(100%);
filter: gray;
}
.icon:hover{
filter: none;
-webkit-filter: grayscale(0%);
}
</style>
47、圖片自適應object-fit
當圖片比例不固定時荷逞,想要讓圖片自適應媒咳,一般都會用background-size:cover/contain,但是這個只適用于背景圖种远。css3中可使用object-fit屬性來解決圖片被拉伸或是被縮放的問題涩澡。使用的提前條件:圖片的父級容器要有寬高。
img{
width: 100%;
height: 100%;
object-fit: cover;
}
fill: 默認值坠敷。內容拉伸填滿整個content box, 不保證保持原有的比例妙同。
contain: 保持原有尺寸比例。長度和高度中長的那條邊跟容器大小一致膝迎,短的那條等比縮放粥帚,可能會有留白。
cover: 保持原有尺寸比例限次。寬度和高度中短的那條邊跟容器大小一致芒涡,長的那條等比縮放÷袈可能會有部分區(qū)域不可見拖陆。(常用)
none: 保持原有尺寸比例。同時保持替換內容原始尺寸大小懊亡。
scale-down:保持原有尺寸比例,如果容器尺寸大于圖片內容尺寸,保持圖片的原有尺寸乎串,不會放大失真店枣;容器尺寸小于圖片內容尺寸,用法跟contain一樣叹誉。
48鸯两、行內標簽元素出現(xiàn)間隙問題
方式一:父級font-size設置為0
.father{
font-size:0;
}
方式二:父元素上設置word-spacing的值為合適的負值
.father{
word-spacing:-2px
}
其它方案:1將行內元素寫為1行(影響閱讀);2使用浮動樣式(會影響布局)长豁。
49钧唐、解決vertical-align屬性不生效
在使用vertical-align:middle實現(xiàn)垂直居中的時候,經常會發(fā)現(xiàn)不生效的情況匠襟。這里需要注意它生效需要滿足的條件:
作用環(huán)境:父元素設置line-height钝侠。需要和height一致该园。或者將display屬性設置為table-cell帅韧,將塊元素轉化為單元格里初。
作用對象:子元素中的inline-block和inline元素。
<div class="box">
<img src=".\test.jpg"/>
<span>內部文字</span>
</div>
<style>
.box{
width:300px;
line-height: 300px;
font-size: 16px;
}
.box img{
width: 30px;
height:30px;
vertical-align:middle
}
.box span{
vertical-align:middle
}
</style>
PS:vertical-align不可繼承忽舟,必須對子元素單獨設置双妨。同時需要注意的是line-height的高度基于font-size(即字體的高度),如果文字要轉行會出現(xiàn)異常哦刁品。