CSS的inherit与auto
2011-03-02 17:22
435 查看
转自:/article/6948377.html
一个很显浅的寓言,千年老树,电打雷劈,屹立不倒,却毁于蝼蚁的侵袭之下。自以为精通CSS的人,常常被一些小问题搞到头晕脑胀。通常是一个很小的数值,经过层层放大歪曲后,整个布局就走形了。CSS是一门很简单的语言,易学易用,但也最容易出垃圾代码。这是没有深入研究这门语言所致。本人认为,CSS是由以下三大块构成的:默认值,继承系统与加权系统。默认值,也就是浏览器在用户没有设置属性的情况下,默认指定的属性。CSS框架基本都有一个叫reset.css 的文件,就是对其进行重设,消除各浏览器的差异的。继承系统就是下面要重点讨论的东西。加权系统,也就是优先级的问题,不在本文的讨论范畴,不说了。
在CSS中,许多属性都是可以继承的,如某个段落的字体设置为白色,其元素的字体不用设置或设置为inhert,它就是白色。这些属性被称之为inherited property,它会从父元素获取对应属性的经过计算与转换的值(computed value),如果父元素和它的情形一样,它就继续往上找,最后没有就使用浏览器的默认值。
下面是 inherited properties的一览表:
border-collapse
border-spacing
caption-side
color
cursor
direction
empty-cells
font
font-family
font-stretch
font-size
font-size-adjust
font-style
font-variant
font-weight
letter-spacing
line-height
list-style
opacity
list-style-image
list-style-type
quotes
text-align
text-indent
text-transform
white-space
word-spacing
// function runEx(cod) {
cod=document.all(cod)
var code=cod.value;
if (code!=""){
var newwin=window.open('','','');//打开一个窗口并赋给变量newwin。
newwin.opener = null // 防止代码对页面修改
newwin.document.write(code);//向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
newwin.document.close();
}
}
// ]]> http://reference.sitepoint.com/css/inheritvalue
<!doctype html>
<html dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<style type="text/css">
</style>
<script type="text/javascript">
function getStyle(el, style){
if(!+"\v1"){
style = style.replace(/\-(\w)/g, function(all, letter){
return letter.toUpperCase();
});
return el.currentStyle[style];
}else{
return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)
}
}
var _ = function(id){
return document.getElementById(id);
};
window.onload = function(){
alert(getStyle(_("text2"),"color"))
}
</script>
<title>CSS</title>
</head>
<body>
<div id ="text" style="width:20em;height:140px;background:#8080c0;padding:2px;border:1px solid red;color:#fff">父元素
<div id="text2" style="width:80%;height:4em;background:#b45b3e">子元素</div>
</div>
</body>
</html>
我们给父元素设置了字体的样式,没有设置子元素的,当取出子元素的时,发现其值转换为rgb格式(当然IE除外啦!)
不过,在IE7及其之前的版本,是不支持用inhert来设置direction与visibility以外的样式属性。具体可参见这里与这里
在IE8中,原本是inherited property的text-align在th中失效。
本来th应该会从table中继承文本向右对齐的设置,但失效了……
<!doctype html>
<html dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<style type="text/css">
table, tr, td, th {
border-collapse:collapse;
border:1px solid #000;
}
table {
text-align:right;
}
td, th {
width:100px;
}
</style>
<title>CSS</title>
</head>
<body>
<table>
<tr>
<th>Ruby</th>
<th>Rouvre</th>
</tr>
<tr>
<td>By</td>
<td>司徒正美</td>
</tr>
</table>
</body>
</html>
解决IE8这个弱智Bug也很容易,就是显式地设置inhert。
<!doctype html>
<html dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<style type="text/css">
table, tr, td, th {
border-collapse:collapse;
border:1px solid #000;
}
table {
text-align:right;
}
td, th {
width:100px;
}
th {
text-align:inherit;
}
</style>
<title>CSS</title>
</head>
<body>
<table>
<tr>
<th>Ruby</th>
<th>Rouvre</th>
</tr>
<tr>
<td>By</td>
<td>司徒正美</td>
</tr>
</table>
</body>
</html>
此外还有一些CSS属性是不能继承的,最经典如border系列。它被称之为non-inherited property,如果我们不为它设置,我们只能取得浏览器的默认值,默认值在火狐中被称之为 initial value 。一个相关的好消息是,默认值在火狐也可以指定了,这样我们就不用reset样式了!
下面是non-inherited property的一览表:
background
border
bottom
clear
display
float
height
left
margin
outline
overflow
padding
position
right
top
visibility
width
z-index
<!doctype html>
<html dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<style type="text/css">
</style>
<script type="text/javascript">
function getStyle(el, style){
if(!+"\v1"){
style = style.replace(/\-(\w)/g, function(all, letter){
return letter.toUpperCase();
});
return el.currentStyle[style];
}else{
return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)
}
}
var _ = function(id){
return document.getElementById(id);
};
window.onload = function(){
alert(getStyle(_("text2"),"background-color"))
}
</script>
<title>CSS</title>
</head>
<body>
<div id ="text" style="width:20em;height:140px;background:#8080c0;padding:2px;border:1px solid red;color:#fff">父元素
<div id="text2" style="width:80%;height:4em;color:#b45b3e">子元素</div>
</div>
</body>
</html>
我们给父元素设置了背景颜色,没有设置子元素的,这时会取得浏览器的默认值transparent(W3C那一方好像只要是颜色都会转换为rgb格式,多出的a为Alpha)
http://monc.se/kitchen/38/cascading-order-and-inheritance-in-css http://elizabethcastro.com/html/extras/cssref.html
接着我们来看auto,这是一个含糊不清但是有长度概念的值。应用于以下属性:
overflow
cursor
height
width
marker-offset
margin
margin-* (left|bottom|top|right|start|end)
top
bottom
left
right
table-layout
z-index
-moz-column-width
languages
在块级元素的可度量的属性中(如width,height),如果不设置值,其默认值是auto,但它很容易会被父级元素的值覆盖,也就是隐式地成为了inhert了。在内联元素中,由于不具备盒子模型,如果不设置,就算是火狐也原本奉还它,这对于精确计算元素的宽度与高度是非常不利的。auto还有对称性,这个在居中布局我们常常运用到它。在非度量的属性中,如overflow,就要具体情况具体分析了。
一个很显浅的寓言,千年老树,电打雷劈,屹立不倒,却毁于蝼蚁的侵袭之下。自以为精通CSS的人,常常被一些小问题搞到头晕脑胀。通常是一个很小的数值,经过层层放大歪曲后,整个布局就走形了。CSS是一门很简单的语言,易学易用,但也最容易出垃圾代码。这是没有深入研究这门语言所致。本人认为,CSS是由以下三大块构成的:默认值,继承系统与加权系统。默认值,也就是浏览器在用户没有设置属性的情况下,默认指定的属性。CSS框架基本都有一个叫reset.css 的文件,就是对其进行重设,消除各浏览器的差异的。继承系统就是下面要重点讨论的东西。加权系统,也就是优先级的问题,不在本文的讨论范畴,不说了。
在CSS中,许多属性都是可以继承的,如某个段落的字体设置为白色,其元素的字体不用设置或设置为inhert,它就是白色。这些属性被称之为inherited property,它会从父元素获取对应属性的经过计算与转换的值(computed value),如果父元素和它的情形一样,它就继续往上找,最后没有就使用浏览器的默认值。
下面是 inherited properties的一览表:
border-collapse
border-spacing
caption-side
color
cursor
direction
empty-cells
font
font-family
font-stretch
font-size
font-size-adjust
font-style
font-variant
font-weight
letter-spacing
line-height
list-style
opacity
list-style-image
list-style-type
quotes
text-align
text-indent
text-transform
white-space
word-spacing
// function runEx(cod) {
cod=document.all(cod)
var code=cod.value;
if (code!=""){
var newwin=window.open('','','');//打开一个窗口并赋给变量newwin。
newwin.opener = null // 防止代码对页面修改
newwin.document.write(code);//向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
newwin.document.close();
}
}
// ]]> http://reference.sitepoint.com/css/inheritvalue
<!doctype html>
<html dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<style type="text/css">
</style>
<script type="text/javascript">
function getStyle(el, style){
if(!+"\v1"){
style = style.replace(/\-(\w)/g, function(all, letter){
return letter.toUpperCase();
});
return el.currentStyle[style];
}else{
return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)
}
}
var _ = function(id){
return document.getElementById(id);
};
window.onload = function(){
alert(getStyle(_("text2"),"color"))
}
</script>
<title>CSS</title>
</head>
<body>
<div id ="text" style="width:20em;height:140px;background:#8080c0;padding:2px;border:1px solid red;color:#fff">父元素
<div id="text2" style="width:80%;height:4em;background:#b45b3e">子元素</div>
</div>
</body>
</html>
我们给父元素设置了字体的样式,没有设置子元素的,当取出子元素的时,发现其值转换为rgb格式(当然IE除外啦!)
不过,在IE7及其之前的版本,是不支持用inhert来设置direction与visibility以外的样式属性。具体可参见这里与这里
在IE8中,原本是inherited property的text-align在th中失效。
< table > |
< tr > |
< th >Ruby</ th > |
< th >Rouvre</ th > |
</ tr > |
< tr > |
< td >By</ td > |
< td >司徒正美</ td > |
</ tr > |
</ table > |
table, tr, td, th { |
border-collapse : collapse ; |
border : 1px solid #000 ; |
} |
table { |
text-align : right ; |
} |
td, th { |
width : 100px ; |
} |
<!doctype html>
<html dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<style type="text/css">
table, tr, td, th {
border-collapse:collapse;
border:1px solid #000;
}
table {
text-align:right;
}
td, th {
width:100px;
}
</style>
<title>CSS</title>
</head>
<body>
<table>
<tr>
<th>Ruby</th>
<th>Rouvre</th>
</tr>
<tr>
<td>By</td>
<td>司徒正美</td>
</tr>
</table>
</body>
</html>
解决IE8这个弱智Bug也很容易,就是显式地设置inhert。
table, tr, td, th { |
border-collapse : collapse ; |
border : 1px solid #000 ; |
} |
table { |
text-align : right ; |
} |
td, th { |
width : 100px ; |
} |
th { |
text-align :inherit; |
} |
<html dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<style type="text/css">
table, tr, td, th {
border-collapse:collapse;
border:1px solid #000;
}
table {
text-align:right;
}
td, th {
width:100px;
}
th {
text-align:inherit;
}
</style>
<title>CSS</title>
</head>
<body>
<table>
<tr>
<th>Ruby</th>
<th>Rouvre</th>
</tr>
<tr>
<td>By</td>
<td>司徒正美</td>
</tr>
</table>
</body>
</html>
此外还有一些CSS属性是不能继承的,最经典如border系列。它被称之为non-inherited property,如果我们不为它设置,我们只能取得浏览器的默认值,默认值在火狐中被称之为 initial value 。一个相关的好消息是,默认值在火狐也可以指定了,这样我们就不用reset样式了!
下面是non-inherited property的一览表:
background
border
bottom
clear
display
float
height
left
margin
outline
overflow
padding
position
right
top
visibility
width
z-index
<!doctype html>
<html dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<style type="text/css">
</style>
<script type="text/javascript">
function getStyle(el, style){
if(!+"\v1"){
style = style.replace(/\-(\w)/g, function(all, letter){
return letter.toUpperCase();
});
return el.currentStyle[style];
}else{
return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)
}
}
var _ = function(id){
return document.getElementById(id);
};
window.onload = function(){
alert(getStyle(_("text2"),"background-color"))
}
</script>
<title>CSS</title>
</head>
<body>
<div id ="text" style="width:20em;height:140px;background:#8080c0;padding:2px;border:1px solid red;color:#fff">父元素
<div id="text2" style="width:80%;height:4em;color:#b45b3e">子元素</div>
</div>
</body>
</html>
我们给父元素设置了背景颜色,没有设置子元素的,这时会取得浏览器的默认值transparent(W3C那一方好像只要是颜色都会转换为rgb格式,多出的a为Alpha)
http://monc.se/kitchen/38/cascading-order-and-inheritance-in-css http://elizabethcastro.com/html/extras/cssref.html
接着我们来看auto,这是一个含糊不清但是有长度概念的值。应用于以下属性:
overflow
cursor
height
width
marker-offset
margin
margin-* (left|bottom|top|right|start|end)
top
bottom
left
right
table-layout
z-index
-moz-column-width
languages
在块级元素的可度量的属性中(如width,height),如果不设置值,其默认值是auto,但它很容易会被父级元素的值覆盖,也就是隐式地成为了inhert了。在内联元素中,由于不具备盒子模型,如果不设置,就算是火狐也原本奉还它,这对于精确计算元素的宽度与高度是非常不利的。auto还有对称性,这个在居中布局我们常常运用到它。在非度量的属性中,如overflow,就要具体情况具体分析了。
相关文章推荐
- 【CSS】inherit 和 auto
- CSS的inherit与auto使用分析
- CSS中inherit指定继承的使用方法和auto的区别
- CSS的inherit与auto使用分析
- css inherit和auto的区别
- CSS的inherit与auto使用分析
- CSS的inherit与auto
- CSS的inherit与auto
- CSS的inherit与auto使用分析
- css里头的inherit和auto
- CSS的inherit与auto
- 深入理解CSS的height:100%和height:inherit之间的使用区别
- CSS中inherit指定继承的使用方法
- 转载CSS中height:100%和height:inherit的异同
- css 中display的inherit
- CSS中height:100%和height:inherit的异同
- I do not want to inherit the child opacity from the parent in CSS(不想让子元素继承父元素的透明度)
- 谈谈一些有趣的CSS题目(四)-- 从倒影说起,谈谈 CSS 继承 inherit
- 关于TabControl 控件,窗体自动适应,大小的属性AutoScaleModo=Inherit
- CSS中height:100%和height:inherit的异同