【解疑答惑】css中经常被忽略的代码陷阱

时间:2022-05-04
本文章向大家介绍【解疑答惑】css中经常被忽略的代码陷阱,主要内容包括一. css 2.x code、二. css 3 code、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

css大家都认为是很简单东西,但是是代码就有让人头疼的时候,只是多少的问题,伴着小编走过的路,在前端多少也滚了一些坑,今天为了方便后来者,把收集到的东西跟大家分享一下,有需要的朋友可以当作参考,希望对各位盟友有帮助:

一. css 2.x code

1. 文字换行

/*强制不换行*/
white-space:nowrap;
/*自动换行*/
word-wrap: break-word;
word-break: normal;
/*强制英文单词断行*/
word-break:break-all;

2. 两端对齐

text-align:justify;text-justify:inter-ideogra

3. 去掉Webkit(chrome)浏览器中input(文本框)或textarea的黄色焦点框

input,button,select,textarea{
 outline:none;}
textarea{ 
 font-size:13px; resize:none;}

去掉chrome记住密码后自动填充表单的黄色背景

4. ie6:

position:fixed

.fixed-top

/* position fixed Top */

{

position:fixed;

bottom:auto;top:0;

}* html

.fixed-top

/* IE6 position fixed Top */

{

position:absolute;

bottom:auto;

top:expression(eval(document.documentElement.scrollTop));

}

*html{

background-1image:url(about:blank);

background-attachment:fixed;}

5. clearfix

.clearfix:after{
visibility:hidden;
display:block;
font-size:0;
content:" ";
clear:both;
height:0;
}
.clearfix{
display:inline-block;
}
html[xmlns] .clearfix{display:block;}
* html .clearfix{height:1%;}
.clearfix{*zoom: 1;}
.clearfix:after{clear:both;display:table;content:"”;}
.clearfix{overflow:hidden;_zoom:1;}

6. seperate-table

.tab{border-collapse:separate;border:1px solid #e0e0e0;}.tab th,.tab td{padding:3px;font-size:12px;background:#f5f9fb;border:1px solid;border-color:#fff #deedf6 #deedf6 #fff;}.tab th{background:#edf4f0;}.tab tr.even td{background:#fff;}

7. min-height: 最小高度兼容代码

.minheight500{min-height:500px;height:auto !important;height:500px;overflow:visible;}

8. 鼠标不允许点击:

cursor:not-allowed;

9. mac font: osx平台字体优化

font-family:"Hiragino Sans GB","Hiragino Sans GB W3",'微软雅黑';

10. 省略号:

.ellipsis{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}

二. css 3 code

1. 渐变:

.a{background:-webkit-gradient(linear,left top,left bottom,from(#69bdf9),to(#4aa7e8));background:-moz-linear-gradient(top,#67bcf8,#3b96d6);background:-o-linear-gradient(top,#67bcf8,#3b96d6);background:linear-gradient(top,#67bcf8,#3b96d6);}

2.投影:

.b{box-shadow:inset 1px -1px 0 #f1f1f1;text-shadow:1px 1px 0px #630;}filter:progid:DXImageTransform.Microsoft.gradient(enabled='true',startColorstr='#99000000',endColorstr='#99000000');background:rgba(0,0,0,.6);

background:rgba(0,0,0,0.5);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr='#50000000',endColorstr='#50000000')9;

看哪个startColorstr和endColorstr,一共8位,后6位是RGB的颜色代码,前两位是16进制的

比如60%透明,就是256x0.6=154,再换算成16进制=9A background-image:-ms-linear-gradient(top, #fff, #ddd); ie10渐变 http://www.iefans.net/ie10-yulanban-css3-jianbian/

alpha透明兼容代码生成:

http://leegorous.net/tools/bg-alpha.html

透明兼容

3. search占位

::-webkit-input-placeholder {}::-moz-input-placeholder {}input:focus::-webkit-input-placeholder { color: transparent; }-webkit-appearance:none;  google边框去除
input[type="search"]{-webkit-appearance:textfield;} // 去除chrome默认样式
http://i.wanz.im/2011/02/04/remove_border_from_input_type_search/
http://blog.csdn.net/do_it__/article/details/6789699
line-height: normal; /* for non-ie */line-height: 22px9; /* for ie */   

4. 渐变

background: #bde25e;
 /* Old browsers */
background: -moz-linear-gradient(top, #bde25e 2%, #8bb31d 100%); 
/* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(2%,#bde25e), color-stop(100%,#8bb31d)); 
/* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #bde25e 2%,#8bb31d 100%); 
/* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #bde25e 2%,#8bb31d 100%); 
/* Opera 11.10+ */
background: -ms-linear-gradient(top, #bde25e 2%,#8bb31d 100%); 
/* IE10+ */
background: linear-gradient(to bottom, #bde25e 2%,#8bb31d 100%); 
/* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#bde25e', endColorstr='#8bb31d',GradientType=0 ); 
/* IE6-9 */
 @media screen and (max-width:1220px) and (min-width:1151px) {
    #wrapper {font-size:15px;}}

5.阻止默认事件

pointer-events:none; 

以上是小编整理的部分常用的css代码,篇幅所限,后续会陆续更新感谢盟友的阅读;