How to set the style attribute in react: 1. Set the inline style through "
"; 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'}}Copy after login2. Set percentage (relative data)
<div style={{width: 'calc(100% - 35px)',height: 'calc(100% - 15px)'}}>Copy after login3. 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; }Copy after loginThen use it in the style:
<div style={{width: 'calc(100% - 35px)',height: getWinHeight(200)}}> <div id="jsoneditor" className="jsoneditor-react-container" /> </div>Copy after login2. 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>Copy after login2. Setting the height of the parent
does not work:If the height setting of the parent
does not work, then the height of the childmust 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 child
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>Copy after loginWriting 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!
Related labels:source:php.cnPrevious article:What to do if react null error is reported Next article:How to convert timestamp to date in reactStatement of this WebsiteThe content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cnLatest Articles by Author
2023-04-24 11:00:01 2023-04-24 10:55:51 2023-04-24 10:52:44 2023-04-23 17:40:51 2023-04-23 17:38:02 2023-04-23 17:34:02 2023-04-23 10:15:45 2023-04-23 10:10:52 2023-04-21 16:01:59 2023-04-21 15:58:01Latest IssuesPopular RecommendationsPopular TutorialsMore>
JAVA Beginner's Video Tutorial2512255 Latest DownloadsMore>