[关闭]
@Tean 2017-09-15T07:03:49.000000Z 字数 3654 阅读 1280

CSS

css


1. CSS

层叠样式表

2. 页面中引入CSS方式

2.1 内联样式

  1. <div style="color:#f00; font-size:14px;"></div>

2.2 内部样式表

  1. <style>
  2. div {color:#f00; font-size:24px;}
  3. </style>

2.3 外部样式表

  • link标签 : 通过HTML引入外部样式表(推荐的写法)
  1. <link href="外部样式路径" rel="stylesheet">
  • @import : 通过CSS引入外部样式表(不推荐使用)
  1. <style>
  2. @import url("外部样式表路径");
  3. </style>

3. CSS选择器

3.1 基本选择器

  • 标签选择器
  1. div {color:red;}
  • 类选择器
  1. .cla-name {color:green;}
  2. // 使用
  3. <div class="cla-name other-cla"></div>
  • id选择器
  1. #idName {color:green;}
  2. // 使用
  3. <div id="idName"></div>

3.2 复合选择器

  • 后代选择器 : 以空格隔开
  1. div p {}
  2. .container li {}
  3. .parent .son span {}
  • 并集选择器 : 以逗号隔开
  1. div, p {}
  2. .container, li {}
  3. .parent, .son, span {}
  • 交集选择器:
  1. div.cla {}
  2. p#main {}
  3. div .main p.info, div#first {}
  • 全局选择器
  1. * {}
  2. div * {}

3.3 其它选择器

  • div > p
  • div + p
  • div ~ p
  • div[id]
  • ...

4. 常用样式

4.1 字体样式

  1. div {
  2. font-size: 12px; font-size: 2em; font-size:2rem;
  3. font-family: Arial; font-family: Consolas, "Microsoft YaHei";
  4. font-weight: 100-900 | normal | bold ;
  5. font-style: normal | italic | oblique;
  6. font: italic bold 12px/24px Arial;
  7. font: 12px Arial;
  8. }

4.2 文本样式

  1. div {
  2. text-align:left | center | right | justify;
  3. text-decoration: none | underline | overline | line-through;
  4. text-indent: 24px | 2em;
  5. text-overflow: ellipsis | clip;
  6. color: #ff0000 | #f00 | red | rgb(255,0,0) | rgba(255,0,0,.4);
  7. line-height: 24px | 100% | 1;
  8. letter-spacing: 5px;
  9. word-spacing:10px;
  10. }
  11. // 超出显示...
  12. .ddd {
  13. overflow: hidden;
  14. text-overflow: ellipsis;
  15. white-space: no-wrap;
  16. }
  17. // 强制换行
  18. .break-word {
  19. word-wrap: break-word;
  20. word-break: break-all;
  21. }

4.3 背景样式

  1. div {
  2. background-color: red;
  3. background-image: url("图片路径");
  4. background-repeat: repeat | no-repeat | repeat-x | repeat-y;
  5. background-position: 2px 3px | left top | 50% 50%;
  6. background-attachment: scroll | fixed;
  7. background: red url('') no-repeat center center fixed;
  8. }

4.4 盒子模型

  1. div {
  2. margin: 10px;
  3. margin: 10px 20px;
  4. margin: 10px 20px 30px;
  5. margin: 10px 20px 30px 40px;
  6. margin: 0 auto;
  7. padding:10px;
  8. border: 1px solid red;
  9. border-top: 1px solid red;
  10. border-style: solid;
  11. border-style: solid dashed dotted double;
  12. border-top-style: solid;
  13. }
  14. // 三角箭头
  15. .trg {
  16. width:0;
  17. height:0;
  18. border-width:10px;
  19. border-style: solid;
  20. border-color: transparent red transparent transparent;
  21. }

4.5 浮动

  1. div {
  2. float: none | left | right;
  3. clear: none | left | right | both;
  4. }
  5. // 常用清楚浮动方式
  6. .clearfix:after {
  7. content: '';
  8. display: table;
  9. clear: both;
  10. }
  11. .clearfix {
  12. *zoom: 1;
  13. }

4.6 定位

  1. div {
  2. position: static | relative | absolute | fixed;
  3. z-index: 2;
  4. left: 12px;
  5. top: 23px;
  6. bottom: 23px;
  7. right: 23px;
  8. }

4.7 透明

  1. div {
  2. opacity: .3;
  3. filter: alpha(opacity=30);
  4. background: rgba(0,0,0,.3);
  5. }

4.8 列表

  1. ul {
  2. list-style-type: none;
  3. }

4.9 表格

  1. table {
  2. border-collapse: collapse;
  3. }

4.10 display & visibility & overflow

  1. div {
  2. display: inline | none | block | inline-block | table | table-cell | flex;
  3. visibility: hidden | visiable;
  4. overflow: hidden | visiable | scroll | auto;
  5. }

4.11 其它

  1. div {
  2. width: 100px;
  3. height:100px;
  4. }

5. CSS Hack

  1. /*Mozilla内核浏览器:firefox3.5+*/
  2. -moz-transform: rotate | scale | skew | translate ;
  3. /*Webkit内核浏览器:Safari and Chrome*/
  4. -webkit-transform: rotate | scale | skew | translate ;
  5. /*Opera*/
  6. -o-transform: rotate | scale | skew | translate ;
  7. /*IE9*/
  8. -ms-transform: rotate | scale | skew | translate ;
  9. /*W3C标准*/
  10. transform: rotate | scale | skew | translate ;
  1. width: 100px; // 所有浏览器
  2. *width: 80px; // IE6、7
  3. _width: 60px; // IE6
  4. width:100px; // 所有浏览器
  5. width:60px\9; // IE浏览器
  6. width:80px\0; // IE8+
  7. width:50px\9\0; // IE9+
  8. div {
  9. float:left;
  10. margin-left: 11px;
  11. _display: inline;
  12. }
  1. <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]-->
  2. <!--[if IE]> 所有的IE可识别 <![endif]-->
  3. <!--[if IE 5.0]> 只有IE5.0可以识别 <![endif]-->
  4. <!--[if IE 5]> 仅IE5.0与IE5.5可以识别 <![endif]-->
  5. <!--[if gt IE 5.0]> IE5.0以上版本都可以识别 <![endif]-->
  6. <!--[if lt IE 6]> IE6以下版本可识别 <![endif]-->
  7. <!--[if IE 6]>this is ie6<![endif]-->
  8. <!--[if gt IE 6]> IE6以上版本可识别 <![endif]-->
  9. <!--[if lt IE 7]> IE7以下版本可识别 <![endif]-->
  10. <!--[if IE 7]>this is ie7<![endif]-->
  11. <!--[if gt IE 7]> IE7以上版本可识别 <![endif]-->
  12. <!--[if lt IE 8]> IE8以下版本可识别 <![endif]-->
  13. <!--[if IE 8]>this is ie8<![endif]-->
  14. <!--[if gt IE 8]> IE8以上版本可识别 <![endif]-->
  15. <!--[if lt IE 9]> IE9以下版本可识别 <![endif]-->
  16. <!--[if IE 9]>this is ie9<![endif]-->

6. 浏览器内核

  • IE : Trident ['traɪd(ə)nt]
  • Edge : EdgeHTML
  • Firefox : Gecko ['gekəʊ]
  • Chrome : 之前浏览器: Webkit,新版本用的是Bink(基于webkit开发的)
  • Safari : Webkit
  • Opera : 之前浏览器: Presto ['prestəʊ],新版本用的是Bink(基于webkit开发的)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注