30段超实用CSS代码(1)

来源:互联网 发布:自己的淘宝后台在哪里 编辑:程序博客网 时间:2024/04/20 20:11

Web开发者的福利30段超实用CSS代码。上周,发表了一篇《Web开发者不容错过的20段CSS代码》,大家一致觉得很实用。该文是笔者对后30个的翻译,希望对大家有帮助。


  1.花式连字符(&)

  这个类应该在span元素里使用,并且里面包括&字符。它使用经典的serif字体和斜体来增强&符号。

1
2
3
4
5
.amp {
    font-family: Baskerville, 'Goudy Old Style', Palatino, 'Book Antiqua',serif;
    font-style:italic;
    font-weight:normal;
}

  源码地址: http://css-tricks.com/snippets/css/fancy-ampersand/

  2.段落首字符下沉

  通常,这种效果会出现在印刷媒体上,如报纸或书籍。同样,如果网页布局合理,它也可以使用在Web页面上,它仅针对段落使用,但你也可以与单个元素绑定。

1
2
3
4
5
6
7
8
p:first-letter{
    display:block;
    margin:5px0 0 5px;
    float:left;
    color:#ff3366;
    font-size:5.4em;
    font-family: Georgia, Times New Roman, serif;
}

  3.内层CSS3盒阴影

  盒阴影(box shadow)属性几乎可以运用在任何元素上,它们看起来都非常好看。下面这段代码主要聚焦内层阴影的设计。

1
2
3
4
5
#mydiv {
    -moz-box-shadow:inset2px 0 4px #000;
    -webkit-box-shadow:inset2px 0 4px #000;
    box-shadow:inset2px 0 4px #000;
}

  4.外层CSS3盒阴影

  下面介绍一段外层阴影代码设计,注意,代码里的第三个参数表示模糊距离(blur distance),而第四个参数表示铺开的(spread)距离。关于这些值的设计,你可以前往 W3Schools学习。

1
2
3
4
5
#mydiv {
    -webkit-box-shadow:02px 2px -2px rgba(0,0,0,0.52);
    -moz-box-shadow:02px 2px -2px rgba(0,0,0,0.52);
    box-shadow:02px 2px -2px rgba(0,0,0,0.52);
}

  5.三角形列表符号

  该符号只能在CSS3里生成,在主流浏览器中,这是一项非常酷的技术。而其唯一的潜在问题是缺乏对后退方法的支持。 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ul {
    margin:0.75em0;
    padding:01em;
    list-style:none;
}
li:before {
    content:"";
    border-color:transparent#111;
    border-style:solid;
    border-width:0.35em0 0.35em 0.45em;
    display:block;
    height:0;
    width:0;
    left:-1em;
    top:0.9em;
    position:relative;
}

  源码地址: http://jsfiddle.net/chriscoyier/yNZTU/

  6.居中对齐并设置固定宽度

1
2
3
4
#page-wrap {
    width:800px;
    margin:0auto;
}

  源码地址: http://css-tricks.com/snippets/css/centering-a-website/

  7.CSS3列文本

1
2
3
4
5
6
7
8
9
#columns-3{
    text-align:justify;
    -moz-column-count:3;
    -moz-column-gap:12px;
    -moz-column-rule:1pxsolid #c4c8cc;
    -webkit-column-count:3;
    -webkit-column-gap:12px;
    -webkit-column-rule:1pxsolid #c4c8cc;
}

  源码地址: http://www.djavupixel.com/development/css-development/master-css3-ultimate-css-code-snippets/

  8.固定页脚

  在网页里添加固定的页脚其实非常简单,并且也很实用。有些网站的页脚设计得很漂亮,可以给网站呈现出一个完美的结尾。

