4.jsx-style属性.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>jsx-行内样式</title>
  8. <script src="../babel.min.js"></script>
  9. <script src="../react.development.js"></script>
  10. <script src="../react-dom.development.js"></script>
  11. <style>
  12. .red {
  13. color: red;
  14. }
  15. .blue {
  16. color: blue;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="root"></div>
  22. <script type="text/babel">
  23. // style属性 在jsx中不能是string类型。必须是 对象object。
  24. let styles = {
  25. width: '500px',
  26. height: '500px',
  27. border: '1px solid red',
  28. margin: '0 auto',
  29. display: 'flex',
  30. justifyContent: 'space-around',
  31. alignItems: 'center',
  32. flexWrap: 'nowrap',
  33. };
  34. let element = (
  35. <div style={styles}>
  36. <div
  37. style={{
  38. width: '200px',
  39. height: '200px',
  40. backgroundColor: 'red',
  41. }}
  42. ></div>
  43. <div
  44. style={{
  45. width: '200px',
  46. height: '200px',
  47. backgroundColor: 'blue',
  48. }}
  49. >
  50. <div className={'red'}>我是最牛X的</div>
  51. </div>
  52. </div>
  53. );
  54. ReactDOM.createRoot(document.getElementById('root')).render(element);
  55. </script>
  56. </body>
  57. </html>