简体   繁体   English

禁用 CSS 特定 div 中所有元素的样式?

[英]Disable CSS Styling for all elements in specific div?

My script generates a user's email signature and then displays it in a popup modal.我的脚本生成用户的 email 签名,然后将其显示在弹出模式中。 The issue I have is that the modal-body div is also subject to the view's CSS styling which messes with the look and feel of the signature, hence I would like to disable CSS to get an unstyled look at the signature.我遇到的问题是, modal-body div 还受视图的 CSS 样式的影响,这会混淆签名的外观和感觉,因此我想禁用 CSS 以查看签名的无样式外观。

Is it possible to disable CSS styling for all elements in the modal-body div (p, br, a href, etc.) and how does one best go about this?是否可以禁用 CSS modal-body div 中所有元素的样式(p、br、a href 等),go 如何做到最好?

Thanks谢谢

There are various CSS reset values you can use with the all property to set the value of almost every property.您可以将各种 CSS 重置值与all属性一起使用,以设置几乎每个属性的值。

.modal-body {
  all: initial | unset | revert
}
  • initial value for property default values属性默认值的initial
  • unset value for property default or inherit if an inherited property如果是继承的属性,则unset属性默认值或继承
  • revert for default value for that element (browser styles), I think the only difference here would be that display would be block instead of inline for a div element. revert该元素的默认值(浏览器样式),我认为这里唯一的区别是显示将是块而不是 div 元素的内联。

The example above would only style the .modal-body itself though, if you want to reset values for every single element within it as well you could do the following:上面的示例只会设置.modal-body本身的样式,如果您也想重置其中每个元素的值,您可以执行以下操作:

.modal-body,
.modal-body * {
  all: initial | unset | revert
}

This will not only target .modal-body but every descendant of .modal-body .这不仅会针对.modal-body ,还会针对 .modal- .modal-body的每个后代。 Be careful with a value other than unset for this though as it's extremely powerful, and with great power comes great responsibility.小心使用unset以外的值,因为它非常强大,能力越大,责任越大。

If you're still having issues, your other styles might be more specific and therefore override this rule.如果您仍有问题,您的其他 styles 可能更具体,因此会覆盖此规则。 You could try and add !important however that would necessitate using it on every re-style of elements in your modal.您可以尝试添加!important但是这将需要在您的模态元素的每个重新样式中使用它。 In this case I would suggest giving your modal an ID and using that to style it, and combing through your other CSS to remove ID selectors flattening the specificity.在这种情况下,我建议为您的模式提供一个 ID 并使用它来设计它的样式,并通过您的其他 CSS 进行组合以删除 ID 选择器以展平特异性。

Not sure I fully understand your comment... If this is not the HTML you meant, please update your question to contain your HTML and I'll update this answer:不确定我是否完全理解您的评论...如果这不是您的意思 HTML,请更新您的问题以包含您的 HTML,我将更新此答案:

<div id="modalSignaturePreview">
  <div class="modal-body"></div>
</div>

Anyways for the above HTML you can target it in CSS with #modalSignaturePreview.modal-body , here we're targeting all elements with a class of modal-body that are a descendant of any element with an ID of modalSignaturePreview无论如何,对于上面的 HTML,您可以使用#modalSignaturePreview.modal-body在 CSS 中定位它,这里我们将所有具有 class modal-body 的元素定位为 ID 为 modalSignaturePreview 的任何元素的后代

Sounds like you want the all css property.听起来您想要all css 属性。

.modal-body * {
  all: initial;
}

Example here这里的例子

Should return everything to before it was modified with CSS at all.应该将所有内容返回到用 CSS 修改之前的状态。 The specificity for .modal-body * is only 0 1 0 though, so you may need to make this selector more specific or add !important . .modal-body *的特异性仅为 0 1 0,因此您可能需要使此选择器更具体或添加!important

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

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