设为首页收藏本站关注微信<<< 抵制 IE6 人人有责 >>>
搜索
热搜: 活动 交友 discuz
查看: 2048|回复: 1
打印 上一主题 下一主题

[WEB前端] css常用代码大全

[复制链接]
跳转到指定楼层
楼主
发表于 2015-9-5 08:20:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
包括CSS3代码,有一些css效果很顽固,经常会一时找不出解决方案,网络上也有很多的工具和高手提供了具体的浏览器兼容代码,这个页面我今后会经常整理,希望能够帮到重构和正在找这些效果的代码,如果你有更好的实现方案,不妨告诉我,或者你有解决不了的,也不妨告诉我。你想找浏览器兼容写法吗?点CSS浏览器兼容与hack代码(经验汇总,保持最新)

  • Css背景颜色透明(#ddd)


  1. .liter{
  2. filter:progid:DXImageTransform.Microsoft.gradient(enabled='true',startColorstr='#4CDDDDDD', endColorstr='#4CDDDDDD');
  3. }
  4. :root .liter {
  5. filter:none;     /*处理IE9浏览器中的滤镜效果*/
  6. background-color:rgba(221,221,221,0.3);
  7. }
复制代码

  • 图片垂直居中对齐


第一种:table-cell法
html:
  1. <div class="test_box">
  2.         <img src="book.jpg" alt="css常用代码大全" alt="" />
  3. </div>
复制代码

CSS:
  1. .test_box {width:200px;height:200px;overflow:hidden;text-align:center;font-size:0;border:1px solid #000000;}
  2. .test_box .hook {display:inline-block;width:0;height:100%;overflow:hidden;margin-left:-1px;font-size:0;line-height:0;vertical-align:middle;}
  3. .test_box img {vertical-align:middle;border:0 none;}
复制代码

  • css border制作小三角(兼容IE6)


  1. .triangle {display:inline-block;width:0;height:0;overflow:hidden;line-height:0;font-size:0;
  2. vertical-align:middle;
  3. border-right:7px solid #000fff;
  4. border-left:0 none;
  5. border-top:7px solid transparent;
  6. border-bottom:7px solid transparent;
  7. _color:#FF3FFF;
  8. _filter:chroma(color=#FF3FFF);
  9. }
复制代码

  • CSS固定在底部


CSS代码

  1. /*
  2. Sticky Footer Solution
  3. by Steve Hatcher
  4. http://stever.ca
  5. http://www.cssstickyfooter.com
  6. */
  7. * {margin:0;padding:0;}
  8. /* must declare 0 margins on everything, also for main layout components use padding, not
  9. vertical margins (top and bottom) to add spacing, else those margins get added to total height
  10. and your footer gets pushed down a bit more, creating vertical scroll bars in the browser */
  11. html, body {height: 100%;}
  12. #wrap {min-height: 100%;}
  13. #main {
  14.     overflow:auto;
  15.     padding-bottom: 150px;  /* must be same height as the footer */
  16. }
  17. #footer {
  18.     position: relative;
  19.     margin-top: -150px; /* negative value of footer height */
  20.     height: 150px;
  21.     clear:both;
  22. }
  23. /*Opera Fix*/
  24. body:before {/* thanks to Maleika (Kohoutec)*/
  25.     content:"";
  26.     height:100%;
  27.     float:left;
  28.     width:0;
  29.     margin-top:-32767px;/* thank you Erik J - negate effect of float*/
  30. }
  31. /* IMPORTANT
  32. You also need to include this conditional style in the  of your HTML file to feed this style to IE 6 and lower and 8 and higher.
  33. <!--[if !IE 7]>
  34. <style type="text/css">
  35.     #wrap {display:table;height:100%}
  36. </style>
  37. < ![endif]-->
  38. */
复制代码

html代码
  1. <div id="wrap">
  2.         <div id="main">
  3.         </div>
  4. </div>
  5. <div id="footer">
  6. </div>
复制代码

纯粹的css固定在底部
  1. #footer {
  2.    position:fixed;
  3.    left:0px;
  4.    bottom:0px;
  5.    height:32px;
  6.    width:100%;
  7.    background:#333;
  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. }
复制代码

  • 用CSS包裹内容很长的URL和文本


这个代码片段通过保证文本的包裹元素宽度适应内容的宽度,能够避免很长的文本超出内容区域。
  1. pre {
  2.     white-space: pre;           /* CSS 2.0 */
  3.     white-space: pre-wrap;      /* CSS 2.1 */
  4.     white-space: pre-line;      /* CSS 3.0 */
  5.     white-space: -pre-wrap;     /* Opera 4-6 */
  6.     white-space: -o-pre-wrap;   /* Opera 7 */
  7.     white-space: -moz-pre-wrap; /* Mozilla */
  8.     white-space: -hp-pre-wrap;  /* HP Printers */
  9.     word-wrap: break-word;      /* IE 5+ */
  10. }
复制代码

  • 用css3创造3D文字


