CSS控件继承 - 继承其他控件样式 [英] CSS Control Inheritance - Inheriting Other Control Styles
问题描述
我的问题的两个部分:
1)有没有办法在CSS中继承另一个控件的属性和样式?假设控件没有父级/子级层次结构,并且可以是完全不同的控件类型:
1) Is there a way to inherit another control's attributes and styles in CSS? Assume the controls have no parent/child hierarchy and can be completely different control types:
IE:
#MyFirstControl
{
width: 100px;
}
#MySecondControl
{
width: MyFirstControl.styles.width; /* This doesn't work */
}
2)假设一个Label标签任何其他标签的子级。 width属性将不会与inherit或auto一起使用。发生了什么问题?
2) Assume a Label tag is a child of any other tag. The width attribute will neither work with "inherit" nor "auto". What's wrong?
IE:
<style>
div
{
width: 100px;
}
</style>
<div>
<!-- This label does what it wants for width. It's not the width of the containing div -->
<label style="width: inherit">Some Text</label>
<div>
推荐答案
第1部分: ids,以控制样式:
Part 1: you want to use class names, not ids, to control the styles:
.control_a {
width: 100px;
}
<blah id='MyFirstControl' class='control_a'/>
<blah id='MySecondControl' class='control_a'/>
这样可以在任何数量的标签之间共享样式。另外,请注意,您可以在单个元素上使用多个类名:
This lets you share styles across any number of tags. Also, keep in mind, you can use more than one class name on a single element:
.control_a {
width: 100px;
}
.red { background: #f00; }
.blue { background: #00f; }
<blah id='MyFirstControl' class='control_a red'/>
<blah id='MySecondControl' class='control_a blue'/>
这可让您为单个元素选择多种不同的样式来源。
This lets you select many different sources of style for a single element.
这篇关于CSS控件继承 - 继承其他控件样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!