简体   繁体   English

CSS 被应用到所有文件,即使没有导入

[英]CSS getting applied to all files even though not imported

I am dealing with 2 components: header.js and footer.js .我正在处理 2 个组件: header.jsfooter.js

I also have 2 css files: header.module.css and footer.module.css .我也有 2 个 css 文件: header.module.cssfooter.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.cssa样式似乎超过了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 . footerbackground-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.

相关问题 反应中的 Css 文件被应用于所有文件,即使它只导入一个 - Css file in react gets applied to all files even though it was imported in just one 即使已导入,也找不到模块 - Module not found even though it is imported 为什么我将 css 中的 div 的无显示应用于其所有后代,即使我将其中一个后代设置为显示块? - Why is my display of none for my div in css being applied to all of its descendants even though I am setting one of the descendants to display block? $(…).timepicker不是函数错误,即使包括timepicker css和js文件? - $(…).timepicker is not a function error even though include timepicker css and js files? Typescript `Operator '+' cannot be applied to types` 错误发生,即使涵盖了所有情况 - Typescript `Operator '+' cannot be applied to types` error occurs even though all cases are covered 即使包含所有文件,在js小提琴中也看不到滑块 - slider not seen in js fiddle even though all files are included jQuery函数即使已导入也无法定义 - jquery function can not be defined even though it is imported Css 未应用于按钮 - Css not getting applied on button jQuery ui.css和js文件未应用于所有页面 - jquery ui.css and js files not applied to all pages CSS未申请背景图片 - Css is not getting Applied for background image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM