CSS常用的代码段

1.CSS RESET

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font-size: 100%;
    vertical-align: baseline;
    background: transparent;
}

body {
    line-height: 1;
}

ol, ul {
    list-style: none;
}

blockquote, q {
    quotes: none;
}

blockquote:before, blockquote:after,
q:before, q:after {
    content: ‘‘;
    content: none;
}

/* remember to define focus styles! */
:focus {
    outline: 0;
}

/* remember to highlight inserts somehow! */
ins {
    text-decoration: none;
}

del {
    text-decoration: line-through;
}

/* tables still need ‘cellspacing="0"‘ in the markup */
table {
    border-collapse: collapse;
    border-spacing: 0;
}

2.根据不同格式的链接显示不同的下划线

/* external links */
a[href^="http://"]{
    padding-right: 20px;
    background: url(external.gif) no-repeat center right;
}

/* emails */
a[href^="mailto:"]{
    padding-right: 20px;
    background: url(email.png) no-repeat center right;
}

/* pdfs */
a[href$=".pdf"]{
    padding-right: 20px;
    background: url(pdf.png) no-repeat center right;
}

3.IE中取出textarea的滚动条

textarea{
    overflow:auto;
}

4.浏览器特定的hacks

/* IE 6 */
* html .yourclass { }

/* IE 7 */
*+html .yourclass{ }

/* IE 7 and modern browsers */
html>body .yourclass { }

/* Modern browsers (not IE 7) */
html>/**/body .yourclass { }

/* Opera 9.27 and below */
html:first-child .yourclass { }

/* Safari */
html[xmlns*=""] body:last-child .yourclass { }

/* Safari 3+, Chrome 1+, Opera 9+, Fx 3.5+ */
body:nth-of-type(1) .yourclass { }

/* Safari 3+, Chrome 1+, Opera 9+, Fx 3.5+ */
body:first-of-type .yourclass {  }

/* Safari 3+, Chrome 1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
 .yourclass  {  }
}

5.clearfix

.clearfix:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content: " ";
    clear: both;
    height: 0;
}

.clearfix { display: inline-block; }

/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */

6.固定宽度且剧中

.wrapper {
     width:960px;
     margin:0 auto;
}

7.圆角

.round{
   -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
   border-radius: 10px; /* future proofing */
   -khtml-border-radius: 10px; /* for old Konqueror browsers */
}

8.伪元素像第一个字母添加样式

p:first-letter{
   display:block;
   margin:5px 0 0 5px;
   float:left;
   color:#000;
   font-size:60px;
   font-family:Georgia;
}

9.使用@font-face

@font-face {
    font-family: ‘MyFontFamily‘;
    src: url(‘myfont-webfont.eot?‘) format(‘eot‘),
         url(‘myfont-webfont.woff‘) format(‘woff‘),
         url(‘myfont-webfont.ttf‘)  format(‘truetype‘),
         url(‘myfont-webfont.svg#svgFontName‘) format(‘svg‘);
    }

10.跨浏览器的inline-block

li {
        width: 200px;
        min-height: 250px;
        border: 1px solid #000;
        display: -moz-inline-stack;
        display: inline-block;
        vertical-align: top;
        margin: 5px;
        zoom: 1;
        *display: inline;
        _height: 250px;
}

11.fixed footer

#footer {
   position:fixed;
   left:0px;
   bottom:0px;
   height:30px;
   width:100%;
   background:#999;
}

/* IE 6 */
* html #footer {
   position:absolute;
   top:expression((0-(footer.offsetHeight)+(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight)+(ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop))+‘px‘);
}

12.css3 media queries 智能设备判断不同的媒体

/* Smartphones (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen
and (min-width : 321px) {
/* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen
and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}

/* iPads (landscape) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}

/* Desktops and laptops ----------- */
@media only screen
and (min-width : 1224px) {
/* Styles */
}

/* Large screens ----------- */
@media only screen
and (min-width : 1824px) {
/* Styles */
}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}

13.多图背景

#multiple-images {
    background: url(image_1.png) top left no-repeat,
    url(image_2.png) bottom left no-repeat,
    url(image_3.png) bottom right no-repeat;
}

14.多列

#columns-3 {
   text-align: justify;
   -moz-column-count: 3;
   -moz-column-gap: 12px;
   -moz-column-rule: 1px solid #c4c8cc;
   -webkit-column-count: 3;
   -webkit-column-gap: 12px;
   -webkit-column-rule: 1px solid #c4c8cc;
}

15.在IE的最小高度

.box {
    min-height:500px;
    height:auto !important;
    height:500px;
}

16.突出显示文本样式

