仅对特定元素trait应用样式表 [英] Apply style sheet to only a specific element trait

查看:116
本文介绍了仅对特定元素trait应用样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在做了一些google搜索后,我无法得出一个答案,所以这里是我的问题。是否有一种方法来指定一个特定的元素trait,比如一个整个样式表应该应用的id?

After doing a bit of googling I was unable to come up with an answer, so here's my question. Is there a way to specify a specific element trait, such as an id, inwhich an entire style sheet should be applied?

例如,如果我有一个html块

For example if I have a block of html like this

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="./style.css">
    </head>
    <body>
        <div id="my_id">
            <!-- A bunch of elements here -->
        </div>
    </body>
</html>

是否有一种方法可以指定 ./ style中包含的规则。类似于指定 #my_id 只能应用于 #my_id 中匹配的元素

Is there a way to specify that the rules contained in ./style.css should only be applied to matching elements with in #my_id, similar to specifying #my_id before each rule in the style sheet.

我理解在每个规则实现之前指定 #my_id

I understand specifying #my_id before each rule will achieve this, but am wondering if there is a way to do it without the need to add bloat to the style sheet.

我正在使用一个greasemonkey(userscript)脚本,它创建了一个在特定页面上创建并放置所有其他所需GUI元素的容器元素。我不希望我的CSS干扰页面上的CSS(例如意外重写了已经存在的页面上的规则),但不想添加不必要的bloat到我的样式表,如果它可以避免,因为所有规则

I'm working on a greasemonkey(userscript) script, which creates a 'container' element on a specific page in which it creates and places all other GUI elements required. I don't want my CSS interfering with the CSS on the page(such as accidentally over writing rules that already exist on the page), but don't want to add unnecessary bloat to my style sheet if it can be avoided since all rules are only to be applied to the 'wrapper' element and/or it's children.

推荐答案

至仅适用于'wrapper'元素和/我的知识这是不可能的。正如你所说,你可以在规则前加上ID,但这不是你想做的。

To my knowledge this is not possible. As you stated you can prefix the rules with the ID, but this is not what you wanted to do.

我应该说,你不应该指定一个单一ID的样式表,因为ID应该在页面的上下文中是唯一的。此外,我发现将不同的ID应用于在不同的页面上用于不同目的的元素是不好的做法,因为它可以使常见的JavaScript页面很混乱。

I should say however, that you shouldn't have to specify a stylesheet for a single ID, because an ID is supposed to be unique in the context of the page. Furthermore, I find it bad practice to apply the same ID to elements which serve different purposes on different pages, because it can make writing common JavaScript pages quite confusing.

这篇关于仅对特定元素trait应用样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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