分享30个开发人员有用的CSS代码片段

CSS 是我们使用的功能强大和最常用 web 语言。他可以兼容新旧不同的浏览器,他的兼容跨度确实够大的,从微软的IE到火狐再到谷歌浏览器。在设计的时候我们总是想创建更有创意和风格的网站,无论创建怎么样的网站,有些CSS代码是必须要掌握的,可以说这是核心,掌握这样的一些CSS代码,前端布局方面基本上再不成问题,什么边框圆角,工具提示,浏览器hacks等等都不是问题。

在今天的文章中分享我收藏的 30 个CSS 代码段,无论您是经验丰富的 web 设计师还是新手,他们都是非常值得借鉴的;

Css Reset by Eric Meyer

 
 
  1. html, body, div, span, applet, object, iframe,
  2. h1, h2, h3, h4, h5, h6, p, blockquote, pre,
  3. a, abbr, acronym, address, big, cite, code,
  4. del, dfn, em, font, img, ins, kbd, q, s, samp,
  5. small, strike, strong, sub, sup, tt, var,
  6. b, u, i, center,
  7. dl, dt, dd, ol, ul, li,
  8. fieldset, form, label, legend,
  9. table, caption, tbody, tfoot, thead, tr, th, td {
  10. margin: 0;
  11. padding: 0;
  12. border: 0;
  13. outline: 0;
  14. font-size: 100%;
  15. vertical-align: baseline;
  16. background: transparent;
  17. }
  18. body {
  19. line-height: 1;
  20. }
  21. ol, ul {
  22. list-style: none;
  23. }
  24. blockquote, q {
  25. quotes: none;
  26. }
  27. blockquote:before, blockquote:after,
  28. q:before, q:after {
  29. content: '';
  30. content: none;
  31. }
  32. /* remember to define focus styles! */
  33. :focus {
  34. outline: 0;
  35. }
  36. /* remember to highlight inserts somehow! */
  37. ins {
  38. text-decoration: none;
  39. }
  40. del {
  41. text-decoration: line-through;
  42. }
  43. /* tables still need 'cellspacing="0"' in the markup */
  44. table {
  45. border-collapse: collapse;
  46. border-spacing: 0;
  47. }

根据文件格式显示不同的链接样式

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

在IE浏览器删除textarea的滚动条

 
 
  1. textarea{
  2. overflow:auto;
  3. }

浏览器特定的 hacks

 
 
  1. /* IE 6 */
  2. * html .yourclass { }
  3. /* IE 7 */
  4. *+html .yourclass{ }
  5. /* IE 7 and modern browsers */
  6. html>body .yourclass { }
  7. /* Modern browsers (not IE 7) */
  8. html>/**/body .yourclass { }
  9. /* Opera 9.27 and below */
  10. html:first-child .yourclass { }
  11. /* Safari */
  12. html[xmlns*=""] body:last-child .yourclass { }
  13. /* Safari 3+, Chrome 1+, Opera 9+, Fx 3.5+ */
  14. body:nth-of-type(1) .yourclass { }
  15. /* Safari 3+, Chrome 1+, Opera 9+, Fx 3.5+ */
  16. body:first-of-type .yourclass { }
  17. /* Safari 3+, Chrome 1+ */
  18. @media screen and (-webkit-min-device-pixel-ratio:0) {
  19. .yourclass { }
  20. }

Clearfix

 
 
  1. .clearfix:after {
  2. visibility: hidden;
  3. display: block;
  4. font-size: 0;
  5. content: " ";
  6. clear: both;
  7. height: 0;
  8. }
  9. .clearfix { display: inline-block; }
  10. /* start commented backslash hack \*/
  11. * html .clearfix { height: 1%; }
  12. .clearfix { display: block; }
  13. /* close commented backslash hack */

固定宽度且居中

 
 
  1. .wrapper {
  2. width:960px;
  3. margin:0 auto;
  4. }

Rounded corners – border-radius

 
 
  1. .round{
  2. -moz-border-radius: 10px;
  3. -webkit-border-radius: 10px;
  4. border-radius: 10px; /* future proofing */
  5. -khtml-border-radius: 10px; /* for old Konqueror browsers */
  6. }

伪元素向文本的第一个字母添加特殊样式

 
 
  1. p:first-letter{
  2. display:block;
  3. margin:5px 0 0 5px;
  4. float:left;
  5. color:#000;
  6. font-size:60px;
  7. font-family:Georgia;
  8. }

使用 @fontface

 
 
  1. @font-face {
  2. font-family: 'MyFontFamily';
  3. src: url('myfont-webfont.eot?') format('eot'),
  4. url('myfont-webfont.woff') format('woff'),
  5. url('myfont-webfont.ttf') format('truetype'),
  6. url('myfont-webfont.svg#svgFontName') format('svg');
  7. }

跨浏览器的inline-block

 
 
  1. li {
  2. width: 200px;
  3. min-height: 250px;
  4. border: 1px solid #000;
  5. display: -moz-inline-stack;
  6. display: inline-block;
  7. vertical-align: top;
  8. margin: 5px;
  9. zoom: 1;
  10. *display: inline;
  11. _height: 250px;
  12. }

Fixed Footer

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

更改内容区域的大小

 
 
  1. #content {
  2. width: 100%;
  3. margin: 0;
  4. float: none;
  5. }

