CSS技巧和经验

十度 HTML 2016年03月01日 收藏

CSS技巧和经验列表(以下实例默认运行环境都为Standard mode)

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

方法1

  1. img {
  2.     display:block;
  3. }

方法2

  1. img {
  2.     vertical-align:top;
  3. }
  4. // 除了top值,还可以设置为text-top | middle | bottom | text-bottom,甚至特定的<length>和<percentage>值都可以

方法3

  1. // #test为img的父元素
  2. #test {
  3.     font-size:0;
  4.     line-height:0;
  5. }

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

  1. input {
  2.     vertical-align:middle;
  3. }

3. 如何让单行文本在容器内垂直居中

  1. #test {
  2.     height:25px;
  3.     line-height:25px;
  4. }
  5. // 只需设置文本的行高等于容器的高度即可

4. 如何让超链接访问后和访问前的颜色不同且访问后仍保留hover和active效果

  1. a:link {
  2.     color:#03c;
  3. }
  4. a:visited {
  5.     color:#666;
  6. }
  7. a:hover {
  8.     color:#f30;
  9. }
  10. a:active {
  11.     color:#c30;
  12. }
  13. // 按L-V-H-A的顺序设置超链接样式即可,可速记为LoVe(喜欢)HAte(讨厌)

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

  1. html {
  2.     scrollbar-3dlight-color:#999;
  3.     scrollbar-darkshadow-color:#999;
  4.     scrollbar-highlight-color:#fff;
  5.     scrollbar-shadow-color:#eee;
  6.     scrollbar-arrow-color:#000;
  7.     scrollbar-face-color:#ddd;
  8.     scrollbar-track-color:#eee;
  9.     scrollbar-base-color:#ddd;
  10. }
  11. // 将原来设置在body上的滚动条颜色样式定义到html标签选择符上即可

6. 如何使文本溢出边界不换行强制在一行内显示

  1. #test {
  2.     width:150px;
  3.     white-space:nowrap;
  4. }
  5. // 设置容器的宽度和white-space为nowrap即可,其效果类似<nobr>标签

7. 如何使文本溢出边界显示为省略号

  1. // 此方法Firefox5.0尚不支持
  2. #test {
  3.     width:150px;
  4.     white-space:nowrap;
  5.     overflow:hidden;
  6.     text-overflow:ellipsis;
  7. }
  8. // 首先需设置将文本强制在一行内显示,然后将溢出的文本通过overflow:hidden截断,并以text-overflow:ellipsis方式将截断的文本显示为省略号。

8. 如何使连续的长字符串自动换行

  1. #test {
  2.     width:150px;
  3.     word-wrap:break-word;
  4. }
  5. // word-wrap的break-word值允许单词内换行

9. 如何清除浮动

方法1

  1. #test {
  2.     clear:both;
  3. }
  4. // #test为浮动元素的下一个兄弟元素

方法2

  1. #test {
  2.     display:block;
  3.     zoom:1;
  4.     overflow:hidden;
  5. }
  6. // #test为浮动元素的父元素。zoom:1也可以替换为固定的width或height

方法3

  1. #test {
  2.     zoom:1;
  3. }
  4. #test:after {
  5.     display:block;
  6.     clear:both;
  7.     visibility:hidden;
  8.     height:0;
  9.     content:'';
  10. }
  11. // #test为浮动元素的父元素

10. 如何定义鼠标指针的光标形状为手型并兼容所有浏览器

  1. #test {
  2.     cursor:pointer;
  3. }
  4. // 若将cursor设置为hand,将只有IE和Opera支持,且hand为非标准属性值

11. 如何让已知高度的容器在页面中水平垂直居中

  1. #test {
  2.     position:absolute;
  3.     top:50%;
  4.     left:50%;
  5.     width:200px;
  6.     height:200px;
  7.     margin:-100px 0 0 -100px;
  8. }

12. 如何让未知尺寸的图片在已知宽高的容器内水平垂直居中

  1. #test {
  2.     display:table-cell;
  3.     width:200px;
  4.     height:200px;
  5.     text-align:center;
  6.     vertical-align:middle;
  7. }
  8. #test p {
  9.     margin:0;
  10. }
  11. #test p img {
  12.     vertical-align:middle;
  13. }
  14. // #test是img的祖父节点,p是img的父节点

