在一篇文章中收集所有的CSS代码片段几乎是不可能的事情,但是我们这里列出了一些相对于其他的更有用的代码片段,不要被这些代码的长度所吓到,因为它们都很容易实现,并且具有良好的文档。除了那些解决常见的恼人的问题外,也包含了一些解决新问题的新技术。
1. 垂直对齐
如果你之前遇到过这个问题,你就应该知道它是多么的烦人,幸运的是,现在你可以使用CSS3变换来解决这个问题:
1 | .vc{ |
2 | position: relative; |
3 | top: 50%; |
4 | -webkit-transform: translateY(-50%); |
5 | -o-transform: translateY(-50%); |
6 | transform: translateY(-50%); |
7 | } |
2. 只在一侧或者两侧具有投影
01 | .box-shadow { |
02 | background-color: #AC92EC; |
03 | width: 160px; |
04 | height: 90px; |
05 | margin-top: -45px; |
06 | margin-left: -80px; |
07 | position: absolute; |
08 | top: 50%; |
09 | left: 50%; |
10 | } |
11 | .box-shadow:after { |
12 | content: "" ; |
13 | width: 150px; |
14 | height: 1px; |
15 | margin-top: 88px; |
16 | margin-left: -75px; |
17 | display: block; |
18 | position: absolute; |
19 | left: 50%; |
20 | z-index: -1; |
21 | -webkit-box-shadow: 0px 0px 8px 2px #000000; |
22 | -moz-box-shadow: 0px 0px 8px 2px #000000; |
23 | box-shadow: 0px 0px 8px 2px #000000; |
24 | } |
3. 渐变背景动画效果
从CSS3开始,动画变得非常的酷了,但是切不可过分的使用它们。下面这一技巧巧妙地的移动背景位置,使其看起来像动画一样:
01 | button { |
02 | padding: 15px; |
03 | background-image: linear-gradient(#FC6E51, #FFF); |
04 | background-size: auto 200%; |
05 | background-position: 0 100%; |
06 | transition: background-position 0.5s; |
07 | } |
08 | button:hover { |
09 | background-position: 0 0; |
10 | } |
4. 将文本分成多列
1 | div |
2 | { |
3 | -moz-column- count :3; /* Firefox */ |
4 | -webkit-column- count :3; /* Safari and Chrome */ |
5 | column- count :3; |
6 | } |
5. 表格自动宽度
1 | td { |
2 | white-space: nowrap; |
3 | } |
6. 像出版物一样,第一个字变得大些
1 | p:first-child::first-letter{ |
2 | font-family: "papyrus" ; |
3 | font-size: 28px; |
4 | font-weight: bold; |
5 | } |
7. 特定浏览器的CSS Hacks的完整列表
有时候解决跨浏览器兼容性可能会非常的棘手,但这些特定浏览器的技巧可能会帮你解决问题。
01 | /***** Selector Hacks ******/ |
02 | |
03 | /* IE6 and below */ |
04 | * html #uno { color: red } |
05 | |
06 | /* IE7 */ |
07 | *:first-child+html #dos { color: red } |
08 | |
09 | /* IE7, FF, Saf, Opera */ |
10 | html>body #tres { color: red } |
11 | |
12 | /* IE8, FF, Saf, Opera (Everything but IE 6,7) */ |
13 | html> /**/ body #cuatro { color: red } |
14 | |
15 | /* Opera 9.27 and below, safari 2 */ |
16 | html:first-child #cinco { color: red } |
17 | |
18 | /* Safari 2-3 */ |
19 | html[xmlns*= "" ] body:last-child #seis { color: red } |
20 | |
21 | /* safari 3+, chrome 1+, opera9+, ff 3.5+ */ |
22 | body:nth-of-type(1) #siete { color: red } |
23 | |
24 | /* safari 3+, chrome 1+, opera9+, ff 3.5+ */ |
25 | body:first-of-type #ocho { color: red } |
26 | |
27 | /* saf3+, chrome1+ */ |
28 | @media screen and (-webkit-min-device-pixel-ratio:0) { |
29 | #diez { color: red } |
30 | } |
31 | |
32 | /* iPhone / mobile webkit */ |
33 | @media screen and (max-device-width: 480px) { |
34 | #veintiseis { color: red } |
35 | } |
36 | |
37 | |
38 | /* Safari 2 - 3.1 */ |
39 | html[xmlns*= "" ]:root #trece { color: red } |
40 | |
41 | /* Safari 2 - 3.1, Opera 9.25 */ |
42 | *|html[xmlns*= "" ] #catorce { color: red } |
43 | |
44 | /* Everything but IE6-8 */ |
45 | :root *> #quince { color: red } |
46 | |
47 | /* IE7 */ |
48 | *+html #dieciocho { color: red } |
49 | |
50 | /* Firefox only. 1+ */ |
51 | #veinticuatro, x:-moz-any-link { color: red } |
52 | |
53 | /* Firefox 3.0+ */ |
54 | #veinticinco, x:-moz-any-link, x: default { color: red } |
55 | |
56 | |
57 | |
58 | /***** Attribute Hacks ******/ |
59 | |
60 | /* IE6 */ |
61 | #once { _color: blue } |
62 | |
63 | /* IE6, IE7 */ |
64 | #doce { *color: blue; /* or #color: blue */ } |
65 | |
66 | /* Everything but IE6 */ |
67 | #diecisiete { color /**/ : blue } |
68 | |
69 | /* IE6, IE7, IE8 */ |
70 | #diecinueve { color: blue\9; } |
71 | |
72 | /* IE7, IE8 */ |
73 | #veinte { color /*\**/ : blue\9; } |
74 | |
75 | /* IE6, IE7 -- acts as an !important */ |
76 | #veintesiete { color: blue !ie; } /* string after ! can be anything */ |
8. 创建模糊文本
1 | .blurry-text { |
2 | color: transparent; |
3 | text-shadow: 0 0 5px rgba(0,0,0,0.5); |
4 | } |
9. 不使用表格实现跨浏览器垂直水平居中图片
这段代码可以在一个已知宽高的容器内垂直水平居中一个未知大小的图片,这是 IE 的一个hack:
01 | <figure class = 'logo' > |
02 | <span></span> |
03 | <img class = 'photo' /> |
04 | </figure> |
05 | .logo { |
06 | display: block; |
07 | text-align: center; |
08 | display: block; |
09 | text-align: center; |
10 | vertical-align: middle; |
11 | border: 4px solid #dddddd; |
12 | padding: 4px; |
13 | height: 74px; |
14 | width: 74px; } |
15 | .logo * { |
16 | display: inline-block; |
17 | height: 100%; |
18 | vertical-align: middle; } |
19 | .logo .photo { |
20 | height: auto; |
21 | width: auto; |
22 | max-width: 100%; |
23 | max-height: 100%; } |
10. 高亮选中的 input
01 | // HTML |
02 | <input id= "mycheck1" type= "checkbox" /> |
03 | <label for = "mycheck1" >Check box label here</label> |
04 | <br /> |
05 | <input id= "mycheck2" type= "checkbox" checked/> |
06 | <label for = "mycheck2" >Check box label here</label> |
07 | <br /> |
08 | <input id= "mycheck3" type= "checkbox" /> |
09 | <label for = "mycheck3" >Check box label here</label> |
10 |
11 | // CSS |
12 | input:checked + label{ |
13 | background: yellow; |
14 | } |
11. 跨浏览器透明度
1 | selector { |
2 | filter: alpha(opacity=50); /* internet explorer */ |
3 | -khtml-opacity: 0.5; /* khtml, old safari */ |
4 | -moz-opacity: 0.5; /* mozilla, netscape */ |
5 | opacity: 0.5; /* fx, safari, opera */ |
6 | } |
12. CSS投影
01 | // 外投影 |
02 | .shadow { |
03 | -moz-box-shadow: 5px 5px 5px #ccc; |
04 | -webkit-box-shadow: 5px 5px 5px #ccc; |
05 | box-shadow: 5px 5px 5px #ccc; |
06 | } |
07 |
08 | // 内投影 |
09 | .shadow { |
10 | -moz-box-shadow:inset 0 0 10px #000000; |
11 | -webkit-box-shadow:inset 0 0 10px #000000; |
12 | box-shadow:inset 0 0 10px #000000; |
13 | } |
13. 跨浏览器最小高度
1 | #div { |
2 | min-height: 500px; |
3 | height:auto !important; |
4 | height: 500px; |
5 | } |
14. 固定 Footer
01 | #footer { |
02 | position:fixed; |
03 | left:0px; |
04 | bottom:0px; |
05 | height:30px; |
06 | width:100%; |
07 | background:#999; |
08 | } |
09 | |
10 | /* IE 6 */ |
11 | * html #footer { |
12 | position:absolute; |
13 | 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' ); |
14 | } |
15. 清除浮动Clearfix
01 | /* slightly enhanced, universal clearfix hack */ |
02 | .clearfix:after { |
03 | visibility: hidden; |
04 | display: block; |
05 | font-size: 0; |
06 | content: " " ; |
07 | clear: both; |
08 | height: 0; |
09 | } |
10 | .clearfix { display: inline-block; } |
11 | /* start commented backslash hack \*/ |
12 | * html .clearfix { height: 1%; } |
13 | .clearfix { display: block; } |
14 | /* close commented backslash hack */ |
16. 给可点击元素添加手型光标
1 | a[href], input[type= 'submit' ], input[type= 'image' ], label[ for ], select, button, .pointer { |
2 | cursor: pointer; |
3 | } |
17. iPad 定向CSS
01 | <!-- css --> |
02 | @media only screen and (max-device-width: 1024px) and (orientation:portrait) { |
03 | .landscape { display: none; } |
04 | } |
05 | @media only screen and (max-device-width: 1024px) and (orientation:landscape) { |
06 | .portrait { display: none; } |
07 | } |
08 | |
09 | <!-- example markup --> |
10 | <h1 class = "portrait" >Your device orientation is "portrait" <h1> |
11 | <h1 class = "landscape" >Your device orientation is "landscape" <h1> |
18. Pre 标签内文本换行
01 | pre{ |
02 | height: 120px; |
03 | overflow: auto; |
04 | font-family: “Consolas”,monospace; |
05 | font-size: 9pt; |
06 | text-align:left; |
07 | background-color: #FCF7EC; |
08 | overflow-x: auto; /* Use horizontal scroller if needed; for Firefox 2, not |
09 | white-space: pre-wrap; /* css-3 */ |
10 | white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */ |
11 | word-wrap: break -word; /* Internet Explorer 5.5+ */ |
12 | margin: 0px 0px 0px 0px; |
13 | padding:5px 5px 3px 5px; |
14 | white-space : normal; /* crucial for IE 6, maybe 7? */ |
15 | } |
19. CSS3媒体查询
01 | /* Smartphones (portrait and landscape) ----------- */ |
02 | @media only screen |
03 | and (min-device-width : 320px) |
04 | and (max-device-width : 480px) { |
05 | /* Styles */ |
06 | } |
07 | |
08 | /* Smartphones (landscape) ----------- */ |
09 | @media only screen |
10 | and (min-width : 321px) { |
11 | /* Styles */ |
12 | } |
13 | |
14 | /* Smartphones (portrait) ----------- */ |
15 | @media only screen |
16 | and (max-width : 320px) { |
17 | /* Styles */ |
18 | } |
19 | |
20 | /* iPads (portrait and landscape) ----------- */ |
21 | @media only screen |
22 | and (min-device-width : 768px) |
23 | and (max-device-width : 1024px) { |
24 | /* Styles */ |
25 | } |
26 | |
27 | /* iPads (landscape) ----------- */ |
28 | @media only screen |
29 | and (min-device-width : 768px) |
30 | and (max-device-width : 1024px) |
31 | and (orientation : landscape) { |
32 | /* Styles */ |
33 | } |
34 | |
35 | /* iPads (portrait) ----------- */ |
36 | @media only screen |
37 | and (min-device-width : 768px) |
38 | and (max-device-width : 1024px) |
39 | and (orientation : portrait) { |
40 | /* Styles */ |
41 | } |
42 | |
43 | /* Desktops and laptops ----------- */ |
44 | @media only screen |
45 | and (min-width : 1224px) { |
46 | /* Styles */ |
47 | } |
48 | |
49 | /* Large screens ----------- */ |
50 | @media only screen |
51 | and (min-width : 1824px) { |
52 | /* Styles */ |
53 | } |
54 | |
55 | /* iPhone 4 ----------- */ |
56 | @media |
57 | only screen and (-webkit-min-device-pixel-ratio : 1.5), |
58 | only screen and (min-device-pixel-ratio : 1.5) { |
59 | /* Styles */ |
60 | } |
20. 重置加载
01 | html, body, div, span, applet, object, iframe, |
02 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, |
03 | a, abbr, acronym, address, big, cite, code, |
04 | del, dfn, em, font, img, ins, kbd, q, s, samp, |
05 | small, strike, strong, sub, sup, tt, var , |
06 | dl, dt, dd, ol, ul, li, |
07 | fieldset, form, label, legend, |
08 | table, caption, tbody, tfoot, thead, tr, th, td { |
09 | margin: 0; |
10 | padding: 0; |
11 | border: 0; |
12 | outline: 0; |
13 | font-weight: inherit; |
14 | font-style: inherit; |
15 | font-size: 100%; |
16 | font-family: inherit; |
17 | vertical-align: baseline; |
18 | } |
19 | /* remember to define focus styles! */ |
20 | :focus { |
21 | outline: 0; |
22 | } |
23 | body { |
24 | line-height: 1; |
25 | color: black; |
26 | background: white; |
27 | } |
28 | ol, ul { |
29 | list-style: none; |
30 | } |
31 | /* tables still need 'cellspacing="0"' in the markup */ |
32 | table { |
33 | border-collapse: separate; |
34 | border-spacing: 0; |
35 | } |
36 | caption, th, td { |
37 | text-align: left; |
38 | font-weight: normal; |
39 | } |
40 | blockquote:before, blockquote:after, |
41 | q:before, q:after { |
42 | content: "" ; |
43 | } |
44 | blockquote, q { |
45 | quotes: "" "" ; |
46 | } |
21. 多边框
元素必须是相对定位,且具有足够的padding来显示多余的边框:
01 | #borders { |
02 | position:relative; |
03 | z-index:1; |
04 | padding:30px; |
05 | border:5px solid #f00; |
06 | background:#ff9600; |
07 | } |
08 | #borders:before { |
09 | content: "" ; |
10 | position:absolute; |
11 | z-index:-1; |
12 | top:5px; |
13 | left:5px; |
14 | right:5px; |
15 | bottom:5px; |
16 | border:5px solid #ffea00; |
17 | background:#4aa929; |
18 | } |
19 | |
20 | #borders:after { |
21 | content: "" ; |
22 | position:absolute; |
23 | z-index:-1; |
24 | top:15px; |
25 | left:15px; |
26 | right:15px; |
27 | bottom:15px; |
28 | border:5px solid #00b4ff; |
29 | background:#fff; |
30 | } |
22. 移除IE中textarea的滚动条
1 | textarea { overflow: auto; } |
23. 简单但好看的引用样式
01 | blockquote { |
02 | background:#f9f9f9; |
03 | border-left:10px solid #ccc; |
04 | margin:1.5em 10px; |
05 | padding:.5em 10px; |
06 | quotes: "\201C" "\201D" "\2018" "\2019" ; |
07 | } |
08 | blockquote:before { |
09 | color:#ccc; |
10 | content:open-quote; |
11 | font-size:4em; |
12 | line-height:.1em; |
13 | margin-right:.25em; |
14 | vertical-align:-.4em; |
15 | } |
16 | blockquote p { |
17 | display:inline; |
18 | } |
24. :-moz-placeholder
01 | <!doctype html> |
02 | <html> |
03 | <head> |
04 | <title>Placeholder demo</title> |
05 | <style type= "text/css" > |
06 | input:-moz-placeholder { |
07 | color: green; |
08 | } |
09 | </style> |
10 | </head> |
11 | <body> |
12 | <input id= "test" placeholder= "Placeholder text!" > |
13 | </body> |
14 | </html> |
25. 另一种固定footer的方式
01 | * { margin:0; padding:0; } |
02 | |
03 | html, body, #wrap { height: 100%; } |
04 | |
05 | body > #wrap {height: auto; min-height: 100%;} |
06 | |
07 | #main { padding-bottom: 150px; } /* must be same height as the footer */ |
08 | |
09 | #footer { |
10 | position: relative; |
11 | margin-top: -150px; /* negative value of footer height */ |
12 | height: 150px; |
13 | clear:both;} |
14 | |
15 | /* CLEAR FIX*/ |
16 | .clearfix:after {content: "." ; |
17 | display: block; |
18 | height: 0; |
19 | clear: both; |
20 | visibility: hidden;} |
21 | .clearfix {display: inline-block;} |
22 | /* Hides from IE-mac \*/ |
23 | * html .clearfix { height: 1%;} |
24 | .clearfix {display: block;} |
25 | /* End hide from IE-mac */ |
26 | <div id= "wrap" > |
27 | |
28 | <div id= "main" class = "clearfix" > |
29 | |
30 | </div> |
31 | |
32 | </div> |
33 | |
34 | <div id= "footer" > |
35 | |
36 | </div> |
26. 背景透明
1 | .rgba { |
2 | background-color: transparent; |
3 | background-color: rgba(200,200,200,0.8); |
4 | filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99dddddd,endColorstr=#99dddddd); |
5 | -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99dddddd,endColorstr=#99dddddd)" ; |
6 | } |
27. 居中未知宽度的DIV元素
01 | .content { |
02 | margin: 0 auto 8px; |
03 | display: table; |
04 | } |
05 | |
06 | .content div { |
07 | display: table-cell; |
08 | } |
09 | |
10 | <!--[ if IE]> |
11 | .content { |
12 | display: block; |
13 | text-align: center; |
14 | } |
15 | .content div { |
16 | display: inline; |
17 | zoom: 1; |
18 | } |
19 | <![ endif ]--> |
28. 根据文件类型设置样式
01 | /* external links */ |
02 | a[href^= "http://" ] |
03 | { |
04 | padding-right: 13px; |
05 | background: url(external.gif) no-repeat center right; |
06 | } |
07 | |
08 | /* emails */ |
09 | a[href^= "mailto:" ] |
10 | { |
11 | padding-right: 20px; |
12 | background: url(email.png) no-repeat center right; |
13 | } |
14 | |
15 | /* pdfs */ |
16 | a[href$= ".pdf" ] |
17 | { |
18 | padding-right: 18px; |
19 | background: url(acrobat.png) no-repeat center right; |
20 | } |
29. 解决IE6/7双倍margin/padding问题
1 | ul li |
2 | { |
3 | float: right; |
4 | margin-right: 10px; |
5 | *display: inline; /*Target IE7 and bellow*/ |
6 | _display: inline; /*Target IE6 and bellow*/ |
7 | } |
8 | /* This example fixes the double right margin bug */ |
30. 更改选中文本的样式
01 | ::selection |
02 | { |
03 | color: white; |
04 | background-color: red; |
05 | } |
06 | |
07 | ::-moz-selection /* Firefox needs an extra attention for this */ |
08 | { |
09 | color: white; |
10 | background-color: red; |
11 | } |
31. 首字下沉
1 | p:first-letter{ |
2 | display:block; |
3 | margin:5px 0 0 5px; |
4 | float:left; |
5 | color:#FF3366; |
6 | font-size:60px; |
7 | font-family:Georgia; |
8 | } |