简体   繁体   English

如何防止 Css 应用于其他文件?

[英]How to Prevent Css applying to other Files?

In this React Component I have Cookbook.js and Cookbook.css.在这个 React 组件中,我有 Cookbook.js 和 Cookbook.css。 I have a bunch of styles in Cookbook.css and specifically it has我在 Cookbook.css 中有一堆 styles,特别是它

form {
    display: inline-block !important;
    padding-top: 30px;
    margin-left: 100px;
}

Which is fine since I import that into the Cookbook.js.这很好,因为我将它导入到 Cookbook.js 中。 But I created another Component called Survey.js along with Survey.css.但我与 Survey.css 一起创建了另一个名为 Survey.js 的组件。 In Survey.js I use a form as well but I ONLY import survey.css.在 Survey.js 中,我也使用了一个表单,但我只导入了survey.css。 Yet for some reason, The CSS from Cookbook.css gets applied to the form in my Survey.js.然而由于某种原因,来自 Cookbook.css 的 CSS 被应用于我的 Survey.js 中的表单。 As a result, my form on Survey.js is in a odd spot.结果,我在 Survey.js 上的表单处于一个奇怪的位置。 How Can I ensure that the css for each form is independent of each other?如何确保每个表格的 css 是相互独立的?

When you create CSS rules, it is often easier to use class names instead of id's.当您创建 CSS 规则时,使用 class 名称而不是 id 通常更容易。 Such as:如:

.class {
 background-color: blue; 
}

When you have common elements across multiple components, the CSS color will apply the styling to all elements such as:当您在多个组件中有共同元素时,CSS 颜色会将样式应用于所有元素,例如:

p {
 background-color: blue; 
}

If you want to differentiate the styling where it applies in one component but does not apply to another which I think you are trying to do in your case, you need to use id's instead of element or class names.如果您想区分它应用于一个组件但不适用于另一个组件的样式,我认为您正在尝试在您的情况下执行此操作,您需要使用 id 而不是元素或 class 名称。

Add an id to the component that you want to style and create a rule for that element such as:向要设置样式的组件添加 id 并为该元素创建规则,例如:

#hero {
 background-color: bluel;
}

This should be able to ensure that CSS is different from each other.这样应该可以保证CSS彼此不同。

I think that you are looking for CSS modules.我认为您正在寻找 CSS 模块。 CSS modules are CSS files that only apply to a single component. CSS 模块是仅适用于单个组件的 CSS 文件。 Here is an example: https://css-tricks.com/css-modules-part-1-need/ .这是一个示例: https://css-tricks.com/css-modules-part-1-need/ More about CSS modules can also be found on Google and other forums.更多关于 CSS 模块的信息也可以在 Google 和其他论坛上找到。

Thank you,谢谢,

Caiden Sanders.凯登·桑德斯。

In React when a component is mounted, its specific CSS file is also imported.React中,当一个组件被挂载时,它的特定CSS文件也会被导入。 You should know that React makes only a single HTML page application.你应该知道React只制作一个 HTML 页面应用程序。 In one HTML page if you import multiple CSS files and if they have conflicting CSS, then CSS will be applied on the basis of priority.在一个Z4C4AD5FCA2E7A3F74DB1CED00381AA4Z页面中,如果您导入多个CSS文件,并且它们与CSS FBEDZ文件

CSS that comes last overrides existing if common elements conflicting unless you haven't used !important with any property.如果公共元素发生冲突,最后出现的CSS将覆盖现有的元素,除非您没有将!important与任何属性一起使用。

So, you should use unique ids or classes to prevent conflicts wherever required, and use common CSS if you have similar behaviour for certain elements.因此,您应该在需要时使用唯一的 id 或类来防止冲突,如果您对某些元素有类似的行为,请使用常见的CSS

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

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