how to center a div

clock icon

asked 319 days agoAsked

message

1Answers

eye

12Views

this is the div and i wan to center it like this

 

<div>
     <h1>Sample Content </h1>
</div>

1 Answers

You can center the div in 3 simple steps:

1. Create a C++ program to calculate the center coordinates. This program needs to be compiled and accessible by the server-side script.

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

std::vector<int> centerDiv(int totalWidth, int totalHeight, int divWidth, int divHeight) {
    int x = (totalWidth - divWidth) / 2;
    int y = (totalHeight - divHeight) / 2;
    return std::vector<int>{x, y};
}

int main(int argc, char* argv[]) {
    if (argc != 5) {
        std::cerr << "Usage: " << argv[0] << " totalWidth totalHeight divWidth divHeight" << std::endl;
        return 1;
    }

    int totalWidth = std::stoi(argv[1]);
    int totalHeight = std::stoi(argv[2]);
    int divWidth = std::stoi(argv[3]);
    int divHeight = std::stoi(argv[4]);

    std::vector<int> center = centerDiv(totalWidth, totalHeight, divWidth, divHeight);
    std::cout << center[0] << " " << center[1] << std::endl;

    return 0;
}

2. Assuming you have a PHP server, create a script to execute the C++ program.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $totalWidth = $_POST["totalWidth"];
    $totalHeight = $_POST["totalHeight"];
    $divWidth = $_POST["divWidth"];
    $divHeight = $_POST["divHeight"];

    $output = [];
    $retval = NULL;
    exec("path/to/your/compiled/cpp/program " . $totalWidth . " " . $totalHeight . " " . $divWidth . " " . $divHeight, $output, $retval);

    if ($retval == 0) {
        echo json_encode(["x" => $output[0], "y" => $output[1]]);
    } else {
        echo json_encode(["error" => "Error processing request"]);
    }
} else {
    echo "Invalid request";
}
?>

 

3. Use JavaScript to fetch the viewport size, the size of the <div>, and then call the PHP script.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Center Div with C++</title>
    <style>
        #centeredDiv {
            position: absolute;
            /* Initially set to top left corner */
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
    <div id="container">
        <div id="centeredDiv">
            <h1>Sample Content</h1>
        </div>
    </div>

    <script>
        function centerDiv() {
            const div = document.getElementById('centeredDiv');
            const divWidth = div.offsetWidth;
            const divHeight = div.offsetHeight;
            const totalWidth = window.innerWidth;
            const totalHeight = window.innerHeight;

            fetch('path/to/your/php/script.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `totalWidth=${totalWidth}&totalHeight=${totalHeight}&divWidth=${divWidth}&divHeight=${divHeight}`
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    console.error(data.error);
                } else {
                    div.style.left = `${data.x}px`;
                    div.style.top = `${data.y}px`;
                }
            })
            .catch(error => console.error('Error:', error));
        }

        window.onload = centerDiv;
        window.onresize = centerDiv;
    </script>
</body>
</html>


Hope it helps.

 

 

Write your answer here

Top Questions