Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I vertically and horizontally center a div within a larger div?

Tags:

html

css

How to horizontally center a <div> in another <div>? I managed to center horizontally using styling from the accepted answer. How can I also center it vertically? The inner div is of an unknown height.

like image 441
Rayne Avatar asked Dec 12 '25 07:12

Rayne


1 Answers

From: http://www.jakpsatweb.cz/css/css-vertical-center-solution.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
  <title>Universal vertical center with CSS</title>
  <style>
    .greenBorder {border: 1px solid green;} /* just borders to see it */
  </style>
</head>

<body>
  <div class="greenBorder" style="display: table; height: 400px; #position: relative; overflow: hidden;">
    <div style=" #position: absolute; #top: 50%;display: table-cell; vertical-align: middle;">
      <div class="greenBorder" style=" #position: relative; #top: -50%">
        any text<br>
        any height<br>
        any content, for example generated from DB<br>
        everything is vertically centered
      </div>
    </div>
  </div>
</body>
</html>
like image 67
Parker Avatar answered Dec 15 '25 00:12

Parker