简体   繁体   English

如何防止HTML规则成为CSS规则的目标?

[英]How to prevent a HTML element from being targeted by a CSS rule?

Here is a difficulty I am trying to solve. 这是我要解决的难点。 I am working inside a client's page to develop a scroller interface. 我在客户端页面内工作以开发滚动界面。 Basically, I cannot change the doctype, the surrounding elements and the stylesheets or scripts that are already in the client's page and I have to make my little block of code "fit" inside this. 基本上,我无法更改doctype,周围元素以及客户页面中已有的样式表或脚本,我必须让我的小块代码“适合”在其中。 This is common for web developers. 这对Web开发人员来说很常见。 The tricky part now is that some img elements inside my block are actually being targeted by a CSS rule inside the inherited client's stylesheet (which, of course, I cannot remove or change). 现在棘手的部分是我的块中的一些img元素实际上是被继承的客户端样式表中的CSS规则所针对的(当然,我无法删除或更改)。 It would be too long to explain why here in this case I actually can't use more specific CSS rules myself to compensate this, but it's a fact. 解释为什么在这种情况下为什么我实际上不能使用更具体的CSS规则来弥补这一点,但这是事实。 So my question is : is there a way to prevent a HTML element from being targeted by a CSS rule other than creating another rule or deleting the rule? 所以我的问题是:除了创建其他规则或删除规则之外,有没有办法阻止HTML元素被CSS规则作为目标? The difficulty is that a rule like 困难在于规则就像

.containter1 .containter3 { ... }

will target an element inside : 将目标内部的元素:

<div class="container1">
   <div class="containter2">
      <div class="containter3">Element
      ...

Elements inside the page don't make "walls" for CSS rules, which "jump" over containers to target elements. 页面内的元素不会为CSS规则制作“墙”,CSS规则会“跳过”容器到目标元素。 So a rule like 所以像这样的规则

img { ... }

will target any img tag. 将针对任何img标记。 The only way I know to compensate this is to create a more specific CSS rule targetting the precise img to protect. 我知道补偿这一点的唯一方法是创建一个更具体的CSS规则,目标是保护精确的img。 But I cannot do that here. 但我不能在这里做到这一点。 Is there a way to get the same result without creating a CSS rule, only by adding HTML? 有没有办法在不创建CSS规则的情况下获得相同的结果,只有添加HTML?

/ * EDIT TO CLARIFY * / / *编辑澄清* /

I know CSS rules, specificity, inheritance, etc. My question was more pragmatic. 我知道CSS规则,特异性,继承等。我的问题更加务实。 Consider this example to clarify the problem : imagine you have a client's stylesheet that you can't touch and that defines the following general rule: 考虑这个例子来澄清问题:假设你有一个你无法触及的客户端样式表,它定义了以下一般规则:

img { display:none; }

The problem is that you cannot set a corresponding generic rule to do the opposite, like : 问题是您无法设置相应的通用规则来执行相反的操作,例如:

img { display:not-none; }

because there is no such thing as the opposite to none. 因为没有与之相反的事情。 The opposite of "none" can either be "inline" , "block" , "inline-block" , and so on. "none"的反义词可以是"inline""block""inline-block"等等。

So basically, this means that the first generic rule forces you to explicitly define the display property for each and every img in your page. 基本上,这意味着第一个通用规则会强制您为页面中的每个img显式定义display属性。 And that sucks. 这很糟糕。 So I was trying to find a hack to solve situations like this (my actual problem is even worst than this, believe me, but this example is much clearer and quicker to explain). 所以我试图找到一个黑客来解决这样的情况(我的实际问题甚至比这还要糟糕,相信我,但这个例子更清晰,更快速地解释)。

If you're saying you want to prevent targeting without changing any code, then no, that's obviously not possible. 如果您说要在不更改任何代码的情况下阻止定位,那么不,这显然是不可能的。

In-line styles always over-ride style-sheet rules ( unless they're using an !important tag, then you'll need to also use it). 内联样式总是覆盖样式表规则(除非他们使用!important标记,然后您还需要使用它)。

You should be able to reset whatever elements you need, using syntax from your favorite CSS reset. 您应该可以使用您喜欢的CSS重置语法重置所需的任何元素。 Here are some options: 以下是一些选项:

http://www.cssreset.com/ http://www.cssreset.com/

So, something like - 所以,像 -

<div style="border:0 !important;padding:0 !important;margin:0 !important;height:auto;"></div>

is your best bet. 是你最好的选择。

The only way you can change CSS for specific element is modification of existing styleshits or creating new style which is more specific and will overload other styles. 您可以更改特定元素的CSS的唯一方法是修改现有样式快照或创建更具体的新样式并将使其他样式超载。

and I have to make my little block of code "fit" inside this. 我必须让我的小块代码“适合”。

Once you have make some block of code, you can put style tag inside that block of HTML code like this, for instance: 一旦你创建了一些代码块,就可以将样式标记放在这个HTML代码块中,例如:

<div id="block_of_code_available_for_modification">
   <style type="text/css">
       //css code which will fix styles of your content without influencing other elements on a page.
   </style>
</div>

Or, if you have just a few elements you need to fix styles for, you can use style attribute of HTML elements (once you can set modify HTML, you can always add something like below... Well, the same as adding style tag). 或者,如果您只需要修复样式的几个元素,则可以使用HTML元素的style属性(一旦您可以设置修改HTML,您可以随时添加类似下面的内容......嗯,与添加样式标记相同)。 Priority of css properties inside style attribute is the highest one. style属性中的css属性的优先级最高。 Except if there is no !important in some previouse styles: 除非在某些前期风格中没有!important

<img style="any css properties you need" src="..." />

The default display value for an img element is inline-block . img元素的默认display值是inline-block If you want to reset the display value for all images, why not use that? 如果要重置所有图像的显示值,为什么不使用它?

If you've got multiple different types of elements that are being set to weird values, then the problem is maybe a bit more complex as you'd need to consider which elements to set to what display type. 如果您有多种不同类型的元素被设置为奇怪的值,那么问题可能会更复杂,因为您需要考虑将哪些元素设置为哪种显示类型。 But all HTML elements do have well-defined default display types, so it shouldn't be too hard to reset them all. 但是所有HTML元素都有明确定义的默认显示类型,因此重置它们不应该太难。

img {display: inline-block;}
span, a, etc {display:inline;}
div, etc {display:block;}
... etc ...

If it comes down to it, you could just use one of the reset CSS scripts that are available, to set everything back to the correct defaults. 如果归结为它,您可以使用其中一个可用的重置CSS脚本 ,将所有内容设置回正确的默认值。

No there is no way you can stop other rules from getting applied on a particular element. 没有办法阻止其他规则应用于特定元素。 you have to redefine all those rules for that html element so they will overwrite all the other rules. 你必须重新定义该html元素的所有规则,以便它们覆盖所有其他规则。

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

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