官术网_书友最值得收藏!

  • React Cookbook
  • Carlos Santana Roldan
  • 207字
  • 2021-07-16 17:49:35

Styling a component with CSS classes and inline styles

In the last recipe, we learned how to create a class component. Now let's add some CSS to our Home component.

In React, one of the best practices is to have the style file in the same directory as the component. If you have worked with PHP, Node, or any other server language, you probably write your styles in a style.css file, and you include it using a link tag in your template. React uses Webpack, which is the most popular module bundler at the moment. With Webpack, we can configure the way that we want to handle our styles (using CSS directly or by using a CSS preprocessor such as Sass, Stylus, or Less CSS), and with Webpack we can implement CSS modules. This is a powerful way to avoid the three main issues of CSS:

  • No more conflicts (unintentional CSS overwrites)
  • Explicit dependencies (styles per component)
  • No global scope

In Chapter 10Mastering Webpack 4.x, we will cover Webpack, and we'll be able to implement CSS modules using Sass or Stylus in our project.

主站蜘蛛池模板: 沾益县| 宜兰县| 石嘴山市| 沈阳市| 陇南市| 中江县| 来安县| 台东县| 登封市| 柳林县| 老河口市| 凉城县| 鹤壁市| 龙门县| 天津市| 开化县| 肥城市| 永丰县| 五常市| 永清县| 高雄市| 贡山| 揭阳市| 梓潼县| 安西县| 遂溪县| 金山区| 都匀市| 大兴区| 光泽县| 武定县| 林口县| 电白县| 南部县| 海盐县| 鄄城县| 漠河县| 太原市| 青州市| 墨脱县| 鸡东县|