p{ width:200rpx; overflow: hidden; text-overflow:ellipsis; white-space: nowrap; }2、多行文本溢出顯示省略號p { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; }二、 文字垂直居中1、單行文字的垂直居中height 和 line-height 同樣的高度.box{ width:200px; height:100px; line-height:100px;}2、多行文字的垂直居中解決方案:vertical-align 方法
.box{ width:500px; height:100px; vertical-align:middle; display:table-cell;}3、首行縮進<p style="text-indent:2em;">這是一段內容文字,這是一段內容文字</p>4、首字下沉p:first-letter{ font-size:40px; float: left; color:red; }5、中英文自動換行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;}6、文字陰影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;}7、設置 input 中 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;}
二、布局樣式1、div 垂直居中<div> <div></div> </div>固定高寬 div 垂直居中.box{ position: absolute; top: 50%; left: 50%; background-color: red; width: 100px; height: 100px; margin: -50px 0 0 -50px; }不固定高寬 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;}2、清除浮動方法一:父級 div 定義 height優點:簡單,代碼少,容易掌握
缺點:只適合高度固定的布局,要給出精確的高度,如果高度和父級 div 不一樣時,會產生問題
建議:不推薦使用,只建議高度固定的布局時使用。
<style type="text/css">.div1{background:#000080;border:1px solid red;/*解決代碼*/height:200px;}.div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}.left{float:left;width:20%;height:200px;background:#DDD}.rightright{float:rightright;width:30%;height:80px;background:#DDD}</style>
<div> <div>Left</div> <div>Right</div></div><div> div2</div>方法二:結尾處加空 div 標籤 clear:both原理:添加一個空 div,利用 css 提高的 clear:both 清除浮動,讓父級 div 能自動獲取到高度
優點:簡單,代碼少,瀏覽器支持好,不容易出現怪問題
缺點:不少初學者不理解原理;如果頁面浮動布局多,就要增加很多空 div,讓人感覺很不爽
建議:不推薦使用,但此方法是以前主要使用的一種清除浮動方法
<style type="text/css">.div1{background:#000080;border:1px solid red;}.div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}.left{float:left;width:20%;height:200px;background:#DDD}.rightright{float:rightright;width:30%;height:80px;background:#DDD}/*清除浮動代碼*/.clearfloat:after{display:block;clear:both;content:"";visibility:hidden;height:0}.clearfloat{zoom:1}</style>
<div> <div>Left</div> <div>Right</div></div><div> div2</div>方法三:父級 div 定義 overflow:hidden原理:必須定義 width 或 zoom:1,同時不能定義 height,使用 overflow:hidden 時,瀏覽器會自動檢查浮動區域的高度
優點:簡單,代碼少,瀏覽器支持好
缺點:不能和 position 配合使用,因為超出的尺寸的會被隱藏。
建議:只推薦沒有使用 position 或對 overflow:hidden 理解比較深的朋友使用。
<style type="text/css">.div1{background:#000080;border:1px solid red;/*解決代碼*/width:98%;overflow:hidden}.div2{background:#800080;border:1px solid red;height:100px;margin-top:10px;width:98%}.left{float:left;width:20%;height:200px;background:#DDD}.rightright{float:rightright;width:30%;height:80px;background:#DDD}</style>
<div> <div>Left</div> <div>Right</div></div><div> div2</div>1、IOS 頁面滑動卡頓body,html{ -webkit-overflow-scrolling: touch;}2、CSS滾動條仿 ios::-webkit-scrollbar{ width: 5px; height: 5px; }::-webkit-scrollbar-thumb{ border-radius: 1em; background-color: rgba(50,50,50,.3); }::-webkit-scrollbar-track{ border-radius: 1em; background-color: rgba(50,50,50,.1); }3、實現隱藏滾動條同時又可以滾動.demo::-webkit-scrollbar { display: none; /* Chrome Safari */}
.demo { scrollbar-width: none; /* firefox */ -ms-overflow-style: none; /* IE 10+ */ overflow-x: hidden; overflow-y: auto;}4、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;}效果如下:
注: 如果想繪製右直角三角,則將左 border 設置為 0;如果想繪製左直角三角,將右 border 設置為 0 即可(其它情況同理)。
5、表格邊框合併table,tr,td{ border: 1px solid #666;}table{ border-collapse: collapse;}6、 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 個標籤。
使用方法:
7、 onerror 處理圖片異常使用 onerror 異常處理時,若 onerror 的圖片也出現問題,則圖片顯示會陷入死循環,所以要在賦值異常圖片之後,將地址置空。<img onerror="this.src='url;this.onerror=null'" />8、移動端軟鍵盤變為搜索方式默認情況下軟鍵盤上該鍵位為前往或者確認等文字,要使其變為搜索文字,需要在 input 上加上 type 聲明:<form action="#"> <input type="search" placeholder="請輸入..." name="search" /></form>需要一個 form 標籤套起來,並且設置 action 屬性,這樣寫完之後輸入法的右下角就會自動變成搜索。同時,使用了 search 類型後,搜索框上會默認自帶刪除按鈕如需屏蔽,可以使用如下方式:
input[type="search"]::-webkit-search-cancel-button{ -webkit-appearance: none; }一、字體屬性:(font)
1. 大小 {font-size: x-large;}(特大) xx-small;(極小) 一般中文用不到,只要用數值就可以,單位:PX、PD2. 樣式 {font-style: oblique;}(偏斜體) italic;(斜體) normal;(正常)3. 行高 {line-height: normal;}(正常) 單位:PX、PD、EM4. 粗細 {font-weight: bold;}(粗體) lighter;(細體) normal;(正常)5. 變體 {font-variant: small-caps;}(小型大寫字母) normal;(正常)6. 大小寫 {text-transform: capitalize;}(首字母大寫) uppercase;(大寫) lowercase;(小寫) none;(無)7. 修飾 {text-decoration: underline;}(下劃線) overline;(上劃線) line-through;(刪除線) blink;(閃爍)二、常用字體:(font-family)"Courier New", Courier, monospace, "Times New Roman", Times, serif, Arial, Helvetica, sans-serif, Verdana三、背景屬性:(background)色彩 {background-color: #FFFFFF;}
圖片 {background-image: none;}
重複 {background-repeat: no-repeat;}
滾動 {background-attachment: fixed;}(固定) scroll;(滾動)
位置 {background-position: left;}(水平) top(垂直);
簡寫方法 {background:#000 url(..) repeat fixed left top;} /*簡寫·這個在閱讀代碼中經常出現。四、區塊屬性:(Block)字間距 {letter-spacing: normal;} 數值
對齊 {text-align: justify;}(兩端對齊) left;(左對齊) right;(右對齊) center;(居中)
縮進 {text-indent: 數值px;}
垂直對齊 {vertical-align: baseline;}(基線) sub;(下標) sup;(上標) top; text-top; middle; bottom; text-bottom;
詞間距word-spacing: normal; 數值
空格white-space: pre;(保留) nowrap;(不換行)
顯示 {display:block;}(塊) inline;(內嵌) list-item;(列表項) run-in;(追加部分) compact;(緊湊) marker;(標記) table; inline-table; table-raw-group; table-header-group; table-footer-group; table-raw; table-column-group; table-column; table-cell; table-caption;(表格標題) /*display 屬性的了解很模糊*/
五、方框屬性:(Box)六、邊框屬性:(Border)七、列表屬性:(List-style)類型list-style-type: disc;(圓點) circle;(圓圈) square;(方塊) decimal;(數字) lower-roman;(小羅碼數字) upper-roman; lower-alpha; upper-alpha;
位置list-style-position: outside;(外) inside;
圖像list-style-image: url(..);
八、定位屬性:(Position)Position: absolute; relative; static;
visibility: inherit; visible; hidden;
overflow: visible; hidden; scroll; auto;
clip: rect(12px,auto,12px,auto) (裁切)
九、CSS文字屬性:color : #999999; /*文字顏色*/
font-family : 宋體,sans-serif; /*文字字體*/
font-size : 9pt; /*文字大小*/
font-style:itelic; /*文字斜體*/
font-variant:small-caps; /*小字體*/
letter-spacing : 1pt; /*字間距離*/
line-height : 200%; /*設置行高*/
font-weight:bold; /*文字粗體*/
vertical-align:sub; /*下標字*/
vertical-align:super; /*上標字*/
text-decoration:line-through; /*加刪除線*/
text-decoration: overline; /*加頂線*/
text-decoration:underline; /*加下劃線*/
text-decoration:none; /*刪除連結下劃線*/
text-transform : capitalize; /*首字大寫*/
text-transform : uppercase; /*英文大寫*/
text-transform : lowercase; /*英文小寫*/
text-align:right; /*文字右對齊*/
text-align:left; /*文字左對齊*/
text-align:center; /*文字居中對齊*/
text-align:justify; /*文字分散對齊*/
vertical-align屬性
vertical-align:top; /*垂直向上對齊*/
vertical-align:bottom; /*垂直向下對齊*/
vertical-align:middle; /*垂直居中對齊*/
vertical-align:text-top; /*文字垂直向上對齊*/
vertical-align:text-bottom; /*文字垂直向下對齊*/
十、CSS邊框空白padding-top:10px; /*上邊框留空白*/
padding-right:10px; /*右邊框留空白*/
padding-bottom:10px; /*下邊框留空白*/
padding-left:10px; /*左邊框留空白*/
header 頭部/頁眉;
index 首頁/索引;
logo 標誌;
nav/sub_nav 導航/子導航;
banner 橫幅廣告;
main/content 主體/內容;
container/con 容器;
wrapper/wrap 包裹(類似於container);
menu 菜單;
sub_menu/second_menu 子菜單/二級菜單;
list 列表;
section 分區/分塊(類似於div);
article 文章;
aside 側邊欄/廣告;
footer 頁腳/底部;
title/sub_title 標題/副標題;
news 新聞;
hot 熱點;
pro 產品(product);
company 公司;
msg/info 信息(message)/消息;
ads 廣告(advertisements);
icon 小圖標;
img 圖片(image);
copyright 版權;
contact_us 聯繫我們;
friend_link 友情連結;
tel 聯繫電話(telephone);
address 地址;
& nbsp; 空格(&和n之間的空格去掉,不要忘記分號);
<br/> (文字末尾添加)換行;
CSS樣式(style) CSS 層疊樣式表 (Cascading Style Sheets) ;background 背景;
background: -webkit-gradient(top red orange yellow green lightblue blue purple) 顏色漸變;
position 位置/定位;
relative/absolute/fixed 相對定位/絕對定位/固定定位;
float 浮動;
clear 清除;
vertical-align: middle/top/bottom; 垂直居中/上/下;
line-height 行高;
margin 外邊距;
padding 內邊距;
border 邊框;
solid/dashed/dotted 實線/線虛線/點虛線;
border-radius 圓角;
shadow 陰影;
display 展示;
hidden 隱藏;
block/inline-block 塊元素/行內塊;
overflow 溢出;
cursor 光標;
cursor:pointer; 滑鼠移上變為小手;
animation 動畫;
css sprites 雪碧圖/圖片精靈;
column 分列;
flex 彈性(布局);
表單(form)與表格(table)form 表單;
action 行為;
method 方式/方法;
input 輸入框;
label 標籤;
password 密碼;
radio 單選框;
checkbox 複選框;
btn 按鈕(button);
submit/reset 提交/重置;
textarea 文本域;
select/option 選擇框/選擇項;
placeholder 佔位符(起提示作用);
search 搜索;
icon 小圖標;
autofocus 自動聚焦;
disabled 禁用;
checked 選中(單選框/複選框);
selected 默認選擇項(下拉選擇框);
required 必填項;
readonly 只讀;
table 表格;
thead/tbody/tfoot 表格標題/主體/底部;
colspan 跨列;
rowspan 跨行;
cellspacing 單元格間距(類似於margin);
cellpadding 單元格邊距(類似於padding);
border-collapse: collapse; 邊框合併(用於table上)。