CSS3 Media Queries (这个不常用主要用于智能设备的判断显示不同的媒体)

 
 
  1. /* Smartphones (portrait and landscape) ----------- */
  2. @media only screen
  3. and (min-device-width : 320px)
  4. and (max-device-width : 480px) {
  5. /* Styles */
  6. }
  7. /* Smartphones (landscape) ----------- */
  8. @media only screen
  9. and (min-width : 321px) {
  10. /* Styles */
  11. }
  12. /* Smartphones (portrait) ----------- */
  13. @media only screen
  14. and (max-width : 320px) {
  15. /* Styles */
  16. }
  17. /* iPads (portrait and landscape) ----------- */
  18. @media only screen
  19. and (min-device-width : 768px)
  20. and (max-device-width : 1024px) {
  21. /* Styles */
  22. }
  23. /* iPads (landscape) ----------- */
  24. @media only screen
  25. and (min-device-width : 768px)
  26. and (max-device-width : 1024px)
  27. and (orientation : landscape) {
  28. /* Styles */
  29. }
  30. /* iPads (portrait) ----------- */
  31. @media only screen
  32. and (min-device-width : 768px)
  33. and (max-device-width : 1024px)
  34. and (orientation : portrait) {
  35. /* Styles */
  36. }
  37. /* Desktops and laptops ----------- */
  38. @media only screen
  39. and (min-width : 1224px) {
  40. /* Styles */
  41. }
  42. /* Large screens ----------- */
  43. @media only screen
  44. and (min-width : 1824px) {
  45. /* Styles */
  46. }
  47. /* iPhone 4 ----------- */
  48. @media
  49. only screen and (-webkit-min-device-pixel-ratio : 1.5),
  50. only screen and (min-device-pixel-ratio : 1.5) {
  51. /* Styles */
  52. }

多背景图片

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

多列

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

在IE的最小高度

 
 
  1. .box {
  2. min-height:500px;
  3. height:auto !important;
  4. height:500px;
  5. }

突出显示文本样式

 
 
  1. ::selection {
  2. color: #000000;
  3. background-color: #FF0000;
  4. }
  5. ::-moz-selection {
  6. color: #000000;
  7. background: #FF0000;
  8. }

Box Shadow

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

占位符文本样式

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

CSS3 3D文字效果

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

WebKit的边界半径修正

 
 
  1. -webkit-background-clip: padding-box;

XBrowser的border-radius(CSS3PIE)

 
 
  1. .roundbox {
  2. -moz-border-radius:8px;
  3. -webkit-border-radius:8px;
  4. -khtml-border-radius:8px;
  5. border-radius:8px;
  6. behavior: url(/PIE.htc);
  7. }

更好的兼容IE浏览器的

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

CSS3工具提示(这个很实用,看我网站的导航工具提示效果)

 
 
  1. a {
  2. color: #900;
  3. text-decoration: none;
  4. }
  5. a:hover {
  6. color: red;
  7. position: relative;
  8. }
  9. a[title]:hover:after {
  10. content: attr(title);
  11. padding: 4px 8px;
  12. color: #333;
  13. position: absolute;
  14. left: 0;
  15. top: 100%;
  16. white-space: nowrap;
  17. z-index: 20px;
  18. -moz-border-radius: 5px;
  19. -webkit-border-radius: 5px;
  20. border-radius: 5px;
  21. -moz-box-shadow: 0px 0px 4px #222;
  22. -webkit-box-shadow: 0px 0px 4px #222;
  23. box-shadow: 0px 0px 4px #222;
  24. background-image: -moz-linear-gradient(top, #eeeeee, #cccccc);
  25. background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #eeeeee),color-stop(1, #cccccc));
  26. }

CSS3背景大小

 
 
  1. body {
  2. background: #000 url(http://birdoflight.files.wordpress.com/2009/10/frida-kahlo-1.jpg) center center fixed no-repeat;
  3. -moz-background-size: cover;
  4. background-size: cover;
  5. }
  6. @media only all and (max-width: 1024px) and (max-height: 768px) {
  7. body {
  8. -moz-background-size: 1024px 768px;
  9. background-size: 1024px 768px;
  10. }
  11. }

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

 
 
  1. .page-curl {
  2. position: relative;
  3. background: #ffffff;
  4. background: -moz-linear-gradient(top, #ffffff 0%, #e5e5e5 100%);
  5. background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#e5e5e5));
  6. filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#e5e5e5',GradientType=0 );
  7. -webkit-box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.3);
  8. -moz-box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.3); }
  9. .page-curl:after {
  10. z-index: -1;
  11. position: absolute;
  12. background: transparent;
  13. width: 70%;
  14. height: 55%;
  15. content: '';
  16. right: 10px;
  17. bottom: 10px;
  18. -webkit-transform: skew(15deg) rotate(5deg);
  19. -webkit-box-shadow: 8px 12px 10px rgba(0, 0, 0, 0.3);
  20. -moz-transform: skew(15deg) rotate(5deg);
  21. -moz-box-shadow: 8px 12px 10px rgba(0, 0, 0, 0.3); }
  22. .page-curl:before {
  23. z-index: -2;
  24. position: absolute;
  25. background: transparent;
  26. width: 70%;
  27. height: 55%;
  28. content: '';
  29. left: 10px;
  30. bottom: 10px;
  31. -webkit-transform: skew(-15deg) rotate(-5deg);
  32. -webkit-box-shadow: -8px 12px 10px rgba(0, 0, 0, 0.3);
  33. -moz-transform: skew(-15deg) rotate(-5deg);
  34. -moz-box-shadow: -8px 12px 10px rgba(0, 0, 0, 0.3); }

CSS3模糊文字

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

修复 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 */

链接伪类的顺序

 
 
  1. a:link {color: blue;}
  2. a:visited {color: purple;}
  3. a:hover {color: red;}
  4. a:active {color: yellow;}

响应布局的HTML Meta标签

 
 
  1. <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  2. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  3. <meta name="HandheldFriendly" content="true">

原文链接:http://www.cnblogs.com/58top/archive/2012/11/26/30-useful-css-snippets-for-developers.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了python应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值