[英]CSS getting applied to all files even though not imported
I am dealing with 2 components: header.js
and footer.js
.我正在处理 2 个组件:
header.js
和footer.js
。
I also have 2 css files: header.module.css
and footer.module.css
.我也有 2 个 css 文件:
header.module.css
和footer.module.css
。 Both of them use different styling for the a
tag.他们都为
a
标签使用了不同的样式。
I import the respective CSS files within each js file, but the a
styling in footer.module.css
seems to overtake the styling in header.js
even though it wasn't imported.我在每个 js 文件中导入了相应的 CSS 文件,但是
footer.module.css
中a
样式似乎超过了header.js
中的样式,即使它没有被导入。
Here is the code:这是代码:
header.js
import React from "react"
import { Link } from "gatsby"
import "../styles/header.module.css";
const ListLink = props => (
<li style={{display: `inline-block`, marginRight: `1rem`, fontSize: '1.15rem', fontWeight: 'bold'}}>
<Link className="link" to={props.to}>{props.children}</Link>
</li>
)
footer.js
import React from "react"
import "../styles/footer.module.css";
const FooterLink = props => (
<li style={{ display: `inline-block`, marginRight: `1rem`, marginBottom:'0rem', fontSize: '1.05rem', fontWeight: 'bold'}}>
<a href={props.to} target="_blank">{props.children}</a>
</li>
)
header.module.css
a {
color: var(--textLink);
text-shadow: var(--textShadow);
transition:.2s;
background-image: var(--bgimage);
}
a:hover {
color: #da1e11;
background-image: none;
}
footer.module.css
a{
color: var(--textLink);
text-shadow: var(--textShadow);
transition:.2s;
background-image: none;
}
a:hover {
color: #da1e11;
background-image: none;
}
The background-image
property from footer
overtakes the one specified in header
. footer
的background-image
属性超过了header
中指定的属性。
According to the documentation it's import something from './something.module.css'
and then <Component className={something.error}
根据文档,它
import something from './something.module.css'
,然后<Component className={something.error}
If you are using Gatsby's default <Layout>
, it shares <Header>
and <Footer>
components so, both are loading each CSS in each page as you can see here:如果您使用 Gatsby 的默认
<Layout>
,它共享<Header>
和<Footer>
组件,因此,两者都在每个页面中加载每个 CSS ,如您在此处看到的:
return (
<>
<Header siteTitle={data.site.siteMetadata.title} />
<div>
<main>{children}</main>
<Footer>
© {new Date().getFullYear()}, Built with
{` `}
<a href="https://www.gatsbyjs.org">Gatsby</a>
</Footer>
</div>
</>
)
}
The easiest solution is to wrap the each component in a class to make the CSS only available inside that class, something like this:最简单的解决方案是将每个组件包装在 class 中,以使 CSS 仅在 class 内部可用,如下所示:
import React from "react"
import { Link } from "gatsby"
import "../styles/header.module.css";
const ListLink = props => (
<li className="list__item">
<Link className="link" to={props.to}>{props.children}</Link>
</li>
)
Note: you can even wrap the <Link>
inside a <div>
for example.注意:例如,您甚至可以将
<Link>
包装在<div>
中。
I would suggest removing inline styles if you are using CSS modules to avoid mixing styles and improve readability.如果您使用 CSS 模块,我建议删除内联 styles 以避免混合 styles 并提高可读性。
The same applies for the <Footer>
component. <Footer>
组件也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.