"수직 중심 맞춤은 요소가 절대적으로 배치되고 높이가 알려진 경우에만 가능합니다." -이 문장은 정확하지 않습니다.
display:inline-block;
과 그 가능성을 부모 상자 안에 세로로 정렬 할 수 있습니다. 이 기법을 사용하면 엘리먼트의 높이와 너비를 알지 못해도 요소를 정렬 할 수 있습니다.하지만 부모의 높이를 최소한으로 알고 있어야합니다.
HTML이이 경우;
<div id="container">
<div id="aligned-middle" class="inline-block">Middleman</div>
<div class="strut inline-block"> </div>
</div>
그리고 당신의 CSS는 다음과 같습니다
#container {
/* essential for alignment */
height:300px;
line-height:300px;
text-align:center;
/* decoration */
background:#eee;
}
#aligned-middle {
/* essential for alignment */
vertical-align:middle;
/* decoration */
background:#ccc;
/* perhaps, reapply inherited values, so your content is styled properly */
line-height:1.5;
text-align:left;
}
/* this block makes all the "magic", according to http://www.w3.org/TR/CSS2/visudet.html#propdef-vertical-align specification: "The baseline of an 'inline-block' is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its 'overflow' property has a computed value other than 'visible', in which case the baseline is the bottom margin edge." */
#container .strut {
/* parent's height */
height:300px;
}
.inline-block {
display:inline-block;
*display:inline;/* for IE < 8 */
*zoom:1;/* for IE < 8 */
}
그런 다음 # 정렬 - 중간 #container 내에서 중심됩니다. 이것은이 기법의 가장 간단한 사용법이지만 익숙한 좋은 방법입니다.
"/ * for IE < 8 * /"으로 표시된 규칙은 조건부 주석을 사용하여 별도의 스타일 시트에 배치해야합니다.
당신은 여기의 작업 예제를 볼 수 있습니다 http://jsfiddle.net/UXKcA/3/
편집 : (이 특정 조각 IE6 및 ff3.6 테스트,하지만이 많이 사용, 그것은 꽤 크로스 브라우저입니다 당신이 필요합니다. FF < 3에 대한 지원은 또한 .inline-block
규칙 내에서 display:inline-block;
에서 display:-moz-inline-stack;
를 추가해야합니다.) 폭과 높이가 사전에 정말 알 수없는 경우
정렬이라고 말하면 div를 정렬하려는 것과 명확하지 않습니다. 당신은 div를 중심에 두는 것을 의미합니까? –
중앙에서 div를 수직으로 정렬하려합니까? – Starx
죄송합니다. 센터 정렬을 의미합니다. –