天天看點

工作中常用CSS基礎知識整理彙總

工作中常用CSS基礎知識整理彙總

CSS常用樣式

一、文本樣式

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

單行文本的溢出顯示省略号(一定要有寬度)

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、單行文字的垂直居中

解決方案:line-height 方法

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 class="box-wrap">
     <div class="box"></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 手動定義 height,就解決了父級 div 無法自動擷取到高度的問題。

優點:簡單,代碼少,容易掌握

缺點:隻适合高度固定的布局,要給出精确的高度,如果高度和父級 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 class="div1">
    <div class="left">Left</div>
    <div class="right">Right</div>
</div>
<div class="div2">
    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 class="div1 clearfloat">
    <div class="left">Left</div>
    <div class="right">Right</div>
</div>
<div class="div2">
    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 class="div1">
    <div class="left">Left</div>
    <div class="right">Right</div>
</div>
<div class="div2">
    div2
</div>      

CSS常見問題

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;
}      

效果如下:

工作中常用CSS基礎知識整理彙總

實作帶邊框的三角形

<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;
}      

效果如下:

工作中常用CSS基礎知識整理彙總

注: 如果想繪制右直角三角,則将左 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 個标簽。

使用方法:

li:first-child{}      

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 屬性,這樣寫完之後輸入法的右下角就會自動變成搜尋。

工作中常用CSS基礎知識整理彙總

同時,使用了 search 類型後,搜尋框上會預設自帶删除按鈕

工作中常用CSS基礎知識整理彙總

如需屏蔽,可以使用如下方式:

input[type="search"]::-webkit-search-cancel-button{
     -webkit-appearance: none;
  }      

CSS常用屬性

一、字型屬性:(font)

1. 大小 {font-size: x-large;}(特大) xx-small;(極小) 一般中文用不到,隻要用數值就可以,機關:PX、PD

2. 樣式 {font-style: oblique;}(偏斜體) italic;(斜體) normal;(正常)

3. 行高 {line-height: normal;}(正常) 機關:PX、PD、EM

