來源 | http://www.fly63.com/article/detial/10314
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:all-all;只對英文操作,以文字作為換行信息
自動換行:斷字;只對英文使用,以作為換行說明
空白:預包裝;只對中文,強制換行
空白: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軸偏移量:指陰影居於字體水平偏移的位置。
Y軸偏移量:指陰影居於字體垂直偏移的位置。
模糊:指陰影的模糊值。
顏色:指陰影的顏色;h1{text-shadow: 5px 5px 5px #FF0000;}4、設置placeholder的字體樣式input::-webkit-input-placeholder { color: red;}input::-moz-placeholder { color: red;}input:-ms-input-placeholder { color: red;}input:-moz-placeholder { 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%);}方法四:設置: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吸氣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;}效果如下:
註:如果想畫左直角,則將邊界設置為0;如果想畫左直角設置,將右邊界設置為0(其他情況同理)。
10、Table表格合併合併table,tr,td{ border: 1px solid #666;}table{ border-collapse: collapse;}11、CSS選取第Ñ個標籤元素first-child first-child 表示選擇列表中的第一個標籤。
last-child last-child 表示選擇列表中的最後一個標籤
nth-child(3) 表示選擇列表中的第 3 個標籤
nth-child(2n) )這個表示選擇列表中的偶數標籤
第n個孩子(2N-1)這個表示選擇列表中的奇數標籤
第n個孩子(N + 3)這個表示選擇列表中的標籤從第3個開始到最後。
nth- child(-n+3) 這個表示
選擇列表中的倒數第 3 個標籤,即小於 3 個的標籤。nth-last-child(3) 表示這個選擇列表中的倒數第 3 個標籤。使用方法:
12、移動端軟鍵盤變搜索方式默認情況下軟鍵盤上該鍵位為或確認等文字,要改為搜索文字,需要在輸入上加上類型聲明:
<form action="#"> <input type="search" placeholder="請輸入..." name="search" /></form>需要一個表格標籤套起來,並且設置動作屬性,這樣寫完之後輸入法的右下角會自動變成搜索,同時,使用了搜索類型後,搜索框上會默認自帶刪除按鈕。
13、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; }15、文字之間的關鍵text-indent 抬頭距離,letter-spacing字與字鍵。
p{ text-indent:10px; letter-spacing:10px;}16、元素佔滿整個屏幕高度如果使用100%,會根據父級的高度來決定,所以使用100vh單位。.dom{ width:100%; height:100vh;}17、CSS實現文本節點形象.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、實現文字豎向排版// 單列展示時.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 { 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、使用硬體加速在瀏覽器中開啟硬體加速,使 GPU 圖形處理單元發揮功能,從而提升性能。硬體加速在移動端特別有用,因為它可以有效的減少資源的利用。
當前主流瀏覽器會檢測到頁面特定DOM元素應用了某些CSS規則時會開啟,最顯著特徵的元素的3D轉換。如果不使用3D變形,我們通過下面的方式可以來開啟:
.wrap { transform: translateZ(0);}22、頁面動畫出現問題在 Chrome 和 Safari 中,當我們使用 CSS 轉換或者動畫時可能會有頁面閃爍的效果,下面的代碼可以修復此情況:
.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、刪除轉換閃屏.wrap { -webkit-transform-style: preserve-3d; -webkit-backface-visibility: hidden; -webkit-perspective: 1000;}26、識別字符串裡的'\n'並換行一般在富文本中返回換行符不是<br>的標籤,而且\n。不使用正則轉換的情況下,可通過下面樣式實現換行。body { white-space: pre-line;}27、移除一個標籤被點連結的邊框a { outline: none; text-decoration:none; }28、CSS顯示連結之後的URL<a href="//www.webqdkf.com"> 有課前端網</a><style>a:after {content: " (" attr(href) ")";}</style>29、選擇內容中顯示、下拉內容右圖select{ text-align: center; text-align-last: center;}select option { direction: rtl;}30、修改輸入輸入細節的顏色不改變的顏色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、實現寬高等典型實例.scale { width: 100%; padding-bottom: 56.25%; height: 0; position: relative; }.item { position: absolute; width: 100%; height: 100%; background-color: 499e56;} <div> <div> 這裡是所有子元素的容器 </div> </div>34、transfrom的rotate屬性在span標籤下失效span { display: inline-block}35、css加載動畫.dom{ -webkit-animation:circle 1s infinite linear;}@keyframes circle{ 0%{ transform: rotate(0deg); } 100%{ transform: rotate(360deg); }}實現如下效果:
<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、文字漸變效果實現<div class="text_signature " > 有課前端網,一個專門學習前端知識的網站</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></div><style> .text_shadow{ width:500px; height:100px; box-shadow: 0px 0px 13px 1px rgba(51, 51, 51, 0.1);}</style>38、外觀的漸變<div></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、實現立體字的效果<div> 有課前端網,一個專門學習前端知識的網站</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、全屏背景圖片的實現.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、實現文字描邊的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、水流的實現.dom{ opacity:0.4; filter:alpha(opacity=40); /* IE8 及其更早版本 */}使用rgba()設置顏色顏色
.demo{ background:rgba(255,0,0,1);}說明:RGBA是代表紅色(紅色)綠色(綠色)藍色(藍色)和Alpha(不知道)三個字母的縮寫。
43、解決1px邊框變粗的問題.dom{ height: 1px; background: #dbdbdb; transform:scaleY(0.5);}Ps:出現1px變粗的原因,比如在2倍屏時1px的像素對應2個物理像素。
44、CSS不同的單位的大腦css自己也能夠進行簡單的運算,主要是實現了計算這個函數。
.div{ width: calc(100% - 50px); }45、CSS實現文字模糊.vague_text{ color: transparent; text-shadow: #111 0 0 5px;}46、通過彩色圖標變灰一張彩色的圖片實現了,移出變灰的效果。
<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,但這個只適用於背景圖。縮放的問題。使用的提前條件:圖片的父級容器寬高。
img{ width: 100%; height: 100%; object-fit: cover;}填充:默認值。內容拉伸填滿整個內容框,不保證保持原有的比例。
包含:邊保持邊長和長度中的那條長度,等比縮放,可能會留白。
封面:保持原有的空間長度。寬度和高度中長度一致,長的部分等比縮放。可能部分區域不可見。(經常)
無:保持原有的保留比例。保持同時替換內容的原始尺寸大小。
按比例縮小:保持原有尺寸大小,如果容器尺寸較大,圖片內容尺寸不變,保持圖片的尺寸不變,不會放大縮小;容器尺寸較小的圖片內容尺寸不變,最能容納。
48、行內標籤元素出現空白問題方式一:父級font-size設置為0
方式二:父元素上設置word-spacing的有合適的值
.father{ word-spacing:-2px}其他方案:1將行內元素寫為1行(會影響布局);2行(會影響布局)。
49、解決vertical-align屬性不生效在使用vertical-align:middle實現垂直居中的時候,經常會發現不生效的情況。作用環境:父元素設置line-height。需要和高度一致。將顯示屬性設置為表格單元,將塊元素轉化為單元格。
作用對象:子元素中的inline-block和inline元素。<div> <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:垂直對齊不可繼承,必須對子元素單獨設置。同時需要注意的是線高的高度基於字體大小(即字體的高度),如果文字要轉行會出現異常哦。
本文完~
學習更多技能
請點擊下方公眾號