本教程操作环境:windows10系统、react18.0.0版、dell g3电脑。
react 怎么设置 style属性?
react中设置样式style
1.设置行内样式:
1.基本设置:
使用{},传入一个对象{padding: '2px 0 5px 20px',overflowy: 'auto'}
如下所示:
<div style={{padding: '2px 0 5px 20px',overflowy: 'auto'}}
2.设置百分比(相对数据)
<div style={{width: 'calc(100% - 35px)',height: 'calc(100% - 15px)'}}>
3.通过函数设置:
例如,自己写一个计算window高度的函数:
//参数 adjustvalue的作用是在window.innerheight的基础上,减去多少高度,可以是负值,0,正值function getwinheight(adjustvalue) { let winheight; if (window.innerheight) { winheight = window.innerheight; } else if ((document.body) && (document.body.clientheight)) { winheight = document.body.clientheight; } return winheight-adjustvalue; }
然后在样式中使用:
<div style={{width: 'calc(100% - 35px)',height: getwinheight(200)}}> <div id="jsoneditor" classname="jsoneditor-react-container" /></div>
2.杂七杂八:
1.table占满整行:
设置table标签的style 为 style={{width: 'calc(100% - 10px)'}}
<table style={{width: 'calc(100% - 10px)'}}> <tr> <td style={{width:'50px'}}> <div style={{paddingtop:"10px",marginleft:'10px'}}> <button id="returnbuttonid" text="" icon={"ic_arrow_back"} onclick={doback} /> </div> </td> <td> <div style={{paddingtop:'10px'}}>edit parameter files</div> </td> <td> <div style={{float:'right', margin:'0 10px 0 10px', paddingtop:'10px'}}> <button id="`uploadbuttonid`" text="upload" icon={"ic_arrow_upward"} onclick={onuploadclicked} /> </div> </td> </tr></table>
2.父 <div>设置高度不起作用:
如果父<div>设置高度不管用,那么必须将子<div>的高度也设置一下,可以跟父<div>的高度保持一致,.
入下图所示:父子<div>的高度都被设置为 getwinheight(180)
<div style={{height: getwinheight(180), border:'2px'}}> <splitscreen id="parameterfiles-panel" left={ <div style={{height: getwinheight(180)}}> .......... </div> } right={ <div style={{ whitespace: "nowrap"}}> <div style={{padding: '2px 0 5px 20px',overflowy: 'auto', width: 'calc(100% - 35px)',height: getwinheight(180)}}> <div id="jsoneditor" classname="jsoneditor-react-container" /> </div> </div> } /> </div>
暂时写这么多,以后想到别的,再写。
推荐学习:《react视频教程》
以上就是react 怎么设置 style属性的详细内容。
