html, body{height: 100%;margin: 0;}.v-wrap{height: 100%;white-space: nowrap;text-align: center;}.v-wrap:before{content: "";display: inline-block;vertical-align: middle;width: 0;/* adjust for white space between pseudo element and next sibling */margin-right: -.25em;/* stretch line height */height: 100%;}.v-box{display: inline-block;vertical-align: middle;white-space: normal;}
<div class="v-wrap"><article class="v-box"><p>This is how I've been doing it for some time</p></article></div>
<body>
<div id="all"><div class="ff-valign-wrap"><div class="ff-valign"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet, animi autem doloribus earum expedita, ipsum laboriosam nostrum nulla officiis optio quam quis quod sunt tempora tenetur veritatis vero voluptatem voluptates?</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet, animi autem doloribus earum expedita, ipsum laboriosam nostrum nulla officiis optio quam quis quod sunt tempora tenetur veritatis vero voluptatem voluptates?</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet, animi autem doloribus earum expedita, ipsum laboriosam nostrum nulla officiis optio quam quis quod sunt tempora tenetur veritatis vero voluptatem voluptates?</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet, animi autem doloribus earum expedita, ipsum laboriosam nostrum nulla officiis optio quam quis quod sunt tempora tenetur veritatis vero voluptatem voluptates?</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet, animi autem doloribus earum expedita, ipsum laboriosam nostrum nulla officiis optio quam quis quod sunt tempora tenetur veritatis vero voluptatem voluptates?</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet, animi autem doloribus earum expedita, ipsum laboriosam nostrum nulla officiis optio quam quis quod sunt tempora tenetur veritatis vero voluptatem voluptates?</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet, animi autem doloribus earum expedita, ipsum laboriosam nostrum nulla officiis optio quam quis quod sunt tempora tenetur veritatis vero voluptatem voluptates?</p></div></div></div>
</body>
/* This parent can be any width and height */.block {text-align: center;}
/* The ghost, nudged to maintain perfect centering */.block:before {content: '';display: inline-block;height: 100%;vertical-align: middle;margin-right: -0.25em; /* Adjusts for spacing */}
/* The element to be centered, canalso be of any width and height */.centered {display: inline-block;vertical-align: middle;width: 300px;}
#dialog {position:fixed; top:50%; left:50%; z-index:99991;width:300px; height:60px;margin-top:-150px; /* half of the width */margin-left:-30px; /* half of the height */}
<div class="main-div"><div class="vertical-center"><span>"Lorem ipsum dolor sit amet, consectetur adipiscing 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."</span></div></div>
/* Internet Explorer 8 and others */.main {width: 500px;margin: auto;border: solid;}html {height: 100%;width: 100%;display: table;}body {display: table-cell;vertical-align: middle;}
<!-- [if lte IE 7]><style> /* Should be in the <head> */html, body , .ie {height: 100%;text-align: center;white-space: nowrap;}.ie , .main{display: inline; /* Used with zoom in case you take a block element instead an inline element */zoom: 1;vertical-align: middle;text-align: left;white-space: normal;}</style><b class="ie"></b><!--[endif]--><div class="main"><p>Fill it up with your content </p><p><a href="https://jsfiddle.net/h8z24s5v/embedded/result/">JsFiddle versie</a></p></div>
实际上,Internet Explorer 7在这里会带来一些麻烦,因为它是唯一一个在超文本标记语言/主体元素上严格应用height: 100%的浏览器。
body {margin: 0;}
.outer-container {position: absolute;display: table;width: 100%; /* This could be ANY width */height: 100%; /* This could be ANY height */background: #ccc;}
.inner-container {display: table-cell;vertical-align: middle;}
.centered-content {display: inline-block;background: #fff;padding: 20px;border: 1px solid #000;}
<div class="outer-container"><div class="inner-container"><div class="centered-content">Malcolm in the Middle</div></div></div>
body {margin: 0;}
.outer-container {position: absolute;display: table;width: 100%; /* This could be ANY width */height: 100%; /* This could be ANY height */background: #ccc;}
.inner-container {display: table-cell;vertical-align: middle;text-align: center;}
.centered-content {display: inline-block;text-align: left;background: #fff;padding: 20px;border: 1px solid #000;}
<div class="outer-container"><div class="inner-container"><div class="centered-content">Malcolm in the Middle</div></div></div>
.wrapper {display: grid;grid-template-columns: repeat(4, 1fr);grid-gap: 10px;grid-auto-rows: 200px;grid-template-areas:". a a ."". a a .";}.item1 {grid-area: a;align-self: center;justify-self: center;}
<div style="min-height: 100vh;" class="align-items-center row"><div class="col" style="margin: auto; max-width: 750px;"> //optional style to center horizontally as well
//content goes here
</div></div>
.container {font-family: arial;font-size: 24px;margin: 25px;width: 350px;height: 200px;outline: dashed 1px black;/* Setup */position: relative;}
.child {width: 50px;height: 50px;background-color: red;/* Center vertically and horizontally */position: absolute;top: 50%;left: 50%;margin: -25px 0 0 -25px; /* Apply negative top and left margins to truly center the element */}