<div class="outer"><div class="inner">I'm always centered<br/>doesn't matter how much text, height or width i have.<br/>The dimensions or my parent are irrelevant as well</div></div>
.container {position: relative;}
.container .cat-link {position: absolute;left: 50%;top: 50%;transform: translate3d(-50%,-50%,0);z-index: 100;text-transform: uppercase; /* Forces CSS to treat this as text, not a texture, so no more blurry bugs */background-color: white;}
.color-block {height: 250px;width: 100%;background-color: green;}
<div id="container">hello world hello worldhello world hello worldhello world hello worldhello world hello worldhello world hello worldhello world hello worldhello world hello worldhello world hello world
<div id="centered"><div>hello world <br/>I don't know my width<br/>but I'm still absolute!</div></div></div>
.container {position: relative; /* Important */width: 200px; /* Any width */height: 200px; /* Any height */background: red;}
元素(div)也可以是任何大小(必须是比容器小):
.center {position: absolute; /* Important */top: 50%; /* Position Y halfway in */left: 50%; /* Position X halfway in */transform: translate(-50%,-50%); /* Move it halfway back(x,y) */width: 100px; /* Any width */height: 100px; /* Any height */background: blue;}