水平和垂直居中绝对定位的元素

im1dermike

就像标题所示,我试图将一个绝对定位的元素水平和垂直居中。根据我发现的其他帖子,是我尝试的一个缩影。目的是使红色的ALERT文本在轮廓框内居中。

这是CSS:

.parent {
  position: relative;
}
.child {
  bottom: 0;
  color: red;
  left: 0;
  margin: auto;
  position: absolute;
  right: 0;
  top: 0;
}
科斯塔斯(Kostas Mitsarakis)

尝试将绝对父级定位(您必须设置一个高度):

JSFiddle

.container {
    position: relative;
    border: 1px solid black;
}

.parent {
    position: absolute;
    bottom: 0;
    color: red;
    left: 0;
    top: 0;
    right: 0;
    width: 100%;
    height: 16px;
    margin: auto;
    text-align: center;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章