如何在组件反应中注入CSS

时间:2017-08-02 17:08:50

标签: html css reactjs

我已将html文件转换为组件是7段LED,我从index.css导入了css文件,但我不知道自index.css以来如何注入样式

我尝试通过值注入样式,但css语法更改且组件无法识别 我想知道是否有办法在不修改css文件的情况下传递样式

import React from 'react'
import styles from './index.css'

const Display = () =>{
  return(
    <div id="vertical-center">
        <div id="clock-container">
            <div id="display-1" className="display-container display-size-12 display-no-0">
                <div className="segment-x segment-a"><span className="segment-border"></span></div>
                <div className="segment-y segment-b"><span className="segment-border"></span></div>
                <div className="segment-y segment-c"><span className="segment-border"></span></div>
                <div className="segment-x segment-d"><span className="segment-border"></span></div>
                <div className="segment-y segment-e"><span className="segment-border"></span></div>
                <div className="segment-y segment-f"><span className="segment-border"></span></div>
                <div className="segment-x segment-g"><span className="segment-border"></span></div>
            </div>
        </div>
    </div>
  )
}

export default Display

我如何从中注入样式 index.css:

.clear {
    clear: both;
}

.display-container.display-size-12 {
    margin-bottom: 24px;
}


.display-container {
    position: relative;
    margin-right: 24px;
    float: left;
}

.display-container div.segment-x,
.display-container div.segment-y {
    position: absolute;
}

.display-container span.segment-border {
    display: block;
    position: absolute;
}

.display-container.display-size-12 {
    width: 122px;
    height: 220px;
}

.display-container.display-size-12 .segment-x {
    width: 72px;
    left: 13px;
    border-bottom: 12px solid #000;
    border-left: 12px solid transparent;
    border-right: 12px solid transparent;
}

.display-container.display-size-12 .segment-x .segment-border {
    top: 12px;
    left: -12px;
    right: -12px;
    border-top: 12px solid #000;
    border-left: 12px solid transparent;
    border-right: 12px solid transparent;
}

.display-container.display-size-12 .segment-y {
    height: 72px;
    width: 0;
    border-right: 12px solid #000;
    border-top: 12px solid transparent;
    border-bottom: 12px solid transparent;
}

.display-container.display-size-12 .segment-y .segment-border {
    position: relative;
    left: 12px;
    height: 72px;
    margin-top: -12px;
    border-left: 12px solid #000;
    border-top: 12px solid transparent;
    border-bottom: 12px solid transparent;
}

.display-container.display-size-12 .segment-a {
    top: 0;
}

.display-container.display-size-12 .segment-b {
    top: 13px;
    left: 98px;
}

.display-container.display-size-12 .segment-c {
    top: 111px;
    left: 98px;
}

.display-container.display-size-12 .segment-d {
    top: 196px;
}

.display-container.display-size-12 .segment-e {
    top: 111px;
    left: 0px;
}

.display-container.display-size-12 .segment-f {
    top: 13px;
    left: 0px;
}

.display-container.display-size-12 .segment-g {
    top: 98px;
}

.display-no-0 .segment-g {
    /*display: none;*/
    opacity: 0.1;
}

#vertical-center {
    position: absolute;
    height: 0;
    top: 50%;
    left: 50%
}

#clock-container {
    margin-top: -110px;
    margin-left: -462px;
    width: 924px;
    height: 220px;
}

3 个答案:

答案 0 :(得分:1)

使用

import './index.css'

所有样式都适用于您的组件。

答案 1 :(得分:1)

您应该将style与花括号一起使用,而不要使用className ...

答案 2 :(得分:0)

有一个小的JS框架classnames,它对于将css注入组件非常有用。

import classnames from 'classnames/bind'
import styles from './index.css'
const cx = classnames.bind(styles)

现在你可以使用cx来应用css,如下所示:

<div className={cx('divClass')}>
</div>

申请多个班级:

<div className={cx(['firstClass','secondClass'])}>
</div>