text-shadow属性能帮助你只用CSS创造3D文字。

  1. p.threeD{
  2.     text-shadow:
  3.         -1px 1px 0 #ddd,
  4.         -2px 2px 0 #c8c8c8,
  5.         -3px 3px 0 #ccc,
  6.         -4px 4px 0 #b8b8b8,
  7.         -4px 4px 0 #bbb,
  8.         0px 1px 1px rgba(0,0,0,.4),
  9.         0px 2px 2px rgba(0,0,0,.3),
  10.         -1px 3px 3px rgba(0,0,0,.2),
  11.         -1px 5px 5px rgba(0,0,0,.1),
  12.         -2px 8px 8px rgba(0,0,0,.1),
  13.         -2px 13px 13px rgba(0,0,0,.1)
  14.         ;
  15. }
复制代码

  • CSS透明度


  1. div{
  2.     opacity: .75; /* Standard: FF gt 1.5, Opera, Safari */
  3.     filter: alpha(opacity=75); /* IE lt 8 */
  4.     -ms-filter: "alpha(opacity=75)"; /* IE 8 */
  5.     -khtml-opacity: .75; /* Safari 1.x */
  6.     -moz-opacity: .75; /* FF lt 1.5, Netscape */
  7. }
复制代码

  • 改变博客中默认选中文本的颜色


  1. ::selection {
  2.     background: #ffb7b7; /* Safari */
  3.     color: #ffffff;
  4. }
  5. ::-moz-selection {
  6.     background: #ffb7b7; /* Firefox */
  7.     color: #ffffff;
  8. }
复制代码

  • 多重背景图片

  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. }
复制代码

  • 多栏CSS3


使用css3来创建多栏,它可以自适应网页,不兼容IE
  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. }
复制代码

  • 文本溢出省略


  1. .textoverflow a {
  2.     display:block;
  3.     width:120px;
  4.     margin: 0px 0px 0px 3px;
  5.     white-space: nowrap;
  6.     overflow: hidden;
  7.     float: left;
  8.     -o-text-overflow: ellipsis;     /* for Opera */
  9.     text-overflow: ellipsis;        /* for IE */
  10. }
  11. .textoverflow:after{ content: "..."; }/* for Firefox */
  12. @media all and (min-width: 0px){ .textoverflow:after{ content:""; }/* for Opera */ }
复制代码

让IE9以下的版本支持HTML5

在项目中加入以下JS代码

  1. // html5 shiv

  2. if (!+[1,]) {
  3.     (function() {
  4.     var tags = [
  5.         'article', 'aside', 'details', 'figcaption',
  6.         'figure', 'footer', 'header', 'hgroup',
  7.         'menu', 'nav', 'section', 'summary',
  8.         'time', 'mark', 'audio', 'video'],
  9.         i = 0, len = tags.length;
  10.     for (; i < len; i++) document.createElement(tags[i]);
  11.     })();
  12. }
复制代码
or
  1. <!--[if lt IE 9]>
  2. <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
复制代码




PNG32透明(IE6)
主要用来兼容IE6,不建议使用,由于这个css代码比较耗内存。
  1. .some_element {
  2.     background: url(image.png);
  3.     _background: none;
  4.     _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='image.png', sizingMethod='crop');
  5. }
复制代码


本帖被以下淘专辑推荐:

  • · 常用|主题: 22, 订阅: 0
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享淘帖1
回复

使用道具 举报

沙发
 楼主| 发表于 2015-9-5 08:28:31 | 只看该作者
文字链接平滑左右移动的效果代码

  1. <style>
  2. .test a{background-color:#446CB3; color:#ffffff; padding:10px; text-decoration:none;
  3. -webkit-transition: margin 0.2s ease-out;
  4. -moz-transition: margin 0.2s ease-out;
  5. -khtml-transition: margin 0.2s ease-out; }
  6. a:hover{margin-left:10px;}
  7. </style>
  8. <div class="test"><a href="#1">Test 测试</a></div>
复制代码

当然,要知道css3动画不兼容ie10以下浏览器


SO,就有了下面的jquery代码:
  1. $(function(){
  2. $(“a”).hover(function(){
  3. $(this).stop().animate({“margin-left”:”10px”,”text-shadow”:”2px 2px 10px #333333″},”fast”);
  4. },function(){
  5. $(this).stop().animate({“margin-left”:”0px”},”fast”);
  6. });
  7. });
复制代码

大家可能很疑惑,为什么css里面由一个position:relative呢?其实这是我的另外一种写法,
jquery代码如下:
  1. $(function(){
  2. $(“a”).hover(function(){
  3. $(this).stop().animate({“left”:”10px”},300);
  4. },function(){
  5. $(this).stop().animate({“left”:”0px”},300);
  6. });
  7. });
复制代码



回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

QQ|手机版|Archiver|源于生活(个人生活娱乐工作的笔记)css3,html5,学习笔记    

GMT+8, 2024-4-20 21:36 , Processed in 0.093601 second(s), 30 queries .

Powered by Mr.Kevin-ying

© 2004-2015

快速回复 返回顶部 返回列表