这几天明河出了趟远门,博客的更新落下了,这二天将多发几篇实用的文章\(^o^)/~

以下css代码片段都非常实用,是不可多得的css技巧。
利用text-indent隐藏文本
text-indent的主要作用在于文本缩进,但text-indent还有个妙用,先看代码:
- h1 {
- text-indent:-9999px;
- margin:0 auto;
- width:400px;
- height:100px;
- background:transparent url("images/logo.jpg") no-repeat scroll;
- }
text-indent:-9999px;即文本负缩进-9999像素(溢出容器),那么你在屏幕上就无法看到文本了,达到隐藏文本的作用。
那么其意义在哪呢?
主要是考虑SEO。举个典型例子,你的站点的logo部分是个图片,为了SEO,一般加title属性,但效果显然没有直接加文本来的好,而利用text-indent,你既加上了文本,同时不影响logo效果。
删除IE下文本域的滚动条
- textarea{
- overflow:auto;
- }
全兼容浏览器的透明度设置
- .transparent{
- filter:alpha(opacity=50);
- -moz-opacity:0.5;
- -khtml-opacity:0.5;
- opacity:0.5;
- }
非常实用,通用大部分浏览器。-moz-opacity的作用是为了支持一些老版本的Mozilla浏览器,-khtml-opacity的作用是支持一些老版本的Safari浏览器。
Eric Meyer写的经典重置样式
- html, body, div, span, applet, object, iframe,h1, h2, h3, h4, h5, h6, p, blockquote, pre,a, abbr, acronym,address, big, cite, code,del, dfn, em, font, img, ins, kbd, q, s, samp,small, strike, strong, sub, sup, tt, var,b, u, i, center,dl, dt, dd, ol, ul, li,fieldset, form, label, legend,table, caption, tbody, tfoot, thead, tr, th, td {
- margin: 0;
- padding: 0;
- border: 0;
- outline: 0;
- font-size: 100%;
- vertical-align: baseline;
- background: transparent;
- }
- body {
- line-height: 1;
- }
- ol, ul {
- list-style: none;
- }
- blockquote, q {
- quotes: none;
- }
- blockquote:before, blockquote:after,
- q:before, q:after {
- content: '';
- content: none;
- }
- /* remember to define focus styles! */
- :focus {
- outline: 0;
- }
- /* remember to highlight inserts somehow! */
- ins {
- text-decoration: none;
- }
- del {
- text-decoration: line-through;
- }
- /* tables still need 'cellspacing="0"' in the markup */
- table {
- border-collapse: collapse;
- border-spacing: 0;
- }
基础的css使用图片的按钮样式
- a {
- display: block;
- background: url(sprite.png) no-repeat;
- height: 30px;
- width: 250px;
- }
- a:hover {
- background-position: 0 -30px;
- }
谷歌的字体服务API
- <head>
- Inconsolata:italic|Droid+Sans">
- </head>
- body {
- font-family: 'Tangerine', 'Inconsolata', 'Droid Sans', serif; font-size: 48px;
- }
目前好像还没有中文的字体。
浏览器的特殊hacks
- /* IE 6 */
- * html .yourclass { }
- /* IE 7 */
- *+html .yourclass{ }
- /* IE 7 and modern browsers */
- html>body .yourclass { }
- /* Modern browsers (not IE 7) */
- html>/**/body .yourclass { }
- /* Opera 9.27 and below */
- html:first-child .yourclass { }
- /* Safari */
- html[xmlns*=""] body:last-child .yourclass { }
- /* Safari 3+, Chrome 1+, Opera 9+, Fx 3.5+ */
- body:nth-of-type(1) .yourclass { }
- /* Safari 3+, Chrome 1+, Opera 9+, Fx 3.5+ */
- body:first-of-type .yourclass { }
- /* Safari 3+, Chrome 1+ */
- @media screen and (-webkit-min-device-pixel-ratio:0) {
- .yourclass { }
- }
非常经典,明河严重推荐收藏。
固定定位,通用于IE6
- #footer {
- position:fixed;
- left:0px;
- bottom:0px;
- height:30px;
- width:100%;
- background:#999;
- }
- /* IE 6 */
- * html #footer {
- position:absolute;
- 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');
- }
IE 6是不支持position:fixed;的,这里使用绝对定位来模拟。代码中的expression是啥含义?请点此。
翻转图片
- img.flip {
- -moz-transform: scaleX(-1);
- -o-transform: scaleX(-1);
- -webkit-transform: scaleX(-1);
- transform: scaleX(-1);
- filter: FlipH;
- -ms-filter: "FlipH";
- }
清理浮动
- .clearfix:after {
- visibility: hidden;
- display: block;
- font-size: 0;
- content: " ";
- clear: both;
- height: 0;
- }
- .clearfix { display: inline-block; }
- /* start commented backslash hack */
- * html .clearfix { height: 1%; }
- .clearfix { display: block; }
- /* close commented backslash hack */
极度经典,明河严重推荐收藏。
圆角(不支持IE)
- .round{
- -moz-border-radius: 10px;
- -webkit-border-radius: 10px;
- border-radius: 10px; /* future proofing */
- -khtml-border-radius: 10px; /* for old Konqueror browsers */
- }
@font-face字体设置
- @font-face {
- font-family: 'Graublau Web';
- src: url('GraublauWeb.eot');
- src: local('☺'),
- url('GraublauWeb.woff') format('woff'), url('GraublauWeb.ttf') format('truetype');
- }
居中页面
- .wrapper {
- width:960px;
- margin:0 auto;
- }
IE中的最小高度
- .box {
- min-height:500px;
- height:auto !important;
- height:500px;
- }
正在读取图片的效果
- img {
- background: url(loader.gif) no−repeat 50% 50%;
- }
垂直居中
- .container {
- min-height: 10em;
- display: table-cell;
- vertical-align: middle;
- }
原文:http://www.1stwebdesigner.com/development/useful-css-snippets/

报告博主:右边栏“快速注册”的用户名和邮箱输入框太长了,跑出边栏框了。
我的上网环境:ubuntu10.04+firefox3.6.8
怎么没有about页面呢?留言栏好像也没找到。
留言栏和about页,我没加上去。我在firefox3.6下测试正常,只是是win下的,不知道跟环境有没有关系
哈哈!
先收藏..
快看四川省省运会直播了…
Prefect!果然实用!!!
还有一些css代码片段没发上去,日后补充上去。
最后一点垂直居中display: table-cell,css2还不支持,只有css3才支持。
另外,safari和chrome的hack,还是不理解啊 body:nth-of-type(1) body:first-of-type,怎么有2个,有什么区别呢?
求,详解,呵呵。。
呵呵css2已经存在display: table-cell,只是很少人用,没办法,IE6不支持,好像IE7也不支持。
:nth-of-type(X索引值)的作用为匹配同样式名下的同级第X个元素,而:first-of-type匹配同样式名下的同级第1个元素。二者同属于结构性伪类,而结构性伪类只在firefox3.5以上、safari、chrome(WebKit)、Opera上才支持。
很不错的一个收集。
很有用的一些css,不错。支持了。