简体   繁体   English

如何在Create React App中使用CSS模块(React语义ui)?

[英]How to use css modules (react semantic ui) in create react app?

I am using react semantic ui and I want to add margin left and margin top for Table component (ie align it in center) I also want to reduce the cell size inside Table but my css is not working why so ? 我正在使用react语义ui,并且我想为Table组件添加左边距和上边距(即,将其居中对齐),我也想减小Table内的单元格大小,但是我的CSS无法正常工作,为什么呢? Do I need to add any css loader or scss loader ? 我是否需要添加任何CSS加载程序或Scss加载程序? I am not able to understand why is my CSS code not working ? 我不明白为什么我的CSS代码不起作用?

Note: Table component has a prop called className and I want to make use of className prop and add custom styles. 注意:表组件有一个名为className的道具,我想利用className道具并添加自定义样式。 Link: https://react.semantic-ui.com/collections/table/#types-pagination 链接: https//react.semantic-ui.com/collections/table/#types-pagination

updatePage.js: updatePage.js:

    import React, { Component } from 'react';
import { Icon, Label, Menu, Table } from 'semantic-ui-react';
import faker from 'faker';
import s from './updatesPage.css';

const UpdatesPage = () => (
    <Table celled className={s.updateForm}>
      <Table.Header>
        <Table.Row>
          <Table.HeaderCell>Name</Table.HeaderCell>
          <Table.HeaderCell>URN number</Table.HeaderCell>
          <Table.HeaderCell>Parish</Table.HeaderCell>
          <Table.HeaderCell>Last Updated</Table.HeaderCell>
          <Table.HeaderCell>Notes</Table.HeaderCell>
        </Table.Row>
      </Table.Header>

      <Table.Body>
        <Table.Row>
          <Table.Cell>{faker.name.findName()}</Table.Cell>
          <Table.Cell>{faker.random.number()}</Table.Cell>
          <Table.Cell>{faker.address.streetAddress()}</Table.Cell>
          <Table.Cell>{faker.date.weekday()}</Table.Cell>
          <Table.Cell>{faker.random.words()}</Table.Cell>
        </Table.Row>
        <Table.Row>
          <Table.Cell>{faker.name.findName()}</Table.Cell>
          <Table.Cell>{faker.random.number()}</Table.Cell>
          <Table.Cell>{faker.address.streetAddress()}</Table.Cell>
          <Table.Cell>{faker.date.weekday()}</Table.Cell>
          <Table.Cell>{faker.random.words()}</Table.Cell>
        </Table.Row>
      </Table.Body>
    </Table>
)

export default UpdatesPage;

updatePage.css: updatePage.css:

.updateForm {
    margin-top: 100px;
    margin-left: 50px;
}

Above code is working ie javascript is working but the CSS is not working why so ? 上面的代码可以正常工作,即javascript可以工作,但是CSS不能工作,为什么呢? See screenshot: https://imgur.com/a/sBEUA6J I am not able to see any margin top, left for table. 参见屏幕截图: https : //imgur.com/a/sBEUA6J我看不到任何留在表顶部的边距。

In order to use CSS modules with create-react-app you need to rename your css file to updatesPage.module.css. 为了将CSS模块与create-react-app一起使用,您需要将CSS文件重命名为updatesPage.module.css。

You can find documentation for this in the “Adding a CSS Modules Stylesheet” portion of the user guide 您可以在用户指南的“添加CSS模块样式表”部分中找到有关此文档的信息

The issue with line import css. 行导入css的问题。 While import the css no need to give the module name for it. 导入CSS时,无需为其指定模块名称。 Just change the import line as below, it will work 只需如下更改导入行,它将起作用

import './updatesPage.css';

Also change the code for className as below 还要如下更改className的代码

<Table celled className='updateForm'>

Like sematic ui take priority while setting the css. 像sematic ui一样,在设置CSS时会优先处理。 So you need to change the priority in css add !important . 因此,您需要在css add !important中更改优先级。

.updateForm {
    margin-top: 100px !important;
    margin-left: 50px !important;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM