简体   繁体   English

不能将两个 CSS styles 用于 2 个不同的组件

[英]Cannot use two CSS styles for 2 different components

So i have a problem with the css. I have one login page which has css with styles in it.所以我对 css 有疑问。我有一个登录页面,其中包含 css 和 styles。 One of them is for span:其中之一是跨度:

login.css:登录.css:

span {
    font-size: 12px;
    padding: 3px;
    color: red;
    display: none;
}

and works fine on the login page, but now i have "my profile page " and im using a function from a library (react-drag-drop-files), which makes me a component which has a span element in it并且在登录页面上工作正常,但现在我有“我的个人资料页面”并且我正在使用库中的 function (react-drag-drop-files),这使我成为一个包含 span 元素的组件

在此处输入图像描述 and as i can see here it is using the login.css file which i have not imported into my React function. So i cant style only the span in the "my profile" page simply because i have no access to it.正如我在这里看到的那样,它正在使用我尚未导入到我的 React function 中的 login.css 文件。所以我不能仅在“我的个人资料”页面中设置跨度样式,因为我无权访问它。 I have also tried to make new span style in the myprofile.css but that messed up the span on the login page.我还尝试在 myprofile.css 中创建新的跨度样式,但这弄乱了登录页面上的跨度。 So I think I'm trying to find a way how to separate the two files even though they are not connected.所以我想我正在尝试找到一种方法来分离这两个文件,即使它们没有连接。

I am not sure I understand your problem.我不确定我理解你的问题。

Have you tried using a className for the spans in your login page and create a class in css?您是否尝试过在登录页面中使用 className 作为 span 并在 css 中创建一个类? By doing this you should be able to modify only the login page spans.通过这样做,您应该只能修改登录页面跨度。

You could also create different css files for each page.您还可以为每个页面创建不同的 css 文件。

Hope this helps you希望这对你有帮助

This is how I found out that your span styling applies to both the login page and the profile page.这就是我发现您的 span 样式同时适用于登录页面和个人资料页面的方式。 This is because you have globally targeted the span for styling in css, don't target the span globally, add a class for your span on the login page and style them这是因为你已经全局定位了 css 样式的范围,不要全局定位范围,在登录页面上为你的范围添加一个类并设置它们的样式

From my understanding of your question, I think you can try these.根据我对您问题的理解,我认为您可以尝试这些。

Add parent class to your login.css file:将父 class 添加到您的 login.css 文件中:

.parentClass span {
    font-size: 12px;
    padding: 3px;
    color: red;
    display: none;
} 

Or add a class name to your span in login.js.或者在 login.js 中将 class 名称添加到您的跨度中。

<span className="spanClass">
    // your style here
</span>

login.css file登录.css 文件

.spanClass {
    font-size: 12px;
    padding: 3px;
    color: red;
    display: none;
} 

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

相关问题 不同的组件采用相同的 CSS 样式 - different components taking same CSS styles 样式组件 - 两个不同的元素具有相同的 styles - Styled Components - two different elements with the same styles 两个具有不同 css 样式的打印按钮 - Two print buttons with different css styles DataTable js插件,如何在一列中的两个数据上使用不同的css styles - DataTable js plugin, how to use different css styles on two data in one column 如何在使用javascript创建的不同图像中使用不同的CSS样式? - How can use different css styles in different images created with javascript? 在反应的不同组件中更改 styles - changing styles in different components in react 如何在两个不同的组件中使用路由 - ReactJS - How to use routes in two different components - ReactJS 两种不同颜色的款式 - Two styles with different colors 是否可以在我们的 React 网站中为不同的组件使用不同的 CSS 库? - Is it possible to use different CSS libraries for different components in our react website? 如何在 react 和 next js 中使用 Styled 组件创建全局 styles,使用 css 文件和 styled 组件的组合是否更好? - How to Create global styles with Styled components in react and next js and is it better to use combination of css files and styled component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM