我有一个div设置显示:块(90px高和宽),我有一些文本在里面。
我需要文本在中心垂直和水平对齐。
我已经尝试了text-align:center,但它不做垂直居中部分,所以我尝试了vertical-align:middle,但它不起作用。
什么好主意吗?
我有一个div设置显示:块(90px高和宽),我有一些文本在里面。
我需要文本在中心垂直和水平对齐。
我已经尝试了text-align:center,但它不做垂直居中部分,所以我尝试了vertical-align:middle,但它不起作用。
什么好主意吗?
当前回答
添加行显示:table-cell;到该div的CSS内容。
只有表格单元格支持vertical-align: middle;,但是你可以把[table-cell]定义给div…
一个活生生的例子在这里:http://jsfiddle.net/tH2cc/
div{
height: 90px;
width: 90px;
text-align: center;
border: 1px solid silver;
display: table-cell; /* This says treat this element like a table cell */
vertical-align:middle; /* Now we can center vertically like in a TD */
}
其他回答
div { 身高:90 px; 行高:90 px; text-align:中心; 边界:2px虚线#f69c55; } < div > 你好,世界! ! < / div >
截至2014年的常见技术:
方法1 -转换translateX/translateY: 这里/全屏示例 在受支持的浏览器中(大多数),您可以使用top: 50%/left: 50%结合translateX(-50%) translateY(-50%)动态地将元素垂直/水平居中。 .container { 位置:绝对的; 上图:50%; 左:50%; translateX(-50%) translateY(-50%); }
方法2 - Flexbox方法: 这里/全屏示例 在受支持的浏览器中,将目标元素的显示设置为flex,并使用align-items: center用于垂直居中,使用justify-content: center用于水平居中。不要忘记为额外的浏览器支持添加供应商前缀(参见示例)。 Html, body, .container { 高度:100%; } .container { 显示:flex; 对齐项目:中心; justify-content:中心; }
Approach 3 - table-cell/vertical-align: middle: Example Here / Full Screen Example In some cases, you will need to ensure that the html/body element's height is set to 100%. For vertical alignment, set the parent element's width/height to 100% and add display: table. Then for the child element, change the display to table-cell and add vertical-align: middle. For horizontal centering, you could either add text-align: center to center the text and any other inline children elements. Alternatively, you could use margin: 0 auto assuming the element is block level. html, body { height: 100%; } .parent { width: 100%; height: 100%; display: table; text-align: center; } .parent > .child { display: table-cell; vertical-align: middle; }
Approach 4 - Absolutely positioned 50% from the top with displacement: Example Here / Full Screen Example This approach assumes that the text has a known height - in this instance, 18px. Just absolutely position the element 50% from the top, relative to the parent element. Use a negative margin-top value that is half of the element's known height, in this case - -9px. html, body, .container { height: 100%; } .container { position: relative; text-align: center; } .container > p { position: absolute; top: 50%; left: 0; right: 0; margin-top: -9px; }
Approach 5 - The line-height method (Least flexible - not suggested): Example Here In some cases, the parent element will have a fixed height. For vertical centering, all you have to do is set a line-height value on the child element equal to the fixed height of the parent element. Though this solution will work in some cases, it's worth noting that it won't work when there are multiple lines of text - like this. .parent { height: 200px; width: 400px; text-align: center; } .parent > .child { line-height: 200px; }
方法4和5不是最可靠的。选前三个中的一个。
将这个CSS类赋给目标<div>:
.centered { 宽度:150 px; 身高:150 px; 显示:flex; 对齐项目:中心; justify-content:中心; text-align:中心; 背景:红色;/*不需要只是为了清楚地看到结果*/ } <div class="居中">这个文本水平和垂直居中</div>
在父div中添加以下代码
display: grid;
place-items: center;
调整线高度以获得垂直对齐。
line-height: 90px;