Html 如何使div元素中的文本居中?
我正在尝试创建正方形元素,它将使文本垂直和水平居中。此外,广场的整个区域应该是一个链接。这是我的HTML:Html 如何使div元素中的文本居中?,html,css,Html,Css,我正在尝试创建正方形元素,它将使文本垂直和水平居中。此外,广场的整个区域应该是一个链接。这是我的HTML: <div class="w1h1 medium"> <a class="userLink" target="_blank" href="Fancybox.aspx"> <table style="width: 100%; height: 100%"> <tr style="vertical-align
<div class="w1h1 medium">
<a class="userLink" target="_blank" href="Fancybox.aspx">
<table style="width: 100%; height: 100%">
<tr style="vertical-align: central">
<td style="text-align: center; font-weight: bold;">
text in the middle
</td>
</tr>
</table>
</a>
</div>
它适用于Chrome和Firefox,但不适用于Internet Explorer。在伊江,文本是在广场的顶部,而不是在中间。你能帮我吗
我刚刚在这里创建了游乐场:将
更改为
并将属性显示更改为内联块或内联
试试我的技巧;照此
.outer{ float:left; width:100px; height:100px; background-color:#ccc; }
.innet{ float:left; width:100%; line-height:100px; text-align:center; }
<div class="outer">
<span class="inner">This is my text</span>
</div>
.outer{float:left;宽度:100px;高度:100px;背景色:#ccc;}
.innet{float:left;宽度:100%;行高:100px;文本对齐:居中;}
这是我的文本
莫菲斯好吧!;) 尝试使用:行高:150px
设置框内内容的高度。如果只有一行内容,这应该很有效。在
中使用valign=“middle”
例如:
<td style="text-align: center; font-weight: bold;" valign="middle">
text in the middle
</td>
中间文字
您可以稍微简化您的结构,并在a
元素上使用display:table cell
html
<div class="w1h1 medium">
<a class="userLink" target="_blank" href="Fancybox.aspx">
text in the middle
</a>
</div>
演示在
工作到IE8试试这个:
HTML:
还有一个
请注意,它只允许一行文本。。。这是个问题吗
编辑,找到更好的解决方案,将定位点显示为表格单元格,也可以使用多行:
a {
display: table-cell;
text-align: center;
vertical-align: middle;
width: 150px;
height: 150px;
background-color: #06849b;
color: white;
font-family: sans-serif;
text-decoration: none;
font-weight: bold;
}
获得完全居中文本的一个好方法是使用flexbox布局。您可以使用很少的代码将容器元素的内容水平和垂直居中:
.container-with-centered-content {
display: flex;
align-items: center;
justify-content: center;
}
演示:将
更改为
已经尝试过了。这并没有改变文字的位置,就是这样<代码>显示
CSS
<a href="#">Text here</a>
a {
display: block;
width: 150px;
height: 150px;
background-color: #06849b;
color: white;
font-family: sans-serif;
vertical-align: middle;
line-height: 150px;
text-align: center;
text-decoration: none;
font-weight: bold;
}
a {
display: table-cell;
text-align: center;
vertical-align: middle;
width: 150px;
height: 150px;
background-color: #06849b;
color: white;
font-family: sans-serif;
text-decoration: none;
font-weight: bold;
}
.container-with-centered-content {
display: flex;
align-items: center;
justify-content: center;
}