1. 如何清除图片下方出现几像素的空白间隙?
1.img{display:block}
2.img{vertical-align:top} 除了 top 值, 还可以设置为 text-top | middle | bottom | text-bottom
3.#test {font-size:0;line-heigth:0} #test 为图片的父元素
2. 如何让文本垂直对齐文本输入框?
input{vertical-align:middle}
3. 如何让单行文本在容器内垂直居中?
test{height:25px;line-hieght:25px;} 只需设置文本的行高等于容器的高度即可
4. 如何让多行文本在容器内垂直居中?
实现的关键是把文字当图片处理. 用一个 span 标签将所有的文字封装起来, 设置文字与图片相同的 display 属性 (inline-block 属性), 然后用处理图片垂直居中的方式处理文字的垂直居中即可.
div{display:table-cell; width:550px; height:1.14em; padding:0 0.1em; border:4px solid #beceeb; color:#069; font-size:10em; vertical-align:middle;}
span{display:inline-block; font-size:0.1em; vertical-align:middle;}
注意点: 1. 外部 div 不能使用浮动
5. 如何让超链接访问后和访问前的颜色不同且访问后仍保留 hover 和 active 效果?
- a:link{color:#03c;}
- a:visited{color:#666;}
- a:hover{color:#f30;}
- a:active{color:#c30;}
按 L-V-H-A 的顺序设置超链接样式即可, 可速记为 LoVe(喜欢)HAte(讨厌)
6. 为什么 Standard mode 下 IE 无法设置滚动条的颜色以及如何在谷歌中设置滚动条的样式?
- html,body{
- 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 标签选择符上即可
相对于谷歌:
- ::-webkit-scrollbar {
- width: 6px;
- height: 6px;
- }
- / 滑动轨道 /
- ::-webkit-scrollbar-track {
- background: transparent;
- }
- / 滑块 /
- ::-webkit-scrollbar-thumb {
- background: #86d3f9;
- }
- ::-webkit-scrollbar-thumb:hover {
- background: #7cc2e5;
- }
- ::-webkit-scrollbar-thumb:active {
- background: #60a9d7;
- }
7. 如何使文本溢出边界不换行强制在一行内显示, 如何使连续的长字符串自动换行?
test{width:150px;white-space:nowrap;}
设置容器的宽度和 white-space 为 nowrap 即可, 其效果类似标签
#test{width:150px;word-wrap:break-word;}
word-wrap 的 break-word 值允许单词内换行
8. 如何使文本溢出边界显示为省略号?
test{width:150px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
首先需设置将文本强制在一行内显示, 然后将溢出的文本通过 overflow:hidden 截断, 并以 text-overflow:ellipsis 方式将截断的文本显示为省略号.
9. 如何清除浮动?
- test{clear:both;}
- #test 为浮动元素的下一个兄弟元素
- #test{display:block;zoom:1;overflow:hidden;}
- #test 为浮动元素的父元素. zoom:1 也可以替换为固定的 width 或 height
- #test{zoom:1;}
- #test:after{display:block;clear:both;visibility:hidden;height:0;content:'';}
- #test 为浮动元素的父元素
10. 如何定义鼠标指针的光标形状为手型并兼容所有浏览器?
test{cursor:pointer;}
若将 cursor 设置为 hand, 将只有 IE 和 Opera 支持, 且 hand 为非标准属性值素
11. 如何让已知高度的容器在页面中水平垂直居中?
- test{position:absolute;top:50%;left:50%;width:200px;height:200px;margin:-100px 0 0 -100px;}
- test{position:absolute;top:50%;left:50%;width:200px;height:200px;transform:translate(-50%,-50%);}
12. 如何让未知尺寸的图片在已知宽高的容器内水平垂直居中?
- test{display:table-cell;display:block;position:relative;width:200px;height:200px;text-align:center;vertical-align:middle;}
- #test p{*position:absolute;*top:50%;*left:50%;margin:0;}
- #test p img{*position:relative;*top:-50%;*left:-50%;vertical-align:middle;}
- #test 是 img 的祖父节点, p 是 img 的父节点.
13. 如何设置 span 的宽度和高度 (即如何设置内联元素的宽高)?
span{display:block;width:200px;height:100px;}
要使内联元素可以设置宽高, 只需将其定义为块级或者内联块级元素即可. 所以方法非常多样, 既可以设置 display 属性, 也可以设置 float 属性, 或者 position 属性等等.
14. 如何让某个元素充满整个页面?
- html,body{height:100%;margin:0;}
- test{height:100%;}
15. 如何让某个元素距离窗口上右下左 4 边各 10 像素?
- html,body{height:100%;margin:0;}
- html{_padding:10px;}
- test{position:absolute;top:10px;right:10px;bottom:10px;left:10px;_position:static;_height:100%;}
16. 如何去掉超链接的虚线框?
a{outline:none;}
IE7 及更早浏览器由于不支持 outline 属性, 需要通过 js 的 blur() 方法来实现, 如 < a onfocus="this.blur();"...
17. 如何容器透明, 内容不透明?
- .outer{width:200px;height:200px;background:#000;filter:alpha(opacity=20);opacity:.2;}
- .inner{width:200px;height:200px;margin-top:-200px;}
- <div class="outer"><!-- 我是透明的容器 --></div>
- <div class="inner"> 我是不透明的内容 </div>
原理是容器层与内容层并级, 容器层设置透明度, 内容层通过负 margin 或者 position 绝对定位等方式覆盖到容器层上 2. .outer{width:200px;height:200px;background:rgba(0,0,0,.2);background:#000\9;filter:alpha(opacity=20)\9;}
- .outer .inner{position:relative\9;}
- <div class="outer">
- <div class="inner"> 我是不透明的内容 </div>
- </div>
高级浏览器直接使用 rgba 颜色值实现; IE 浏览器在定义容器透明的同时, 让子节点相对定位, 也可达到效果
18. 如何让整个页面水平居中?
- body{text-align:center;}
- test2{width:960px;margin:0 auto;text-align:left;}
定义 body 的 text-align 值为 center 将使得 IE5.5 也能实现居中
19. 如何做 1 像素细边框的 table?
- test{border-collapse:collapse;border:1px solid #ddd;}
- #test th,#test td{border:1px solid #ddd;}
- <table id="test">
- <tr><th > 姓名 </th><td>Joy Du</td></tr>
- <tr><th > 年龄 </th><td>26</td></tr>
- </table>
- test{border-spacing:1px;background:#ddd;}
- #test tr{background:#fff;}
- <table id="test" cellspacing="1">
- <tr><th > 姓名 </th><td>Joy Du</td></tr>
- <tr><th > 年龄 </th><td>26</td></tr>
- </table>
IE7 及更早浏览器不支持 border-spacing 属性, 但是可以通过 table 的标签属性 cellspacing 来替代.
20. 如何使页面文本行距始终保持为 n 倍字体大小的基调?
body{line-height:n;}
21. 标准模式 Standard mode 和怪异模式 Quirks mode 下的盒模型区别?
标准模式下: Element width = width + padding + border
怪异模式下: Element width = width
22. 以图换字的几种方法及优劣分析
使用 text-indent 的负值, 将内容移出容器
- .test1{width:200px;height:50px;text-indent:-9999px;background:#eee url(*.png) no-repeat;}
- <div class="test"> 以图换字之内容负缩进法 </div>
该方法优点在于结构简洁, 不理想的地方:
1. 由于使用场景不同, 负缩进的值可能会不一样, 不易抽象成公用样式;
2. 当该元素为链接时, 在非 IE 下虚线框将变得不完整;
3. 如果该元素被定义为内联级或者内联块级, 不同浏览器下会有较多的差异
使用 display:none 或 visibility:hidden 将内容隐藏;
.test{width:200px;height:50px;background:#eee url(.png) no-repeat;}
.test span{visibility:hidden;/ 或者 display:none */}
<div class="test"><span > 以图换字之内容隐藏法 </span></div>
该方法优点在于兼容性强并且容易抽象成公用样式, 缺点在于结构较复杂
使用 padding 或者 line-height 将内容挤出容器之外;
- .test{overflow:hidden;width:200px;height:0;padding-top:50px;background:#eee url(.png) no-repeat;}
- .test{overflow:hidden;width:200px;height:50px;line-height:50;background:#eee url(.jpg) no-repeat;}
- <div class="test"> 以图换字之内容负缩进法 </div>
该方法优点在于结构简洁, 缺点在于:
1. 由于使用场景不同, padding 或 line-height 的值可能会不一样, 不易抽象成公用样式;
2. 要兼容 IE5.5 及更早浏览器还得 hack
使用超小字体和文本全透明法;
- .test{overflow:hidden;width:200px;height:50px;font-size:0;line-height:0;color:rgba(0,0,0,0);background:#eee url(*.png) no-repeat;}
- <div class="test"> 以图换字之内容负缩进法 </div>
该方法结构简单易用, 推荐使用
23. 为什么 2 个相邻 div 的 margin 只有 1 个生效?
- .box1{margin:10px 0;}
- .box2{margin:20px 0;}
- <div class="box1">box1</div>
- <div class="box2">box2</div>
本例中 box1 的底部 margin 为 10px,box2 的顶部 margin 为 20px, 但表现在页面上 2 者之间的间隔为 20px, 而不是预想中的 10+20px=30px, 结果是选择 2 者之间最大的那个 margin, 我们把这种机制称之为 "外边距合并";
外边距合并不仅仅出现在相邻的元素间, 父子间同样会出现.
简单列举几点注意事项:
外边距合并只出现在块级元素上;
浮动元素不会和相邻的元素产生外边距合并;
绝对定位元素不会和相邻的元素产生外边距合并;
内联块级元素间不会产生外边距合并;
根元素间不会不会产生外边距合并 (如 html 与 body 间);
设置了属性 overflow 且值不为 visible 的块级元素不会与它的子元素发生外边距合并;
24. 如何在文本框中禁用中文输入法?
input,textarea{ime-mode:disabled;}
ime-mode 为非标准属性, 写该文档时只有 IE 和 Firefox 支持
25. 如何解决列表中 list-style-image 不能精准定位的问题?
不使用 list-style-image 来定义列表项目标记符号, 而用 background-image 来代替, 并通过 background-position 来进行定位
26. 如何解决伪对象: before 和: after 在 input 标签上的怪异表现的问题?
在编写本条目时, 除了 Opera, 在所有浏览器下 input 标签使用伪对象: before 和: after 都没有效果, 即使 Opera 的表现也同样令人诧异
27. 如何解决伪对象: before 和: after 无法在 Chrome,Safari,Opera 上定义过渡和动画的问题?
除了 Firefox, 在所有浏览器下伪对象: before 和: after 无法定义过渡和动画效果
28. 如何让层在 falsh 上显示?
<param name="wmode" value="transparent" />
设置 flash 的 wmode 值为 transparent 或 opaque
29. 如何使用标准的方法在页面上插入 flash?
- <object id="flash-show" type="application/x-shockwave-flash" data=".swf">
- <param name="movie" value=".swf" />
- <param name="wmode" value="transparent" />
- <img src="*.jpg" alt="用于不支持 flash 或屏蔽 flash 时显示" />
- </object>
至于 flash 的宽高可以在 CSS 里设置
30. 如何在点文字时也选中复选框或单选框?
- <input type="checkbox" id="chk1" name="chk" /><label for="chk1"> 选项一 </label>
- <input type="checkbox" id="chk2" name="chk" /><label for="chk2"> 选项二 </label>
- <input type="radio" id="rad1" name="rad" /><label for="rad1"> 选项一 </label>
- <input type="radio" id="rad2" name="rad" /><label for="rad2"> 选项二 </label>
该方式所有主流浏览器都支持
- <label><input type="checkbox" name="chk" /> 选项一 </label>
- <label><input type="checkbox" name="chk" /> 选项二 </label>
- <label><input type="radio" name="rad" /> 选项一 </label>
- <label><input type="radio" name="rad" /> 选项二 </label>
该方式相比方法 1 更简洁, 但 IE6 及更早浏览器不支持
31. IE 下如何对 Standard Mode 与 Quirks Mode 进行切换?
IE6 的触发 (在 DTD 申明前加上 XML 申明):
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE html>
IE5.5 及更早浏览器版本直接以 Quirks Mode 解析.
所有 IE 版本的触发 (在 DTD 申明前加上 HTML 注释):
- <!-- Let IE into quirks mode -->
- <!DOCTYPE html > 当没有 DTD 声明时, 所有 IE 版本也会进入 Quirks Mode.
32. 如何区别 display:none 与 visibility:hidden?
相同的是 display:none 与 visibility:hidden 都可以用来隐藏某个元素;
不同的是 display:none 在隐藏元素的时候, 将其占位空间也去掉; 而 visibility:hidden 只是隐藏了内容而已, 其占位空间仍然保留
33. 如何设置 IE 下的 iframe 背景透明?
设置 iframe 元素的标签属性 allowtransparency="allowtransparency" 然后设置 iframe 内部页面的 body 背景色为 transparent.
不过由此会引发 IE 下一些其它问题, 如: 设置透明后的 iframe 将不能遮住 select
来源: http://www.qdfuns.com/article/18271/18a1206fbcc0e9064fb0f763b555b7c1.html