Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center div on the middle of screen [duplicate]

What is the best pattern to align a semantic ui grid in the middle of the screen?

the css for this will ideal be this one.

.div{     position: absolute;     top: 50%;     left: 50%;     margin-top: -50px;     margin-left: -50px;     width: 100px;     height: 100px; } 

But on semantic this dont look well with grids.

This is part of my html.

 <div class="ui grid container">     <div class="ui center aligned three column grid">       <div class="column">       </div>       <div class="column">         </div>       </div>   </div> 
like image 276
carloss medranoo Avatar asked Jul 04 '15 05:07

carloss medranoo


2 Answers

This should work with any div or screen size:

.center-screen {   display: flex;   justify-content: center;   align-items: center;   text-align: center;   min-height: 100vh; }
 <html>  <head>  </head>  <body>  <div class="center-screen">  I'm in the center  </div>  </body>  </html>

See more details about flex here. This should work on most of the browsers, see compatibility matrix here.

Update: If you don't want the scroll bar, make min-height smaller, for example min-height: 95vh;

like image 100
Caner Avatar answered Sep 19 '22 18:09

Caner


2018: CSS3

div{     position: absolute;     top: 50%;     left: 50%;     margin-right: -50%;     transform: translate(-50%, -50%); } 

This is even shorter. For more information see this: CSS: Centering Things

like image 29
Nixen85 Avatar answered Sep 16 '22 18:09

Nixen85