使用百分比确定的元素高度怎么确定行高保证行内元素居中对齐

比如 body 高度不确定 设为百分百
div高度为body高度百分之20%
现在想要div内文本元素居中对齐,应该怎么设置呢

阅读 2.3k
2 个回答

再内嵌一个元素使用定位和变形的垂直居中方法即可

<!DOCTYPE html>
<html>

<head>

<title>demo</title>
<meta charset="utf-8">
<style>
html,
body {
    position: relative;
    height: 100%;
    width: 100%;
}

div {
    background-color: red;
    position: absolute;
    height: 20%;
    width: 20%;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
}
</style>

</head>

<body>

<div></div>

</body>

</html>

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题