Search code examples
htmlcssuser-interfacelayout

make resizing layout css


I have a website and want it to resize sort of like this: resize layout from replit

anyone know how to do this?


Solution

  • i'm here trying to help. I said sorry if i false. I got reference from w3school. maybe it can help.

    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>HTML DOM - Create resizable split views</title>
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <link rel="stylesheet" href="/css/demo.css" />
            <link rel="preconnect" href="https://fonts.gstatic.com" />
            <link
                rel="stylesheet"
                href="https://fonts.googleapis.com/css2?family=Inter&family=Source+Code+Pro&display=swap"
            />
            <style>
                .container {
                    display: flex;
    
                    /* Misc */
                    border: 1px solid #cbd5e0;
                    height: 16rem;
                    width: 100%;
                }
                .container__left {
                    /* Initially, the left takes 3/4 width */
                    width: 75%;
    
                    /* Misc */
                    align-items: center;
                    display: flex;
                    justify-content: center;
                }
                .resizer {
                    background-color: #cbd5e0;
                    cursor: ew-resize;
                    height: 100%;
                    width: 2px;
                }
                .container__right {
                    /* Take the remaining width */
                    flex: 1;
    
                    /* Misc */
                    align-items: center;
                    display: flex;
                    justify-content: center;
                }
            </style>
        </head>
        <body>
            <div class="container">
                <div class="container__left">Left</div>
                <div class="resizer" id="dragMe"></div>
                <div class="container__right">Right</div>
            </div>
    
            <script>
                document.addEventListener('DOMContentLoaded', function () {
                    // Query the element
                    const resizer = document.getElementById('dragMe');
                    const leftSide = resizer.previousElementSibling;
                    const rightSide = resizer.nextElementSibling;
    
                    // The current position of mouse
                    let x = 0;
                    let y = 0;
                    let leftWidth = 0;
    
                    // Handle the mousedown event
                    // that's triggered when user drags the resizer
                    const mouseDownHandler = function (e) {
                        // Get the current mouse position
                        x = e.clientX;
                        y = e.clientY;
                        leftWidth = leftSide.getBoundingClientRect().width;
    
                        // Attach the listeners to `document`
                        document.addEventListener('mousemove', mouseMoveHandler);
                        document.addEventListener('mouseup', mouseUpHandler);
                    };
    
                    const mouseMoveHandler = function (e) {
                        // How far the mouse has been moved
                        const dx = e.clientX - x;
                        const dy = e.clientY - y;
    
                        const newLeftWidth = ((leftWidth + dx) * 100) / resizer.parentNode.getBoundingClientRect().width;
                        leftSide.style.width = `${newLeftWidth}%`;
    
                        resizer.style.cursor = 'col-resize';
                        document.body.style.cursor = 'col-resize';
    
                        leftSide.style.userSelect = 'none';
                        leftSide.style.pointerEvents = 'none';
    
                        rightSide.style.userSelect = 'none';
                        rightSide.style.pointerEvents = 'none';
                    };
    
                    const mouseUpHandler = function () {
                        resizer.style.removeProperty('cursor');
                        document.body.style.removeProperty('cursor');
    
                        leftSide.style.removeProperty('user-select');
                        leftSide.style.removeProperty('pointer-events');
    
                        rightSide.style.removeProperty('user-select');
                        rightSide.style.removeProperty('pointer-events');
    
                        // Remove the handlers of `mousemove` and `mouseup`
                        document.removeEventListener('mousemove', mouseMoveHandler);
                        document.removeEventListener('mouseup', mouseUpHandler);
                    };
    
                    // Attach the handler
                    resizer.addEventListener('mousedown', mouseDownHandler);
                });
            </script>
        </body>
    </html>