4.jsx-绑定样式.html 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="../babel.min.js"></script>
  8. <script src="../react.development.js"></script>
  9. <script src="../react-dom.development.js"></script>
  10. <style>
  11. .box{
  12. color:darkorange
  13. }
  14. /* .container{
  15. width: 500px;
  16. height: 500px;
  17. border: 1px solid #000;
  18. margin:0 auto;
  19. } */
  20. </style>
  21. </head>
  22. <body>
  23. <div id="root"></div>
  24. <!-- 绑定类名的两种方式:
  25. 1. className="类名"
  26. 2. className={'类名'}
  27. -->
  28. <script type="text/babel">
  29. // let box = <div className="box">今天晚上没有星星</div>
  30. // let box = <div className={'box'}>今天晚上没有星星</div>
  31. // 需求: 大盒子居中,小盒子通过flex布局,左右排列
  32. // 动态渲染样式
  33. // jsx中 style样式是对象格式,不能是字符串
  34. let newBox = {
  35. display:'flex',
  36. justifyContent:'space-between',
  37. alignItems:'center',
  38. width: '500px',
  39. height: '500px',
  40. border: '1px solid #000',
  41. margin:'0 auto',
  42. }
  43. let left = {
  44. width: '200px',
  45. height: '200px',
  46. // jsx中动态渲染样式时,属性名中的 - 变为驼峰命名法
  47. textAlign:'center',
  48. backgroundColor:'pink'
  49. }
  50. let box = (
  51. // <div className="container">
  52. <div style={newBox}>
  53. <div style={left}>左侧盒子</div>
  54. <div style={{
  55. width: '200px',
  56. height: '200px',
  57. textAlign:'center',
  58. backgroundColor:'skyblue'
  59. }}>右侧盒子</div>
  60. </div>
  61. )
  62. let getRoot = document.querySelector('#root')
  63. ReactDOM.createRoot(getRoot).render(box)
  64. </script>
  65. </body>
  66. </html>