13. 如何设置span的宽度和高度(即如何设置内联元素的宽高)

  1. span {
  2.     display:block;
  3.     width:200px;
  4.     height:100px;
  5. }
  6. // 要使内联元素可以设置宽高,只需将其定义为块级或者内联块级元素即可。所以方法非常多样,既可以设置display属性,也可以设置float属性,或者position属性等等。

14. 如何让某个元素充满整个页面

  1. html,body {
  2.     height:100%;
  3.     margin:0;
  4. }
  5. #test {
  6.     height:100%;
  7. }

15. 如何让某个元素距离窗口上右下左4边各10像素

  1. html,body {
  2.     height:100%;
  3.     margin:0;
  4. }
  5. #test {
  6.     position:absolute;
  7.     top:10px;
  8.     right:10px;
  9.     bottom:10px;
  10.     left:10px;
  11. }

16. 如何去掉超链接的虚线框

  1. {
  2.     outline:none;
  3. }

17. 如何容器透明,内容不透明

方法1

  1. .outer {
  2.     width:200px;
  3.     height:200px;
  4.     background:#000;
  5.     filter:alpha(opacity=20);
  6.     opacity:0.2;
  7. }
  8. .inner {
  9.     width:200px;
  10.     height:200px;
  11.     margin-top:-200px;
  12. }
  13.  
  14. <div class="outer"><!--我是透明的容器--></div>
  15. <div class="inner">我是不透明的内容</div>
  16. // 原理是容器层与内容层并级,容器层设置透明度,内容层通过负margin或者position绝对定位等方式覆盖到容器层上

方法2

  1. .outer {
  2.     width:200px;
  3.     height:200px;
  4.     background:rgba(0,0,0,0.2);
  5.     filter:alpha(opacity=20)\9; /* IE */
  6. }
  7. .outer .inner {
  8.     position:relative\9; /* IE */
  9. }
  10.  
  11. <div class="outer">
  12.     <div class="inner">我是不透明的内容</div>
  13. </div>
  14. // 高级浏览器直接使用rgba颜色值实现;IE浏览器在定义容器透明的同时,让子节点相对定位,也可达到效果

18. 如何让整个页面水平居中

  1. body {
  2.     text-align:center;
  3. }
  4. #test2 {
  5.     width:960px;
  6.     margin:0 auto;
  7.     text-align:left;
  8. }
  9. // 定义body的text-align值为center将使得IE5.5也能实现居中

19. 为什么容器的背景色没显示出来?为什么容器无法自适应内容高度?

  1. // 清除浮动,方法请参考本页第9条
  2. // 通常出现这样的情况都是由于没有清除浮动而引起的,所以Debug时应第一时间想到是否有未清除浮动的地方

20. 如何做1像素细边框的table

方法1

  1. #test {
  2.     border-collapse:collapse;
  3.     border:1px solid #ddd;
  4. }
  5. #test th, #test td {
  6.     border:1px solid #ddd;
  7. }
  8.  
  9. <table id="test">
  10.     <tr>
  11.         <th>姓名</th>
  12.         <td>Joy Du</td>
  13.     </tr>
  14.     <tr>
  15.         <th>年龄</th>
  16.         <td>26</td>
  17.     </tr>
  18. </table>

方法2

  1. #test {
  2.     border-spacing:1px;
  3.     background:#ddd;
  4. }
  5. #test tr {
  6.     background:#fff;
  7. }
  8.  
  9. <table id="test" cellspacing="1">
  10.     <tr>
  11.         <th>姓名</th>
  12.         <td>Joy Du</td>
  13.     </tr>
  14.     <tr>
  15.         <th>年龄</th>
  16.         <td>26</td>
  17.     </tr>
  18. </table>
  19. // IE7及更早浏览器不支持border-spacing属性,但是可以通过table的标签属性cellspacing来替代。

21. 如何使页面文本行距始终保持为n倍字体大小的基调

  1. body {
  2.     line-height:n;
  3. }
  4. // 注意,不要给n加单位

