以下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;
- }
本文来自: 实用的css代码片段集合








