"; 2. Through "height: 'calc(100 % - 15px)'" to set the percentage; 3. Set the properties by using the function "getWinHeight(200)" in the style."/> "; 2. Through "height: 'calc(100 % - 15px)'" to set the percentage; 3. Set the properties by using the function "getWinHeight(200)" in the style.">
Home >Web Front-end >Front-end Q&A >How to set style attribute in react
How to set the style attribute in react: 1. Set the inline style through "e3754c9bb53d3105ab9ea988454fc2bf"; 2. Set the percentage through "height: 'calc(100% - 15px)'"; 3. Set the properties by using the function "getWinHeight(200)" in the style.
The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.
How to set the style attribute in react?
Setting style style in React
1. Set inline style:
1. Basic settings:
Use {}, pass in an object {padding: '2px 0 5px 20px', overflowY: 'auto'}
As shown below:
<div style={{padding: '2px 0 5px 20px',overflowY: 'auto'}}
2. Set percentage (relative data)
<div style={{width: 'calc(100% - 35px)',height: 'calc(100% - 15px)'}}>
3. Set by function:
For example, write a function to calculate the height of the 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; }
Then use it in the style:
<div style={{width: 'calc(100% - 35px)',height: getWinHeight(200)}}> <div id="jsoneditor" className="jsoneditor-react-container" /> </div>
2. Miscellaneous:
1. The table occupies the entire row:
Set the style of the table tag to 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. Setting the height of the parent dc6dce4a544fdca2df29d5ac0ea9906b does not work:
If the height setting of the parent dc6dce4a544fdca2df29d5ac0ea9906b does not work, then the height of the child dc6dce4a544fdca2df29d5ac0ea9906b must also be set, which can be done with the parent < The height of ;div> remains the same.
As shown in the figure below: the height of parent and childdc6dce4a544fdca2df29d5ac0ea9906b is set to 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>
Writing so much for the time being, I will think of it later Otherwise, write again.
Recommended learning: "react video tutorial"
The above is the detailed content of How to set style attribute in react. For more information, please follow other related articles on the PHP Chinese website!