22. 标准模式Standard mode和怪异模式Quirks mode下的盒模型区别

相关资料请参阅CSS3属性box-sizing

  1. // 标准模式下:Element width = width + padding + border
  2. // 怪异模式下:Element width = width

23. 以图换字的几种方法及优劣分析

思路1 使用text-indent的负值,将内容移出容器

  1. .test1 {
  2.     width:200px;
  3.     height:50px;
  4.     text-indent:-9999px;
  5.     background:#eee url(*.png) no-repeat;
  6. }
  7.  
  8. <div class="test">以图换字之内容负缩进法</div>
  9. // 该方法优点在于结构简洁,不理想的地方:1.由于使用场景不同,负缩进的值可能会不一样,不易抽象成公用样式;2.当该元素为链接时,在非IE下虚线框将变得不完整;3.如果该元素被定义为内联级或者内联块级,不同浏览器下会有较多的差异

思路2 使用display:none或visibility:hidden将内容隐藏

  1. .test {
  2.     width:200px;
  3.     height:50px;
  4.     background:#eee url(*.png) no-repeat;
  5. }
  6. .test span {
  7.     visibility:hidden; /* 或者display:none */
  8. }
  9.  
  10. <div class="test">
  11.     <span>以图换字之内容隐藏法</span>
  12. </div>
  13. // 该方法优点在于兼容性强并且容易抽象成公用样式,缺点在于结构较复杂

思路3 使用padding或者line-height将内容挤出容器之外

  1. .test {
  2.     overflow:hidden;
  3.     width:200px;
  4.     height:0;
  5.     padding-top:50px;
  6.     background:#eee url(*.png) no-repeat;
  7. }
  8. .test {
  9.     overflow:hidden;
  10.     width:200px;
  11.     height:50px;
  12.     line-height:50;
  13.     background:#eee url(*.jpg) no-repeat;
  14. }
  15.  
  16. <div class="test">以图换字之内容排挤法</div>
  17. //该方法优点在于结构简洁,缺点在于:1.由于使用场景不同,padding或line-height的值可能会不一样,不易抽象成公用样式;2.要兼容IE5.5及更早浏览器还得hack

思路4 使用超小字体和文本全透明法

  1. .test {
  2.     overflow:hidden;
  3.     width:200px;
  4.     height:50px;
  5.     font-size:0;
  6.     line-height:0;
  7.     color:rgba(0,0,0,0);
  8.     background:#eee url(*.png) no-repeat;
  9. }
  10.  
  11. <div class="test">以图换字之超小字体+文本全透明法</div>
  12. // 该方法结构简单易用,推荐使用

24. 为什么2个相邻div的margin只有1个生效

  1. .box1 {
  2.     margin:10px 0;
  3. }
  4. .box2 {
  5.     margin:20px 0;
  6. }
  7.  
  8. <div class="box1">box1</div>
  9. <div class="box2">box2</div>
  10. // 本例中box1的底部margin为10px,box2的顶部margin为20px,但表现在页面上2者之间的间隔为20px,而不是预想中的10+20px=30px,结果是选择2者之间最大的那个margin,我们把这种机制称之为“外边距合并”;外边距合并不仅仅出现在相邻的元素间,父子间同样会出现
  11. // 简单列举几点注意事项: 
  12. // a. 外边距合并只出现在块级元素上; 
  13. // b. 浮动元素不会和相邻的元素产生外边距合并; 
  14. // c. 绝对定位元素不会和相邻的元素产生外边距合并; 
  15. // d. 内联块级元素间不会产生外边距合并; 
  16. // e. 根元素间不会产生外边距合并(如html与body间); 
  17. // f. 设置了属性overflow且值不为visible的块级元素不会与它的子元素发生外边距合并;

25. 如何在文本框中禁用中文输入法

  1. input, textarea {
  2.     ime-mode:disabled;
  3. }
  4. // ime-mode为非标准属性,写该文档时只有IE和Firefox支持

26. 如何解决列表中list-style-image不能精准定位的问题

  1. 不使用list-style-image来定义列表项目标记符号,而用background-image来代替,并通过background-position来进行定位