css - Is this the most efficient way of dead centering a div? -


#outer {     display:table;     position:absolute;     height:100%;     width:100%; } #middle {     display:table-cell;     vertical-align:middle; } #inner {     margin:50px auto 50px auto;     width:600px; }  <body>     <div id="outer">         <div id="middle">             <div id="inner">                 dead centered onto screen             </div>         </div>     </div> </body> 

is efficient, or accepted, way of dead centering div in middle of screen? ask because using tables tabular data, architecting interface, frowned upon.

use css transforms or flexbox.

option 1: css (2d) transforms. global support: 90.8%. trick position element 50% top , left, , offset own dimensions, half, in opposite direction. drawback of method might lead fuzzy text rendering because of subpixel translations.

body {    margin: 0;    padding: 0;  }  .wrapper {    background-color: #eee;    min-height: 100vh;    width: 100%;    position: relative;  }  .wrapper > .content {    background-color: #ddd;    padding: 10px;    position: absolute;    top: 50%;    left: 50%;    -webkit-transform: translate(-50%, -50%);    transform: translate(-50%, -50%);  }
<div class="wrapper">    <div class="content">centered</div>  </div>

option 2: css flexbox. complete global support: 82.81%. browser support little patchier compared css transforms, involves less lines. trick set both align-items , justify-content center on parent element.

body {    margin: 0;    padding: 0;  }  .wrapper {    background-color: #eee;    min-height: 100vh;    width: 100%;    display: flex;    align-items: center;    justify-content: center;  }  .wrapper > .content {    background-color: #ddd;    padding: 10px;  }
<div class="wrapper">    <div class="content">centered</div>  </div>


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -