29个常用的CSS小技巧汇总

来源:互联网 发布:北京科瑞明软件 编辑:程序博客网 时间:2024/06/06 18:49

1.清除图片下方出现几像素的空白间隙

方法1:

img{display:block;}

方法2:

img{vertical-align:top;}

方法3:

test{font-size:0;line-height:0;}
test为img的父元素

2.让文本垂直对齐文本输入框

方法:

input{vertical-align:middle;}

3.让单行文本在容器内垂直居中
方法:

test{height:25px;line-height:25px;}

只需设置文本的行高等于容器的高度即可

4.让超链接访问后和访问前的颜色不同且访问后仍保留hover和active效果
方法:
a:link{color:#03c;}
a:visited{color:#666;}
a:hover{color:#f30;}
a:active{color:#c30;}
按L-V-H-A的顺序设置超链接样式即可,可速记为LoVe(喜欢)HAte(讨厌)

5.为什么Standard mode下IE无法设置滚动条的颜色?
方法:

html{
scrollbar-3dlight-color:#999;
scrollbar-darkshadow-color:#999;
scrollbar-highlight-color:#fff;
scrollbar-shadow-color:#eee;
scrollbar-arrow-color:#000;
scrollbar-face-color:#ddd;
scrollbar-track-color:#eee;
scrollbar-base-color:#ddd;
}

将原来设置在body上的滚动条颜色样式定义到html标签选择符上即可

6.使文本溢出边界不换行强制在一行内显示
方法:

test{width:150px;white-space:nowrap;}

设置容器的宽度和white-space为nowrap即可,其效果类似<nobr>标签

7.使文本溢出边界显示为省略号
方法(此方法Firefox5.0尚不支持):

test{width:150px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}

首先需设置将文本强制在一行内显示,然后将溢出的文本通过overflow:hidden截断,并以text-overflow:ellipsis方式将截断的文本显示为省略号。

8.使连续的长字符串自动换行
方法:

test{width:150px;word-wrap:break-word;}

word-wrap的break-word值允许单词内换行

9.清除浮动
方法1:

test{clear:both;}
test为浮动元素的下一个兄弟元素

方法2:

(<a href="http://www.dztcsd.com/">资质代办</a>)
test{display:block;zoom:1;overflow:hidden;}
test为浮动元素的父元素。zoom:1也可以替换为固定的width或height

方法3:

test{zoom:1;}
test:after{display:block;clear:both;visibility:hidden;height:0;content:'';}
test为浮动元素的父元素

10.定义鼠标指针的光标形状为手型并兼容所有浏览器
方法:(<a href="http://www.dztcsd.com/">资质代办</a>)

test{cursor:pointer;}

若将cursor设置为hand,将只有IE和Opera支持,且hand为非标准属性值


作者: 秀秀的大宝贝 
来源:慕课网
0 0