我想将现有的CSS样式应用于页面上的所有标签。怎么样? [英] I want to apply an existing CSS style to all labels on a page. How?

查看:110
本文介绍了我想将现有的CSS样式应用于页面上的所有标签。怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这与旧的问题不同,因为这是已经存在的样式。因此,如果一个我们称之为standard_label_style的样式已经存在于一个包含的CSS文件中,我该怎么做才能说这个页面上的所有标签都应该具有这样的风格:

  class =standard_label_style

给每一个人?是的,我知道我可以将事后样式应用到jQuery或JavaScript代码片段中。我只是想了解我应该如何使用CSS。



后续

我收到了几条评论,说只是使用像这样的语法.standard_label_style,标签......不幸的是,它不会像我想要的那样做任何事情。这将允许我将其他规则应用于standard_label_style类以及此页面中的标签规则,但不允许将该样式应用于此页面上的所有标签。要看到这个例子,这里是一个样式表和html来演示。没有课堂的标签仍然不会以红色显示,但这正是我希望能够发生的事情。我希望将现有类应用于页面上的所有标签,而不仅仅是具有类的标签,并且不在此页面上添加新样式,现有样式应该是唯一的样式。



included.css:

  .standard_label_style {color:red; } 

test.html:

 < HTML> 
< head>
< link rel =stylesheettype =text / csshref =included.css>
< style>
.standard_label_style,标签{}
< / style>
< / head>
< body>
< label class =standard_label_style>测试标签< / label>< br />
< label> Unclassed Test Label< / label>
< / body>
< / html>


解决方案

p>

您可以直接对所有标签应用样式:

  label {
颜色:石灰;
}

或将类应用于所有标签

  .labelClass {
color:Lime;
}

< label class =labelClass>< / label>

您也可以有多个选择器,因此您可以将当前样式修改为

  .labelClass,label {
color:Lime;
}

标准CSS无法实现的功能就像

  label {
.labelClass;
}

好消息是有一堆服务器端库使CSS吸少,让你做这种事情,例如,如果你使用.NET,请参阅 dotLess 提供了嵌套规则和一个基本的漫游模型。

Note, this is different than the older question How can I apply CSS on all buttons which are present in that page? because this is an already existing style. So given that a style, which we'll call "standard_label_style" already exists in an included CSS file, what can I do to say that all the labels on this page should have that style short of adding:

class="standard_label_style"

to each and every one? And yes, I know I could apply the styles ex-post-facto with a snippet of jQuery or JavaScript code. I'm just trying to learn how I'm supposed to do it with CSS.

Follow Up

I've gotten several comments that say just use syntax like this .standard_label_style, label... Unfortunately that does nothing like what I want. That would allow me to apply additional rules to the standard_label_style class, as well as rules to labels within this page, but would not allow me to apply that style to all the labels on this page. To see an example of this, here is a stylesheet and html to demonstrate. The label without a class will still not appear in red but that's what I'm hoping to have happen. I want to apply an existing class to all those labels on the page, not just the one with the class and without adding new styling on this page, the existing style should be the only style.

included.css:

.standard_label_style { color: red; }

test.html:

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="included.css">
    <style>
      .standard_label_style, label { }
    </style>
  </head>
  <body>
    <label class="standard_label_style">Test Label</label><br/>
    <label>Unclassed Test Label</label>
  </body>
</html>

解决方案

CSS doesn't really work like that.

You can apply a style to all labels directly:

label {
    color: Lime;
}

or apply a class to all labels

.labelClass {
    color: Lime;
}

<label class="labelClass"></label>

You can also have multiple selectors, so you could ammend your current style to be

.labelClass, label {
    color: Lime;
}

What you can't do in standard CSS is something like

label {
    .labelClass;
}

The good news is that there are a bunch of server side libraries which make CSS suck less and let you do exactly this kind of thing, see for example dotLess if you're using .NET which provides nested rules and a basic inheratance model.

这篇关于我想将现有的CSS样式应用于页面上的所有标签。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