1
2
3
4
5
6
7
8
9
10
11
12
13
#footer {
    position:fixed;
    left:0px;
    bottom:0px;
    height:30px;
    width:100%;
    background:#444;
}
/* 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');
}

  源码地址: http://www.flashjunior.ch/school/footers/fixed.cfm

  9.IE 6下修复PNG格式的透明度

  在网站里使用透明的图像已成为一种很普遍的做法,其始于.gif图片格式,但现在也涉及到.png图片格式。而一些老版本的IE不支持透明度,下面这段代码会很好地解决这一问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.bg {
    width:200px;
    height:100px;
    background:url(/folder/yourimage.png)no-repeat;
    _background:none;
    _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/folder/yourimage.png',sizingMethod='crop');
}
/* 1px gif method */
img, .png {
    position:relative;
    behavior: expression((this.runtimeStyle.behavior="none")&&(this.pngSet?this.pngSet=true:(this.nodeName == "IMG"&& this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = "none",
       this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ this.src + "', sizingMethod='image')",
       this.src = "images/transparent.gif"):(this.origBg = this.origBg? this.origBg :this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''),
       this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ this.origBg + "', sizingMethod='crop')",
       this.runtimeStyle.backgroundImage = "none")),this.pngSet=true));
}

  源码地址: http://css-tricks.com/snippets/css/png-hack-for-ie-6/

  10.跨浏览器设置最小高度

  有时开发者需要对HTML元素设置最小高度,然而,这个效果却无法兼容IE和老版本的Firefox,下面这段代码可以修复这个问题。

1
2
3
4
5
#container {
    min-height:550px;
    height:auto!important;
    height:550px;
}

  11. CSS3发光输入框

  下面的这段代码重写了浏览器的默认行为,可以让Chrome和Safari浏览器下普通的表单输入框产生发光效果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
input[type=text], textarea {
    -webkit-transition:all0.30s ease-in-out;
    -moz-transition:all0.30s ease-in-out;
    -ms-transition:all0.30s ease-in-out;
    -o-transition:all0.30s ease-in-out;
    outline:none;
    padding:3px0px 3px 3px;
    margin:5px1px 3px 0px;
    border:1pxsolid #ddd;
}
input[type=text]:focus, textarea:focus {
    box-shadow:00 5px rgba(81,203,238,1);
    padding:3px0px 3px 3px;
    margin:5px1px 3px 0px;
    border:1pxsolid rgba(81,203,238,1);
}

  源码地址: http://css-tricks.com/snippets/css/glowing-blue-input-highlights/

  12.基于文件类型来创建链接样式

  下面这段代码通过使用CSS选择器和图像图标来实现各种类型的链接样式,可能会用到各种协议(HTTP、FTP、IRC,mailto),或者是文件本身的类型(mp3、avi、pdf)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* external links */
a[href^="http://"] {
    padding-right:13px;
    background:url('external.gif')no-repeatcenter right;
}
/* emails */
a[href^="mailto:"] {
    padding-right:20px;
    background:url('email.png')no-repeatcenter right;
}
/* pdfs */
a[href$=".pdf"] {
    padding-right:18px;
    background:url('acrobat.png')no-repeatcenter right;
}

  源码地址: http://www.designyourway.net/blog/resources/31-css-code-snippets-to-make-you-a-better-coder/

  13.pre标签封装代码

  pre标签常用来对代码进行布局,可以解决因为行太多带来的滚动条问题。下面这段代码就使用pre来封装代码,让内容直接显示在页面中。

1
2
3
4
5
6
7
pre{
    white-space: pre-wrap;       /* css-3 */
    white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
    white-space: -pre-wrap;      /* Opera 4-6 */
    white-space: -o-pre-wrap;    /* Opera 7 */
    word-wrap: break-word;       /* Internet Explorer 5.5+ */
}

  源码地址: http://css-tricks.com/snippets/css/make-pre-text-wrap/

  14.鼠标指向时变成手型

  网页中有许多item在点击时,鼠标不会变成小手的形状。这套CSS选择器会强迫鼠标越过一些关键元素和其他对象一起来使用.pointer这个类。

1
2
3
a[href], input[type='submit'], input[type='image'], label[for], select, button, .pointer{
    cursor:pointer;
}

  源码地址: http://css-tricks.com/snippets/css/give-clickable-elements-a-pointer-cursor/

  15.页面顶部阴影

  简单地把下面这段代码拷贝到页面里,它会在body元素之前产生黑色的,渐渐变淡的阴影。这种效果通常用来给一个文本框或网页元素加阴影。

1
2
3
4
5
6
7
8
9
10
11
12
body:before {
    content:"";
    position:fixed;
    top:-10px;
    left:0;
    width:100%;
    height:10px;
    -webkit-box-shadow:0px0px 10px rgba(0,0,0,.8);
    -moz-box-shadow:0px0px 10px rgba(0,0,0,.8);
    box-shadow:0px0px 10px rgba(0,0,0,.8);
    z-index:100;
}

  源码地址: http://css-tricks.com/snippets/css/top-shadow/

  

  英文来源:css-snippets-for-designers

原创粉丝点击