最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Vite React + Bootstrap: CSS rule not applying only to img element - Stack Overflow

programmeradmin3浏览0评论

I'm using Vite (React, Bootstrap) to create a React app. I created a component with a specific CSS file imported. The CSS file is being read, as html {color: red} is being applied (and overriden, but img.someclass or div.otherclass won't show anywhere in the DOM, not even overriden.

This is the component.

import "./GameboyBackground.module.css"
import gameboyImage from "/music-games/assets/gameboy/blue.png"

function GameboyBackground() {
  return (
    <>  
    <div>
        <img
          src={gameboyImage}
          alt="gameboy"
          className="gameboy-bg"
        />
      </div>
    </>
  );
}

export default GameboyBackground;

The CSS file looks like this. Again: html element is affected, img is not even overriden.

img.gameboy-bg {
  width: 100vw;
  height: 100vh;
  object-fit: contain;
}


html {
  color: blue;
}

If I try to apply styles inline, the app won't even render, only an empty root div.

<img
  src={gameboyImage}
  alt="gameboy"
  className="gameboy-bg"
  style="width: 100px;"
/>
// App "crashes"

I'm using Vite (React, Bootstrap) to create a React app. I created a component with a specific CSS file imported. The CSS file is being read, as html {color: red} is being applied (and overriden, but img.someclass or div.otherclass won't show anywhere in the DOM, not even overriden.

This is the component.

import "./GameboyBackground.module.css"
import gameboyImage from "/music-games/assets/gameboy/blue.png"

function GameboyBackground() {
  return (
    <>  
    <div>
        <img
          src={gameboyImage}
          alt="gameboy"
          className="gameboy-bg"
        />
      </div>
    </>
  );
}

export default GameboyBackground;

The CSS file looks like this. Again: html element is affected, img is not even overriden.

img.gameboy-bg {
  width: 100vw;
  height: 100vh;
  object-fit: contain;
}


html {
  color: blue;
}

If I try to apply styles inline, the app won't even render, only an empty root div.

<img
  src={gameboyImage}
  alt="gameboy"
  className="gameboy-bg"
  style="width: 100px;"
/>
// App "crashes"
Share Improve this question edited Feb 17 at 0:32 Phil 165k25 gold badges261 silver badges267 bronze badges asked Feb 16 at 13:34 Marcos BaconMarcos Bacon 213 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

See the Vite documentation for CSS Modules

Any CSS file ending with .module.css is considered a CSS modules file. Importing such a file will return the corresponding module object

This creates unique selectors for your styles that you'll need to import in order to use. For example

// import the class names from your CSS module
import classes from "./GameboyBackground.module.css";

// ...

return (
  <img
    src={gameboyImage}
    alt="gameboy"
    className={classes['gameboy-bg']}
  />
)

The actual class name will be something like _gameboy-bg_<hash>

发布评论

评论列表(0)

  1. 暂无评论