5

I've converted a html file to a component is a 7 segment LED, I imported the css file from index.css, but I don't know how inject the styles since index.css

I tried to inject the styles by means of values, but the css syntax changes and the component does not recognize I want to know if there is a way to pass the styles without modifying the css file

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

How I could inject styles from this 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;
}
1
  • you shouldnt be adding the css file into a react component, bundle all the css into one file and directly add it to html file itself. Commented Aug 2, 2017 at 17:10

3 Answers 3

4

You should use style with curly braces instead of className...

Sign up to request clarification or add additional context in comments.

1 Comment

Can you add an example?
1

Use

import './index.css'

And all the styles will apply to your component.

Comments

1

There is small JS framework classnames which is very useful to inject css into components.

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

Now you can apply css using cx like below:

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

Apply multiple classes:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.