Javascript 如何为React应用程序定义CSS规则?
我是一个新的内联样式在一个小项目。CSS库Bulma有助于在我的应用程序上获得一些美感。请问如何在React/JSX应用程序中定义多个CSS规则Javascript 如何为React应用程序定义CSS规则?,javascript,css,reactjs,Javascript,Css,Reactjs,我是一个新的内联样式在一个小项目。CSS库Bulma有助于在我的应用程序上获得一些美感。请问如何在React/JSX应用程序中定义多个CSS规则 const viewTemplate = ( <div className="box" style={{width:"500px", margin-left:"auto", margin-top:"50px", margin-right:"a
const viewTemplate = (
<div className="box" style={{width:"500px", margin-left:"auto", margin-top:"50px", margin-right:"auto"}}>
<article className="c-media">
<div className="media-content">
{/*Content*/}
<div class="content">
{/* To DO List Item*/}
<label className="todo-label" htmlFor={props.id}>
<strong style={{ fontWeight: props.important ? 'bold' : 'normal' , fontSize: "26px"}}>{props.name}</strong>
</label>
{/* Date, Time & Important Tag*/}
<div>
<small>04.01.2021,</small><small>17:00 Uhr</small>
<span class="tag is-danger is-normal">IMPORTANT</span>
</div>
<div style="margin-top: 20px;">
This is your first To Do Task. It is very important.
</div>
</div>
{/* Buttons */}
<div className="buttons">
<button
type="button"
className="button is-edit"
onClick={()=> setEditing(true)}
>
Edit <span className="visually-hidden">{props.name}</span>
</button>
<button
type="button"
className="button is-danger"
onClick={() => props.deleteTask(props.id)}
>
Delete <span className="visually-hidden">{props.name}</span>
</button>
</div>
</div>
</article>
);
你在找这样的东西吗 这是你第一次完成任务。这是非常重要的。
您可以使用
@push('styles')
<style>
</style>
@endpush
或
我建议您将单独的样式文件导入索引中。应该避免内联样式:为组件提供ID和类名,然后在单独的文件中声明CSS以及Sass等。您始终可以在App.css中导入这些内容或为每个组件创建索引。如果您使用的是Bulma,我猜您已经在App.css中导入了它的Sass文件。只需更改文件夹并对组件执行相同操作,添加:
@import 'path/to/your/component/style.scss'
在App.css中。相应地更改此行以导入CSS。IMHO,创建一个包含索引的单个组件文件夹可以更好地导入其所有文件。在jsx中使用内联样式,如:
<some tag style={{property1 : 'value1', property2 : 'value2'}}/>
或者在js中使用内部样式变量,如:
<some tag style={style1}/>
let style1={
property1: 'value1',
property2: 'value2',
property3: 'value3',}
如果要向React内联样式中添加一些变量,应执行以下操作:
非常感谢。错误:样式属性需要从样式属性到值的映射,而不是字符串。例如,当使用JSX时,style={{marginRight:spating+'em'}}。@pythonbuddha对于您提到的这个问题,您可以使用字符串插值。比如:style={{marginRight:`${spating}em`}}是的,您可以使用模板字符串谢谢您的回答:我听从了您的建议,但发生了以下错误:./src/index.css模块构建失败:错误:enoint:没有这样的文件或目录,从同一目录打开“C:\Users\pythonbuddha\Desktop\Projects\to do list\src\index.css”它应该是@import./index.css”:AFAIK,App.css在src下/
<some tag style={style1}/>
let style1={
property1: 'value1',
property2: 'value2',
property3: 'value3',}