::selection {
   color: #000000;
   background-color: #FF0000;
}

::-moz-selection {
   color: #000000;
   background: #FF0000;
}

17.box-shadow

 box-shadow: 0px 3px 3px rgba(0,0,0,0.2);
 -moz-box-shadow: 0px 3px 3px rgba(0,0,0,0.2);
 -webkit-box-shadow: 0px 3px 3px rgba(0,0,0,0.2);

18.占位符文本样式

::-webkit-input-placeholder { color: #ccc; font-style:italic }
:-moz-placeholder           { color: #ccc; font-style:italic }

19.CSS3 3D文字效果

h1 {
  text-shadow: 0 1px 0 #ccc,
               0 2px 0 #c9c9c9,
               0 3px 0 #bbb,
               0 4px 0 #b9b9b9,
               0 5px 0 #aaa,
               0 6px 1px rgba(0,0,0,.1),
               0 0 5px rgba(0,0,0,.1),
               0 1px 3px rgba(0,0,0,.3),
               0 3px 5px rgba(0,0,0,.2),
               0 5px 10px rgba(0,0,0,.25),
               0 10px 10px rgba(0,0,0,.2),
               0 20px 20px rgba(0,0,0,.15);
}

20.webkit边界半径修订

-webkit-background-clip: padding-box;

21.更好的兼容IE浏览器

<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

22.CSS3 工具提示

a {
  color: #900;
  text-decoration: none;
}

a:hover {
  color: red;
  position: relative;
}

a[title]:hover:after {
  content: attr(title);
  padding: 4px 8px;
  color: #333;
  position: absolute;
  left: 0;
  top: 100%;
  white-space: nowrap;
  z-index: 20px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-box-shadow: 0px 0px 4px #222;
  -webkit-box-shadow: 0px 0px 4px #222;
  box-shadow: 0px 0px 4px #222;
  background-image: -moz-linear-gradient(top, #eeeeee, #cccccc);
  background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #eeeeee),color-stop(1, #cccccc));
}

23.CSS3背景大小

body {
  background: #000 url(http://birdoflight.files.wordpress.com/2009/10/frida-kahlo-1.jpg) center center fixed no-repeat;
  -moz-background-size: cover;
  background-size: cover;
}

@media only all and (max-width: 1024px) and (max-height: 768px) {
  body {
    -moz-background-size: 1024px 768px;
    background-size: 1024px 768px;
  }
}

24.跨浏览器的CSS页面卷曲阴影

.page-curl {
position: relative;
background: #ffffff;
background: -moz-linear-gradient(top, #ffffff 0%, #e5e5e5 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#e5e5e5));
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=‘#ffffff‘, endColorstr=‘#e5e5e5‘,GradientType=0 );
-webkit-box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.3); }

.page-curl:after {
z-index: -1;
position: absolute;
background: transparent;
width: 70%;
height: 55%;
content: ‘‘;
right: 10px;
bottom: 10px;
-webkit-transform: skew(15deg) rotate(5deg);
-webkit-box-shadow: 8px 12px 10px rgba(0, 0, 0, 0.3);
-moz-transform: skew(15deg) rotate(5deg);
-moz-box-shadow: 8px 12px 10px rgba(0, 0, 0, 0.3); }

.page-curl:before {
z-index: -2;
position: absolute;
background: transparent;
width: 70%;
height: 55%;
content: ‘‘;
left: 10px;
bottom: 10px;
-webkit-transform: skew(-15deg) rotate(-5deg);
-webkit-box-shadow: -8px 12px 10px rgba(0, 0, 0, 0.3);
-moz-transform: skew(-15deg) rotate(-5deg);
-moz-box-shadow: -8px 12px 10px rgba(0, 0, 0, 0.3); }

25.CSS3模糊文字

.blur {
    color: transparent;
    text-shadow: 0 0 5px rgba(0,0,0,0.5);
}

26.修复IE6/7 margin/padding双倍间距错误

ul li
{
  float: right;
  margin-right: 10px;
  *display: inline; /*Target IE7 and bellow*/
  _display: inline; /*Target IE6 and bellow*/
}
/* This example fixes the double right margin bug */

27.链接伪类的顺序

a:link {color: blue;}
a:visited {color: purple;}
a:hover {color: red;}
a:active {color: yellow;}

28.响应式的html meta标签

<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">
时间: 2024-08-25 03:29:03

CSS常用的代码段的相关文章

【cocos2dx 3.2】使用搜狗输入法短词,复用常用的代码段

引言: 在写cocos2dx代码的时候,发现有些代码是固定格式,经常要重用 故把它们不断收集起来,然后添加到搜狗输入法的短词里面 这样需要重用的时候,直接输快捷键,效率就提高了 把常用的代码段收集起来,做成一个CHM文档方便设置短词时查阅 搜狗输入法添加短词: 右键搜狗输入法,点击[设置属性] 选择[高级],然后选择右边的[自定义短语设置] 点击[添加新定义],添加自己的快捷键和代码段 常用的代码段(不断更新) 新建一个类的时候,需要写create方法和init方法,快捷键设为cre //创建c

常用JS代码段

1.表单文本框的focus 1 va($('input[type=text]')); 2 function va(a) { 3 for(var i=0;i < a.length; i++){ 4 var $txt= a.eq(i); 5 $txt.focus(function(){ 6 if($(this).val() == this.defaultValue){ 7 $(this).val(''); 8 } 9 }); 10 $txt.blur(function(){ 11 if($(this

前端用户体验优化: JS &amp; CSS 各类效果代码段

前言 不定时更新 在线预览 https://zzyper.github.io/opti... 在线预览的源码 https://github.com/zzyper/opt... 部分内容仅兼容webkit内核,其他内核自行查询 可控密度的虚线分隔线 css .line { height: 1px; width: 100%; transform: scaleY(0.4); -webkit-transform: scaleY(0.4); background-image: linear-gradien

《超实用的JavaScript代码段》—— 读后总结

这本书全是代码,从头到尾跟着坐下来确实收获很多.比那些古板的教科书式的理解更多,不过书中并不是每个例子都做了,有的作者封装的太多,觉得看了收获不多,就没细看——比如模块渐变.有空好好学学这段的代码. 其他的代码都收录于github,参考:CodeJS/JS 书中内容 这本书讲述了很多工作中常用的代码段,有一些也是目前网站很流行的技术.比如图片放大镜,导航置顶,广告浮动等等. 也通过这些例子梳理了下javascript的相关内容,主要包括3个方面: 1 ECMAScript核心 2 DOM 3 B

vs代码段简写知识

Visua Studio代码段基础知识 描述: 代码段是使用上下文菜单命令或者热键(hotkeys)的组合在代码文件中插入的可重用的代码.它们可以插入整个类或方法.能够提高我们开发的效率,节约时间. Visual Studio自带了一些常用的代码段,比如在C#中,插入指定的代码段.就以try finaly为示例. 首先在类中输入tryf,然后连接点击两次Tab键.就会自动生成try finally代码段.如图: 创建代码段 创建代码段必须是XML文件,可以根据创建的代码段文件修改相应的元素和增加

css常用代码大全以及css兼容(转载)

css常见的快捷开发代码汇总(长期更新),包括CSS3代码,有一些css效果很顽固,经常会一时找不出解决方案,网络上也有很多的工具和高手提供了具体的浏览器兼容代码,这个页面我今后会经常整理,希望能够帮到重构和正在找这些效果的代码,如果你有更好的实现方案,不妨告诉我,或者你有解决不了的,也不妨告诉我.你想找浏览器兼容写法吗?点CSS浏览器兼容与hack代码(经验汇总,保持最新) Css背景颜色透明(#ddd) 请参考在线版制作 .liter{ filter:progid:DXImageTransf

十五个常用的jquery代码段

十五个常用的jquery代码段 回到顶部按钮 通过使用 jQuery 中的 animate 和 scrollTop 方法,你无需插件便可创建一个简单地回到顶部动画: 1 // Back to top 2 $('a.top').click(function (e) { 3 e.preventDefault(); 4 $(document.body).animate({scrollTop: 0}, 800); 5 }); 1 <!-- Create an anchor tag --> 2 <

Visual Studio常用小技巧一:代码段+快捷键+插件=效率

用了visual studio 5年多,也该给自己做下备忘录了.每次进新的组换新的电脑,安装自己熟悉的环境又得重新配置,不做些备忘老会忘记一些东西.工具用的好,效率自然翻倍. 1,代码段 在Visual Studio可以自己添加自己喜欢的代码段.visual studio也自带了很多默认的代码段,比如tryf,for等等.我经常需要的就是文件头部的版权说明,还有就是代码注释.添加方法很简单. 微软文档:演练:创建代码段 版权添加方法:Visual Studio 2015为例 找到 C:\Prog

CSS常用代码

以下是常用的代码收集,没有任何技术含量,只是填坑的积累.转载请注明出处,谢谢. 因为提交比较麻烦,后来转置github:https://github.com/jsfront/src/blob/master/css.md 一. css 2.x code 1. 文字换行 /*强制不换行*/ white-space:nowrap; /*自动换行*/ word-wrap: break-word; word-break: normal; /*强制英文单词断行*/ word-break:break-all;