0

I am looking to center a div both vertically and horizontally in the middle of a page. Here is what I have tried and does work so far (only using pixels however)

HTML

<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="CSS/Login.css">
</head>
<body>
    <div class="center">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
        cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
        proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
</body>

CSS

.center { 
    height: 400px;
    position: relative;
    border: 3px solid green; 
}

.center p {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

This does work, however, whenever I change the 400px to 40% it won't and I don't understand why. I would really appreciate some help.

Thanks!

Omar Dajani
  • 378
  • 6
  • 16

0 Answers0