10+有用的css代码片段

10+有用的css代码片段

1. 垂直对齐

1
2
3
4
5
position:relative;
top: 50%;
-webkit-transform: translateY(-50%);
-o-transform: translateY(-50%);
transoform:translateY(-50%);

查看Demo

2. 只在一侧或者两侧具有投影

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
.box-shadow {
background-color: #AC92EC;
width: 160px;
height: 90px;
margin-top: -45px;
margin-left: -80px;
position: absolute;
top: 50%;
left: 50%;
}
.box-shadow:after {
content: "";
width: 150px;
height: 1px;
margin-top: 88px;
margin-left: -75px;
display: block;
position: absolute;
left: 50%;
z-index: -1;
-webkit-box-shadow: 0px 0px 8px 2px #000000;
-moz-box-shadow: 0px 0px 8px 2px #000000;
box-shadow: 0px 0px 8px 2px #000000;
}

查看Demo

3. 渐变背景动画效果

1
2
3
4
5
6
7
8
9
10
button{
padding: 15px;
background-image:liner-grafient(#FC6E51,#FFF);
background-size:auto 200%;
background-position:0 100%;
transition:background-position 0.5s;
}
button:hover{
background-position:0 0;
}

查看Demo

4. 将文本分成多行

1
2
3
4
5
div{
-moz-column-count:3;
-webkit-column-count:3;
column-count:3;
}

查看Demo

5. 表格自动宽度

1
td{white-space: nowrap}

查看Demo

6. 像出版物一样,第一个字变得大些

1
2
3
4
5
p:first-child::first-letter{
font-family:"papyrus";
font-size: 28px;
font-weight: bold;
}

查看Demo

7. 特定浏览器的CSS Hacks的完整列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/***** Selector Hacks ******/
/* IE6 and below*/
* html #uno {color: red}
/* IE7 */
*:first-child+html #dos {color: red}
/* IE7, FF, Saf, Opera */
html>body #tres {color: red}
/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
html>/**/body #cuatro { color: red }
/* Opera 9.27 and below, safari 2 */
html:first-child #cinco { color: red }
/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { color: red }
/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) #siete { color: red }
/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type #ocho { color: red }
/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
#diez { color: red }
}
/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
#veintiseis { color: red }
}
/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece { color: red }
/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=""] #catorce { color: red }
/* Everything but IE6-8 */
:root *> #quince { color: red }
/* IE7 */
*+html #dieciocho { color: red }
/* Firefox only. 1+ */
#veinticuatro, x:-moz-any-link { color: red }
/* Firefox 3.0+ */
#veinticinco, x:-moz-any-link, x:default { color: red }
/***** Attribute Hacks ******/
/* IE6 */
#once { _color: blue }
/* IE6, IE7 */
#doce { *color: blue; /* or #color: blue */ }
/* Everything but IE6 */
#diecisiete { color/**/: blue }
/* IE6, IE7, IE8 */
#diecinueve { color: blue\9; }
/* IE7, IE8 */
#veinte { color/*\**/: blue\9; }
/* IE6, IE7 -- acts as an !important */
#veintesiete { color: blue !ie; } /* string after ! can be anything */

8. 创建模糊文本

1
2
3
4
.blurry-text{
color: transparent;
text-shadow: 0 0 5px rgba(0,0,0,.5);
}

查看Demo

9. 不使用表格实现跨浏览器垂直水平居中图片

这段代码可以在一个已知宽高的容器内垂直水平居中一个未知大小的图片,这是 IE 的一个hack:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<figure class='logo'>
<img class='photo'/>
</figure>
.logo {
display: block;
text-align: center;
vertical-align: middle;
border: 4px solid #dddddd;
padding: 4px;
height: 74px;
width: 74px;
}
.logo *{
display: inline-block;
height: 100%;
vertical-align: middle;
}
.logo .photo {
height: auto;
width: auto;
max-width: 100%;
max-height: 100%;
}

查看Demo

10. 高亮选中的input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// HTML
<input id="mycheck1" type="checkbox" />
<label for="mycheck1">Check box label here</label>
<br />
<input id="mycheck2" type="checkbox" checked/>
<label for="mycheck2">Check box label here</label>
<br />
<input id="mycheck3" type="checkbox" />
<label for="mycheck3">Check box label here</label>
// CSS
input:checked + label{
background: yellow;
}