4. 粗細 {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)

  1. 色彩 {background-color: #FFFFFF;}
  2. 圖檔 {background-image: none;}
  3. 重複 {background-repeat: no-repeat;}
  4.  滾動 {background-attachment: fixed;}(固定) scroll;(滾動)
  5. 位置 {background-position: left;}(水準) top(垂直);

簡寫方法 {background:#000 url(..) repeat fixed left top;} /*簡寫·這個在閱讀代碼中經常出現。

四、區塊屬性:(Block)

  1. 字間距 {letter-spacing: normal;} 數值 
  2. 對齊 {text-align: justify;}(兩端對齊) left;(左對齊) right;(右對齊) center;(居中)
  3.  縮進 {text-indent: 數值px;}
  4. 垂直對齊 {vertical-align: baseline;}(基線) sub;(下标) sup;(上标) top; text-top; middle; bottom; text-bottom;
  5.  詞間距word-spacing: normal; 數值
  6. 空格white-space: pre;(保留) nowrap;(不換行)
  7. 顯示 {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)

  • 1width:; height:; float:; clear:both; margin:; padding:; 順序:上右下左

六、邊框屬性:(Border)

  • border-style: dotted;(點線) dashed;(虛線) solid; double;(雙線) groove;(槽線) ridge;(脊狀) inset;(凹陷) outset; border-width:; 邊框寬度
  • border-color:#;
  • 簡寫方法border:width style color; /*簡寫*/

七、清單屬性:(List-style)

  1. 類型list-style-type: disc;(圓點) circle;(圓圈) square;(方塊) decimal;(數字) lower-roman;(小羅碼數字) upper-roman; lower-alpha; upper-alpha;
  2. 位置list-style-position: outside;(外) inside;
  3. 圖像list-style-image: url(..);

八、定位屬性:(Position)

  1. Position: absolute; relative; static;
  2. visibility: inherit; visible; hidden;
  3. overflow: visible; hidden; scroll; auto;
  4. clip: rect(12px,auto,12px,auto) (裁切)

九、CSS文字屬性:

  1. color : #999999; /*文字顔色*/
  2. font-family : 宋體,sans-serif; /*文字字型*/
  3. font-size : 9pt; /*文字大小*/
  4. font-style:itelic; /*文字斜體*/
  5. font-variant:small-caps; /*小字型*/
  6. letter-spacing : 1pt; /*字間距離*/
  7. line-height : 200%; /*設定行高*/
  8. font-weight:bold; /*文字粗體*/
  9. vertical-align:sub; /*下标字*/
  10. vertical-align:super; /*上标字*/
  11. text-decoration:line-through; /*加删除線*/
  12. text-decoration: overline; /*加頂線*/
  13. text-decoration:underline; /*加下劃線*/
  14. text-decoration:none; /*删除連結下劃線*/
  15. text-transform : capitalize; /*首字大寫*/
  16. text-transform : uppercase; /*英文大寫*/
  17. text-transform : lowercase; /*英文小寫*/
  18. text-align:right; /*文字右對齊*/
  19. text-align:left; /*文字左對齊*/
  20. text-align:center; /*文字居中對齊*/
  21. text-align:justify; /*文字分散對齊*/
  22. vertical-align屬性
  • vertical-align:top; /*垂直向上對齊*/
  • vertical-align:bottom; /*垂直向下對齊*/
  • vertical-align:middle; /*垂直居中對齊*/
  • vertical-align:text-top; /*文字垂直向上對齊*/
  • vertical-align:text-bottom; /*文字垂直向下對齊*/

十、CSS邊框空白

  1.  padding-top:10px; /*上邊框留白白*/
  2. padding-right:10px; /*右邊框留白白*/
  3. padding-bottom:10px; /*下邊框留白白*/
  4. padding-left:10px; /*左邊框留白白*/

頁面布局常用詞彙

  1. header 頭部/頁眉;
  2. index 首頁/索引;
  3. logo 标志;
  4. nav/sub_nav 導航/子導航;
  5. banner 橫幅廣告;
  6. main/content 主體/内容;
  7. container/con 容器;
  8. wrapper/wrap 包裹(類似于container);
  9. menu 菜單;
  10. sub_menu/second_menu 子菜單/二級菜單;
  11. list 清單;
  12. section 分區/分塊(類似于div);
  13. article 文章;
  14. aside 側邊欄/廣告;
  15. footer 頁腳/底部;
  16. title/sub_title 标題/副标題;
  17. news 新聞;
  18. hot 熱點;
  19. pro 産品(product);
  20. company 公司;
  21. msg/info 資訊(message)/消息;
  22. ads 廣告(advertisements);
  23. icon 小圖示;
  24. img 圖檔(image);
  25. copyright 版權;
  26. contact_us 聯系我們;
  27. friend_link 友情連結;
  28. tel 聯系電話(telephone);
  29. address 位址;
  30. & nbsp;  空格(&和n之間的空格去掉,不要忘記分号);
  31. <br/> (文字末尾添加)換行;

CSS樣式(style) CSS 層疊樣式表 (Cascading Style Sheets) ;

  1. background 背景;
  2. background: -webkit-gradient(top red orange yellow green lightblue blue purple) 顔色漸變;
  3. position 位置/定位;
  4. relative/absolute/fixed 相對定位/絕對定位/固定定位;
  5. float 浮動;
  6. clear 清除;
  7. vertical-align: middle/top/bottom; 垂直居中/上/下;
  8. line-height 行高;
  9. margin 外邊距;
  10. padding 内邊距;
  11. border 邊框;
  12. solid/dashed/dotted 實線/線虛線/點虛線;
  13. border-radius 圓角;
  14. shadow 陰影;
  15. display 展示;
  16. hidden 隐藏;
  17. block/inline-block 塊元素/行内塊;
  18. overflow 溢出;
  19. cursor 光标;
  20. cursor:pointer; 滑鼠移上變為小手;
  21. animation 動畫;
  22. css sprites 雪碧圖/圖檔精靈;
  23. column 分列;
  24. flex 彈性(布局);

表單(form)與表格(table)

  1. form 表單;
  2. action 行為;
  3. method 方式/方法;
  4. input 輸入框;
  5. label 标簽;
  6. password 密碼;
  7. radio 單選框;
  8. checkbox 複選框;
  9. btn 按鈕(button);
  10. submit/reset 送出/重置;
  11. textarea 文本域;
  12. select/option 選擇框/選擇項;
  13. placeholder 占位符(起提示作用);
  14. search 搜尋;
  15. icon 小圖示;
  16. autofocus 自動聚焦;
  17. disabled 禁用;
  18. checked 選中(單選框/複選框);
  19. selected 預設選擇項(下拉選擇框);
  20. required 必填項;
  21. readonly 隻讀;
  22. table 表格;
  23. thead/tbody/tfoot 表格标題/主體/底部;
  24. colspan 跨列;
  25. rowspan 跨行;
  26. cellspacing 單元格間距(類似于margin);
  27. cellpadding 單元格邊距(類似于padding);
  28. border-collapse: collapse; 邊框合并(用于table上)。

本文完〜

下一篇: vim縮進