将居中内的标签
我在
标签内有以下
:<div id="AlertDiv"><h1>Yes</h1></div>
这些是它们的 CSS 类:
#AlertDiv {
position:absolute;
height: 51px;
left: 365px;
top: 198px;
width: 62px;
background-color:black;
color:white;
}
#AlertDiv h1{
margin:auto;
vertical-align:middle;
}
如何垂直和水平对齐 < h1>
位于
内?AlertDiv
将大于
。
I have the following <div>
inside a <body>
tag:
<div id="AlertDiv"><h1>Yes</h1></div>
And these are their CSS classes:
#AlertDiv {
position:absolute;
height: 51px;
left: 365px;
top: 198px;
width: 62px;
background-color:black;
color:white;
}
#AlertDiv h1{
margin:auto;
vertical-align:middle;
}
How can I vertically and horizontally align an <h1>
inside of a <div>
?
AlertDiv
will be bigger than <h1>
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
内的标签
我在 标签内有以下
:<div id="AlertDiv"><h1>Yes</h1></div>
这些是它们的 CSS 类:
#AlertDiv {
position:absolute;
height: 51px;
left: 365px;
top: 198px;
width: 62px;
background-color:black;
color:white;
}
#AlertDiv h1{
margin:auto;
vertical-align:middle;
}
如何垂直和水平对齐 < h1>
位于
内?AlertDiv
将大于
。
I have the following <div>
inside a <body>
tag:
<div id="AlertDiv"><h1>Yes</h1></div>
And these are their CSS classes:
#AlertDiv {
position:absolute;
height: 51px;
left: 365px;
top: 198px;
width: 62px;
background-color:black;
color:white;
}
#AlertDiv h1{
margin:auto;
vertical-align:middle;
}
How can I vertically and horizontally align an <h1>
inside of a <div>
?
AlertDiv
will be bigger than <h1>
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您知道
line-height:51px
只会是一行,则可以将line-height:51px
添加到#AlertDiv h1
。还要将text-align:center
添加到#AlertDiv
。下面的演示还使用负边距来保持
#AlertDiv
在两个轴上居中,即使在调整窗口大小时也是如此。演示:jsfiddle.net/KaXY5
You can add
line-height:51px
to#AlertDiv h1
if you know it's only ever going to be one line. Also addtext-align:center
to#AlertDiv
.The demo below also uses negative margins to keep the
#AlertDiv
centered on both axis, even when the window is resized.Demo: jsfiddle.net/KaXY5
在 hX 标签上
On the hX tag
有一种使用变换的新方法。将其应用于居中的元素。它将容器高度向下微移一半,然后“纠正”元素高度的一半。
大多数时候它都有效。我确实遇到了一个问题,
div
位于li
的div
中。列表项设置了高度,外部div
组成 3 列(基础)。第二列和第三列 div 包含图像,并且使用这种技术可以很好地居中,但是第一列中的标题需要一个具有明确高度设置的包装 div。现在,有人知道 CSS 人员是否正在研究一种轻松对齐内容的方法吗?看到已经是 2014 年了,甚至我的一些朋友也经常使用互联网,我想知道是否有人认为居中将是一个有用的样式功能。那么只有我们吗?
There is a new way using transforms. Apply this to the element to centre. It nudges down by half the container height and then 'corrects' by half the element height.
It works most of the time. I did have a problem where a
div
was in adiv
in ali
. The list item had a height set and the outerdiv
s made up 3 columns (Foundation). The 2nd and 3rd column divs contained images, and they centered just fine with this technique, however the heading in the first column needed a wrapping div with an explicit height set.Now, does anyone know if the CSS people are working on a way to align stuff, like, easily? Seeing that its 2014 and even some of my friends are regularly using the internet, I wondered if anyone had considered that centering would be a useful styling feature yet. Just us then?
在此处启动了jsFiddle。
看起来水平对齐与
text-align:center
一起使用。仍在尝试让垂直对齐工作;可能必须使用absolute
定位和top: 50%
或从顶部开始预先计算的padding
。Started a jsFiddle here.
It seems the horizontal alignment works with a
text-align : center
. Still trying to get the vertical align to work; might have to useabsolute
positioning and something liketop: 50%
or a pre-calculatedpadding
from the top.您可以在这里尝试代码:
http://htmledit.squarefree.com/
You can try the code here:
http://htmledit.squarefree.com/
您可以向
h1
添加填充:You could add padding to the
h1
:您可以使用
display: table-cell
将 div 渲染为表格单元格,然后像在普通表格单元格中一样使用vertical-align
。你可以在这里尝试一下:
http://jsfiddle.net/KaXY5/424/
You can use
display: table-cell
in order to render the div as a table cell and then usevertical-align
like you would do in a normal table cell.You can try it here:
http://jsfiddle.net/KaXY5/424/
text-align: center;
自 2024 年起水平居中 h1 文本div
?"),align-content: center;
将在div
? 中对齐h1
div
。请参阅 jsfiddle 和下面的代码片段:
text-align: center;
does horizontally center h1 textH1
in adiv
?"),align-content: center;
will alignh1
inside thediv
.See jsfiddle and this snippet below: