screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script> <script src="https://unpkg.com/@babel/standalone@7.14.7/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useRef, useEffect } = React; function App() { const myElementRef = useRef(); const [height, setHeight] = useState(""); const [width, setWidth] = useState(""); useEffect(() => { const element = myElementRef.current; const width = element.offsetWidth; const height = element.offsetHeight; setHeight(height); setWidth(width); }, []); return ( <div> <h3>React Js Get Element Height and Width</h3> <div ref={myElementRef} style={{ border: "1px solid #ccc" }}> This is my element. <p>Height of Element: {height}</p> <p>Width of Element: {width}</p> </div> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> div { display: flex; flex-direction: column; align-items: center; } h3 { font-size: 1.5rem; margin-bottom: 1rem; } div[style] { position: relative; } p { font-size: 1rem; margin: 0.5rem 0; } </style> </body> </html>