简体   繁体   English

如何在css中继承类

[英]how to inherit classes in css

I wrote few css classes such as these: 我写了几个css类,比如:

.bad
{
    background:#fff0f0;
    color:#800;
    width:1024px;
    padding-left:10px;
    margin-top:0;
    margin-bottom:0;
}
.good
{
    background:#eeffee;
    color:#008800; 
    width:1024px;        //same as above
    padding-left:10px;   //same as above
    margin-top:0;        //same as above
    margin-bottom:0;     //same as above
}

As you see out of 6 attributes, 4 are exactly same. 正如您在6个属性中看到的那样,4个属性完全相同。 So I thought of avoiding this duplication by writing a .common class and inherit them when defining the above classes, as shown below: 所以我想通过编写.common类来避免这种重复,并在定义上面的类时继承它们,如下所示:

.common
{
    width:1024px;
    padding-left:10px;
    margin-top:0;
    margin-bottom:0;
}
.good, .common    //inherit .commom
{
    background:#eeffee;
    color:#008800;
}
.bad, .common     //inherit .commom
{
    background:#fff0f0;
    color:#800;
}

But these change doesn't produce the same effect on the browser. 但是这些更改不会对浏览器产生相同的影响。 I'm applying them on <p> tags and I see a gap between two consecutive <p > tags in the latter case. 我在<p>标签上应用它们,在后一种情况下我看到两个连续的<p >标签之间存在间隙。 So it seems margin-* attributes are not inherited (or doesn't work the way it works in the first case). 所以似乎margin-*属性不是继承的(或者在第一种情况下不起作用)。

All I'm looking for a way to avoid code-duplication. 我正在寻找一种避免代码重复的方法。 Is there any way to achieve that? 有没有办法实现这一目标?

I'm not allowed to use less or any external tool or library. 我不允许使用更少或任何外部工具或库。 I just want my code to look shorter and concise in native CSS which I can put in the html file itself (no external .css file). 我只是希望我的代码在原生CSS中看起来更简洁,更简洁,我可以把它放在html文件本身(没有外部的.css文件)。 I'm allowed to use javascript in the html itself (no jquery though). 我被允许在html本身使用javascript(虽然没有jquery)。

Using HTML you can inherit the other classes properties. 使用HTML,您可以继承其他类属性。 You need to apply in your paragraph like this. 你需要像这样申请你的段落。

<p class="good common">Some Text</p>
<p class="bad common">Some new Text</p>

Need to update your CSS like below. 需要更新您的CSS,如下所示。

.common
{
width:1024px;
padding-left:10px;
margin-top:0;
margin-bottom:0;
}
.good
{
background:#eeffee;
color:#008800;
}
.bad
{
background:#fff0f0;
color:#800;
}

Explanation : 说明

First I will show you want your current structure is doing. 首先,我将向您展示您希望当前的结构正在做什么。 Check this fiddle http://jsfiddle.net/FeyLJ/ . 检查这个小提琴http://jsfiddle.net/FeyLJ/ Now you can see the margin between two paragraph. 现在你可以看到两段之间的差距。 So As of your thinking it will not inherit anything from CSS. 所以,根据你的想法,它不会从CSS继承任何东西。 So need to add the common class to your paragraph. 所以需要在你的段落中添加公共类。 Like 喜欢

 <p class="good common">My Smaple Text</p>
 <p class="bad common">My another Simple Text</p>

After adding this see the fiddle http://jsfiddle.net/FeyLJ/1/ what effect you got it? 添加之后看到小提琴http://jsfiddle.net/FeyLJ/1/你得到了什么效果? Now the margin issue gone. 现在保证金问题消失了。 But did you notice that it produce one more issue. 但是你注意到它又产生了一个问题。 Yes both the paragraph now apply the same color background. 是的,段落现在都应用相同的颜色背景。 Why? 为什么? In CSS if you use the same class with same properties two times then it will take properties from the last defined place. 在CSS中,如果您使用具有相同属性的相同类两次,则它将从最后定义的位置获取属性。 In this case it is taken the same bad class background for both. 在这种情况下,两者都采用相同的坏类背景。

For your clarification move the order of the class like below. 为了您的澄清,移动类的顺序如下。

.bad, .common 
{
background:#fff0f0;
color:#800;
} 

.good, .common 
{
background:#eeffee;
color:#008800;
}

This time it will apply the green color background instead of pink color. 这次它将应用绿色背景而不是粉红色。 I hope you understand what order is doing here. 我希望你明白这里的订单是做什么的。 So it will not inherit anything and why you need this? 所以它不会继承任何东西,为什么你需要这个? We can take out the common class. 我们可以拿出公共课。 So Whereever we want, we can just use multiple classes like my answer above. 所以无论我们想要什么,我们都可以像上面的答案一样使用多个类。

Here is the fiddle with my answer. 这是我答案的小提琴。

.bad, .common //inherit .commom doesn't do what you think, it sets styles to both, to .bad and .common elements. .bad, .common //inherit .commom没有按你的想法做,它将样式设置为.bad.common元素。

If you have both classes on each element, you can use that. 如果每个元素都有两个类,则可以使用它。

<style>

.common {background: #fff0f0; color: #800; width: 1024px; padding-left: 10px; margin-top: 0; margin-bottom: 0;}
.good {background: #efe; color: #080; }

</style>
<p class="common bad">bad</p>
<p class="common good">good</p>

If you have just one class, bad and good on each element, use that 如果你只有一个类,每个元素bad good ,那么就使用它

<style>

.bad, .good {background: #fff0f0; color: #800; width: 1024px; padding-left: 10px; margin-top: 0; margin-bottom: 0;}
.good {background: #efe; color: #080; }

</style>
<p class="bad">bad</p>
<p class="good">good</p>

你应该写一个BaseClass并在你的标签中引用它:

<p class="MyBaseClassForP ExtraClass AnotherExtraClass">text</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM