skip to Main Content

i want the canvas to fill the whole webpage under the heading, maybe with a small margin between the edge of the canvas and the edge of the window. but i can’t get it to fill the screen and now the title is next to it instead of above it. any help is appreciated

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>shark?</title>
    <style>
        html, body {
            height: 100%;
            width: 100%;
            margin: 0;
        }
        .container {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
            width: 100%;
        }
        canvas {
            background-color: dodgerblue;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>shark?</h1>
        <canvas></canvas>
    </div>
</body>
</html>

2

Answers


  1. One way to achieve this is to use flex-direction: column; (https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction) on the .container and then add a width to your canvas. The 90vw unit means set the width to "effectively" 90% the width of the viewport. Set that value to what you want.

    Also remove justify-content: center; from your .container to achieve the desired result.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>shark?</title>
        <style>
            html, body {
                height: 100%;
                width: 100%;
                margin: 0;
            }
            .container {
                display: flex;
                align-items: center;
                flex-direction: column;
                height: 100%;
                width: 100%;
            }
            canvas {
                width: 90vw;
                background-color: dodgerblue;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>shark?</h1>
            <canvas></canvas>
        </div>
    </body>
    </html>
    Login or Signup to reply.
  2. Adding the property flex-direction: column; to the container class to arrange the elements vertically.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>shark?</title>
        <style>
            html, body {
                height: 100%;
                width: 100%;
                margin: 0;
                overflow: hidden; /* Prevent scrollbars */
            }
            .container {
                display: flex;
                flex-direction: column; /* Arrange elements vertically */
                align-items: center;
                justify-content: center;
                height: 100%;
                width: 100%;
            }
            h1 {
                margin-top: 20px; /* Add margin above the heading */
            }
            canvas {
                background-color: dodgerblue;
                margin: 10px; /* Add margin around the canvas */
                flex: 1; /* Allow the canvas to fill available vertical space */
                width: calc(100% - 20px); /* Account for margin */
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>shark?</h1>
            <canvas></canvas>
        </div>
    </body>
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search