实用的60个CSS代码片段[下]
1 31、有趣的& 2 3 .amp { 4 font-family: Baskerville, 'Goudy Old Style', Palatino, 'Book Antiqua', serif; 5 font-style: italic; 6 font-weight: normal; 7 } 8 9 10 32、大字段落 11 12 p:first-letter{ 13 display: block; 14 margin: 5px 0 0 5px; 15 float: left; 16 color: #ff3366; 17 font-size: 5.4em; 18 font-family: Georgia, Times New Roman, serif; 19 } 20 21 22 33、内部CSS3 盒阴影 23 24 #mydiv { 25 -moz-box-shadow: inset 2px 0 4px #000; 26 -webkit-box-shadow: inset 2px 0 4px #000; 27 box-shadow: inset 2px 0 4px #000;} 28 29 30 34、外部CSS3 盒阴影 31 32 #mydiv { 33 -webkit-box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.52); 34 -moz-box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.52); 35 box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.52); 36 } 37 38 39 35、三角形列表项目符号 40 41 42 ul { 43 margin: 0.75em 0; 44 padding: 0 1em; 45 list-style: none; 46 } 47 li:before { 48 content: ""; 49 border-color: transparent #111; 50 border-style: solid; 51 border-width: 0.35em 0 0.35em 0.45em; 52 display: block; 53 height: 0; 54 width: 0; 55 left: -1em; 56 top: 0.9em; 57 position: relative; 58 } 59 60 61 36、固定宽度的居中布局 62 63 64 #page-wrap { 65 width: 800px; 66 margin: 0 auto; 67 } 68 69 70 37、CSS3 列文本 71 72 73 #columns-3 { 74 text-align: justify; 75 -moz-column-count: 3; 76 -moz-column-gap: 12px; 77 -moz-column-rule: 1px solid #c4c8cc; 78 -webkit-column-count: 3; 79 -webkit-column-gap: 12px; 80 -webkit-column-rule: 1px solid #c4c8cc;} 81 82 83 38、CSS固定页脚 84 85 #footer { 86 position: fixed; 87 left: 0px; 88 bottom: 0px; 89 height: 30px; 90 width: 100%; 91 background: #444; 92 } 93 /* IE 6 */ 94 * html #footer { 95 position: absolute; 96 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'); 97 } 98 99 100 39、IE6的PNG透明修复 101 102 .bg { 103 width:200px; 104 height:100px; 105 background: url(/folder/yourimage.png) no-repeat; 106 _background:none; 107 _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/folder/yourimage.png',sizingMethod='crop'); 108 } 109 /* 1px gif method */ 110 img, .png { 111 position: relative; 112 behavior: expression((this.runtimeStyle.behavior="none")&&(this.pngSet?this.pngSet=true:(this.nodeName == "IMG" && this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = "none", 113 this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='image')", 114 this.src = "images/transparent.gif"):(this.origBg = this.origBg? this.origBg :this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''), 115 this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.origBg + "', sizingMethod='crop')", 116 this.runtimeStyle.backgroundImage = "none")),this.pngSet=true)); 117 } 118 119 120 40、跨浏览器设置最小高度 121 122 #container { 123 min-height: 550px; 124 height: auto !important; 125 height: 550px; 126 } 127 128 129 41、CSS3 鲜艳的输入 130 131 input[type=text], textarea { 132 -webkit-transition: all 0.30s ease-in-out; 133 -moz-transition: all 0.30s ease-in-out; 134 -ms-transition: all 0.30s ease-in-out; 135 -o-transition: all 0.30s ease-in-out; 136 outline: none; 137 padding: 3px 0px 3px 3px; 138 margin: 5px 1px 3px 0px; 139 border: 1px solid #ddd;} 140 input[type=text]:focus, textarea:focus { 141 box-shadow: 0 0 5px rgba(81, 203, 238, 1); 142 padding: 3px 0px 3px 3px; 143 margin: 5px 1px 3px 0px; 144 border: 1px solid rgba(81, 203, 238, 1); 145 } 146 147 148 42、基于文件类型的链接样式 149 150 /* external links */ 151 a[href^="http://"] { 152 padding-right: 13px; 153 background: url('external.gif') no-repeat center right; 154 } 155 /* emails */ 156 a[href^="mailto:"] { 157 padding-right: 20px; 158 background: url('email.png') no-repeat center right; 159 } 160 /* pdfs */ 161 a[href$=".pdf"] { 162 padding-right: 18px; 163 background: url('acrobat.png') no-repeat center right; 164 } 165 166 167 43、强制换行 168 169 pre { 170 white-space: pre-wrap; /* css-3 */ 171 white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ 172 white-space: -pre-wrap; /* Opera 4-6 */ 173 white-space: -o-pre-wrap; /* Opera 7 */ 174 word-wrap: break-word; /* Internet Explorer 5.5+ */ 175 } 176 177 178 44、在可点击的项目上强制手型 179 180 181 a[href], input[type='submit'], input[type='image'], label[for], select, button, .pointer { 182 cursor: pointer; 183 } 184 185 186 45、网页顶部盒阴影 187 188 body:before { 189 content: ""; 190 position: fixed; 191 top: -10px; 192 left: 0; 193 width: 100%; 194 height: 10px; 195 -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8); 196 -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8); 197 box-shadow: 0px 0px 10px rgba(0,0,0,.8); 198 z-index: 100; 199 } 200 201 202 46、CSS3对话气泡 203 204 205 .chat-bubble { 206 background-color: #ededed; 207 border: 2px solid #666; 208 font-size: 35px; 209 line-height: 1.3em; 210 margin: 10px auto; 211 padding: 10px; 212 position: relative; 213 text-align: center; 214 width: 300px; 215 -moz-border-radius: 20px; 216 -webkit-border-radius: 20px; 217 -moz-box-shadow: 0 0 5px #888; 218 -webkit-box-shadow: 0 0 5px #888; 219 font-family: 'Bangers', arial, serif; 220 } 221 .chat-bubble-arrow-border { 222 border-color: #666 transparent transparent transparent; 223 border-style: solid; 224 border-width: 20px; 225 height: 0; 226 width: 0; 227 position: absolute; 228 bottom: -42px; 229 left: 30px; 230 } 231 .chat-bubble-arrow { 232 border-color: #ededed transparent transparent transparent; 233 border-style: solid; 234 border-width: 20px; 235 height: 0; 236 width: 0; 237 position: absolute; 238 bottom: -39px; 239 left: 30px; 240 } 241 242 243 47、H1-H5默认样式 244 245 h1,h2,h3,h4,h5{ 246 color: #005a9c;} 247 h1{ 248 font-size: 2.6em; 249 line-height: 2.45em; 250 } 251 h2{ 252 font-size: 2.1em; 253 line-height: 1.9em; 254 } 255 h3{ 256 font-size: 1.8em; 257 line-height: 1.65em; 258 } 259 h4{ 260 font-size: 1.65em; 261 line-height: 1.4em; 262 } 263 h5{ 264 font-size: 1.4em; 265 line-height: 1.25em; 266 } 267 268 269 48、纯CSS背景噪音 270 271 body { 272 background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAAAUVBMVEWFhYWDg4N3d3dtbW17e3t1dXWBgYGHh4d5eXlzc3OLi4ubm5uVlZWPj4+NjY19fX2JiYl/f39ra2uRkZGZmZlpaWmXl5dvb29xcXGTk5NnZ2c8TV1mAAAAG3RSTlNAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEAvEOwtAAAFVklEQVR4XpWWB67c2BUFb3g557T/hRo9/WUMZHlgr4Bg8Z4qQgQJlHI4A8SzFVrapvmTF9O7dmYRFZ60YiBhJRCgh1FYhiLAmdvX0CzTOpNE77ME0Zty/nWWzchDtiqrmQDeuv3powQ5ta2eN0FY0InkqDD73lT9c9lEzwUNqgFHs9VQce3TVClFCQrSTfOiYkVJQBmpbq2L6iZavPnAPcoU0dSw0SUTqz/GtrGuXfbyyBniKykOWQWGqwwMA7QiYAxi+IlPdqo+hYHnUt5ZPfnsHJyNiDtnpJyayNBkF6cWoYGAMY92U2hXHF/C1M8uP/ZtYdiuj26UdAdQQSXQErwSOMzt/XWRWAz5GuSBIkwG1H3FabJ2OsUOUhGC6tK4EMtJO0ttC6IBD3kM0ve0tJwMdSfjZo+EEISaeTr9P3wYrGjXqyC1krcKdhMpxEnt5JetoulscpyzhXN5FRpuPHvbeQaKxFAEB6EN+cYN6xD7RYGpXpNndMmZgM5Dcs3YSNFDHUo2LGfZuukSWyUYirJAdYbF3MfqEKmjM+I2Ef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fj7ei6UUVk4AIl3MyD4MSSTOFgSwsH/QJWaQ5as7ZcmgBZkzjjU1UrQ74ci1gWBCSGHtuV1H2mhSnO3Wp/3fEV5a+4wz//6qy8JxjZsmxxy5+4w9CDNJY09T072iKG0EnOS0arEYgXqYnXcYHwjTtUNAcMelOd4xpkoqiTYICWFq0JSiPfPDQdnt+4/wuqcXY47QILbgAAAABJRU5ErkJggg==); 273 background-color: #0094d0;} 274 275 276 49、持久的列表排序 277 278 ol.chapters { 279 list-style: none; 280 margin-left: 0; 281 } 282 ol.chapters > li:before { 283 content: counter(chapter) ". "; 284 counter-increment: chapter; 285 font-weight: bold; 286 float: left; 287 width: 40px; 288 } 289 ol.chapters li { 290 clear: left; 291 } 292 ol.start { 293 counter-reset: chapter; 294 } 295 ol.continue { 296 counter-reset: chapter 11; 297 } 298 299 300 50、CSS悬浮提示文本 301 302 a { 303 border-bottom:1px solid #bbb; 304 color:#666; 305 display:inline-block; 306 position:relative; 307 text-decoration:none; 308 } 309 a:hover, 310 a:focus { 311 color:#36c;} 312 a:active { 313 top:1px; 314 } 315 /* Tooltip styling */ 316 a[data-tooltip]:after { 317 border-top: 8px solid #222; 318 border-top: 8px solid hsla(0,0%,0%,.85); 319 border-left: 8px solid transparent; 320 border-right: 8px solid transparent; 321 content: ""; 322 display: none; 323 height: 0; 324 width: 0; 325 left: 25%; 326 position: absolute; 327 } 328 a[data-tooltip]:before { 329 background: #222; 330 background: hsla(0,0%,0%,.85); 331 color: #f6f6f6; 332 content: attr(data-tooltip); 333 display: none; 334 font-family: sans-serif; 335 font-size: 14px; 336 height: 32px; 337 left: 0; 338 line-height: 32px; 339 padding: 0 15px; 340 position: absolute; 341 text-shadow: 0 1px 1px hsla(0,0%,0%,1); 342 white-space: nowrap; 343 -webkit-border-radius: 5px; 344 -moz-border-radius: 5px; 345 -o-border-radius: 5px; 346 border-radius: 5px; 347 } 348 a[data-tooltip]:hover:after { 349 display: block; 350 top: -9px; 351 } 352 a[data-tooltip]:hover:before { 353 display: block; 354 top: -41px; 355 } 356 a[data-tooltip]:active:after { 357 top: -10px; 358 } 359 a[data-tooltip]:active:before { 360 top: -42px; 361 } 362 363 364 51、深灰色的圆形按钮 365 366 .graybtn { 367 -moz-box-shadow:inset 0px 1px 0px 0px #ffffff; 368 -webkit-box-shadow:inset 0px 1px 0px 0px #ffffff; 369 box-shadow:inset 0px 1px 0px 0px #ffffff; 370 background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ffffff), color-stop(1, #d1d1d1) ); 371 background:-moz-linear-gradient( center top, #ffffff 5%, #d1d1d1 100% ); 372 filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#d1d1d1'); 373 background-color:#ffffff; 374 -moz-border-radius:6px; 375 -webkit-border-radius:6px; 376 border-radius:6px; 377 border:1px solid #dcdcdc; 378 display:inline-block; 379 color:#777777; 380 font-family:arial; 381 font-size:15px; 382 font-weight:bold; 383 padding:6px 24px; 384 text-decoration:none; 385 text-shadow:1px 1px 0px #ffffff;} 386 .graybtn:hover { 387 background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #d1d1d1), color-stop(1, #ffffff) ); 388 background:-moz-linear-gradient( center top, #d1d1d1 5%, #ffffff 100% ); 389 filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#d1d1d1', endColorstr='#ffffff'); 390 background-color:#d1d1d1;} 391 .graybtn:active { 392 position:relative; 393 top:1px; 394 } 395 396 52、在可打印的网页中显示URLs 397 398 @media print { 399 a:after { 400 content: " [" attr(href) "] "; 401 } 402 } 403 404 405 53、禁用移动Webkit的选择高亮 406 407 body { 408 -webkit-touch-callout: none; 409 -webkit-user-select: none; 410 -khtml-user-select: none; 411 -moz-user-select: none; 412 -ms-user-select: none; 413 user-select: none; 414 } 415 416 417 54、CSS3 圆点图案 418 419 body { 420 background: radial-gradient(circle, white 10%, transparent 10%), 421 radial-gradient(circle, white 10%, black 10%) 50px 50px; 422 background-size: 100px 100px; 423 } 424 425 426 55、CSS3 方格图案 427 428 body { 429 background-color: white; 430 background-image: linear-gradient(45deg, black 25%, transparent 25%, transparent 75%, black 75%, black), 431 linear-gradient(45deg, black 25%, transparent 25%, transparent 75%, black 75%, black); 432 background-size: 100px 100px; 433 background-position: 0 0, 50px 50px; 434 } 435 436 437 56、Github的fork色带 438 439 .ribbon { 440 background-color: #a00; 441 overflow: hidden; 442 /* top left corner */ 443 position: absolute; 444 left: -3em; 445 top: 2.5em; 446 /* 45 deg ccw rotation */ 447 -moz-transform: rotate(-45deg); 448 -webkit-transform: rotate(-45deg); 449 /* shadow */ 450 -moz-box-shadow: 0 0 1em #888; 451 -webkit-box-shadow: 0 0 1em #888;} 452 .ribbon a { 453 border: 1px solid #faa; 454 color: #fff; 455 display: block; 456 font: bold 81.25% 'Helvetiva Neue', Helvetica, Arial, sans-serif; 457 margin: 0.05em 0 0.075em 0; 458 padding: 0.5em 3.5em; 459 text-align: center; 460 text-decoration: none; 461 /* shadow */ 462 text-shadow: 0 0 0.5em #444;} 463 464 465 57、CSS font属性缩写 466 467 p { 468 font: italic small-caps bold 1.2em/1.0em Arial, Tahoma, Helvetica; 469 } 470 471 472 58、论文页面的卷曲效果 473 474 ul.box { 475 position: relative; 476 z-index: 1; /* prevent shadows falling behind containers with backgrounds */ 477 overflow: hidden; 478 list-style: none; 479 margin: 0; 480 padding: 0; 481 } 482 ul.box li { 483 position: relative; 484 float: left; 485 width: 250px; 486 height: 150px; 487 padding: 0; 488 border: 1px solid #efefef; 489 margin: 0 30px 30px 0; 490 background: #fff; 491 -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset; 492 -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset; 493 box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset; 494 } 495 ul.box li:before, 496 ul.box li:after { 497 content: ''; 498 z-index: -1; 499 position: absolute; 500 left: 10px; 501 bottom: 10px; 502 width: 70%; 503 max-width: 300px; /* avoid rotation causing ugly appearance at large container widths */ 504 max-height: 100px; 505 height: 55%; 506 -webkit-box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3); 507 -moz-box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3); 508 box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3); 509 -webkit-transform: skew(-15deg) rotate(-6deg); 510 -moz-transform: skew(-15deg) rotate(-6deg); 511 -ms-transform: skew(-15deg) rotate(-6deg); 512 -o-transform: skew(-15deg) rotate(-6deg); 513 transform: skew(-15deg) rotate(-6deg); 514 } 515 ul.box li:after { 516 left: auto; 517 right: 10px; 518 -webkit-transform: skew(15deg) rotate(6deg); 519 -moz-transform: skew(15deg) rotate(6deg); 520 -ms-transform: skew(15deg) rotate(6deg); 521 -o-transform: skew(15deg) rotate(6deg); 522 transform: skew(15deg) rotate(6deg); 523 } 524 525 526 59、鲜艳的锚链接 527 528 a { 529 color: #00e;} 530 a:visited { 531 color: #551a8b;} 532 a:hover { 533 color: #06e;} 534 a:focus { 535 outline: thin dotted; 536 } 537 a:hover, a:active { 538 outline: 0; 539 } 540 a, a:visited, a:active { 541 text-decoration: none; 542 color: #fff; 543 -webkit-transition: all .3s ease-in-out; 544 } 545 a:hover, .glow { 546 color: #ff0; 547 text-shadow: 0 0 10px #ff0;} 548 549 550 60、带CSS3特色的横幅显示 551 552 .featureBanner { 553 position: relative; 554 margin: 20px 555 } 556 .featureBanner:before { 557 content: "Featured"; 558 position: absolute; 559 top: 5px; 560 left: -8px; 561 padding-right: 10px; 562 color: #232323; 563 font-weight: bold; 564 height: 0px; 565 border: 15px solid #ffa200; 566 border-right-color: transparent; 567 line-height: 0px; 568 box-shadow: -0px 5px 5px -5px #000; 569 z-index: 1; 570 } 571 .featureBanner:after { 572 content: ""; 573 position: absolute; 574 top: 35px; 575 left: -8px; 576 border: 4px solid #89540c; 577 border-left-color: transparent; 578 border-bottom-color: transparent; 579 }