如何清除图片下方出现几像素的空白间隙?
方法1:
img{display:block;}
方法2:
img{vertical-align:top;}
除了top值,还可以设置为text-top | middle | bottom | text-bottom
甚至特定的<length>和<percentage>值都可以
方法3:
#test{font-size:0;line-height:0;}
#test为img的父元素
复制代码
如何让文本垂直对齐文本输入框?
input{vertical-align:middle;}
复制代码
如何让单行文本在容器内垂直居中?
#test{height:25px;line-height:25px;}
复制代码
如何让超链接访问后和访问前的颜色不同且访问后仍保留hover和active效果?
a:link{color:#03c;}
a:visited{color:#666;}
a:hover{color:#f30;}
a:active{color:#c30;}
按L-V-H-A的顺序设置超链接样式即可,可速记为LoVe(喜欢)HAte(讨厌)
复制代码
为什么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标签选择符上即可
复制代码
如何使文本溢出边界不换行强制在一行内显示?
#test{width:150px;white-space:nowrap;}
设置容器的宽度和white-space为nowrap即可,其效果类似<nobr>标签
复制代码
如何使文本溢出边界显示为省略号?
方法(此方法Firefox5.0尚不支持):
#test{
width:150px;
white-space:nowrap;
overflow:hidden;
text-overflow:ellipsis;
}
复制代码
如何使连续的长字符串自动换行?
#test{width:150px;word-wrap:break-word;}
word-wrap的break-word值允许单词内换行
复制代码
如何清除浮动?
方法1:
#test{clear:both;}
#test为浮动元素的下一个兄弟元素
方法2:
#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为浮动元素的父元素
复制代码
如何定义鼠标指针的光标形状为手型并兼容所有浏览器?
#test{cursor:pointer;}
若将cursor设置为hand,将只有IE和Opera支持,且hand为非标准属性值
复制代码
如何让已知高度的容器在页面中水平垂直居中?
#test{
position:absolute;
top:50%;left:50%;
width:200px;
height:200px;
}
Know More:已知高度的容器如何在页面中水平垂直居中
复制代码
如何让未知尺寸的图片在已知宽高的容器内水平垂直居中?
#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的父节点
复制代码
如何设置span的宽度和高度(即如何设置内联元素的宽高)?
span{display:block;width:200px;height:100px;}
复制代码
如何给一个元素定义多个不同的css规则?
.a{color:#f00;}
.b{background:#eee;}
.c{background:#ccc;}
<div class="a b">测试1</div>
<div class="a c">测试2</div>
复制代码
如何让某个元素充满整个页面?
html,body{height:100%;margin:0;}
#test{height:100%;}
复制代码
如何让某个元素距离窗口上右下左4边各10像素?
html,body{height:100%;margin:0;}
#test{position:absolute;top:10px;right:10px;bottom:10px;left:10px;}
复制代码
如何去掉超链接的虚线框?
a{outline:none;}
IE7及更早浏览器由于不支持outline属性,需要通过js的blur()方法来实现,如<a onfocus="this.blur();"...
复制代码
如何容器透明,内容不透明?
.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浏览器在定义容器透明的同时,让子节点相对定位,也可达到效果
复制代码
如何让整个页面水平居中?
body{text-align:center;}
#test2{width:960px;margin:0 auto;text-align:left;}
定义body的text-align值为center将使得IE5.5也能实现居中
复制代码
为什么容器的背景色没显示出来?为什么容器无法自适应内容高度?
通常出现这样的情况都是由于没有清除浮动而引起的
复制代码
如何做1像素细边框的table?
方法1:
#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>
方法2:
#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来替代。
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END