利用css属性实现元素的垂直水平居中
- 元素有固定的高度
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#app{
width: 400px;
height: 200px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
</style>
</head>
<body>
<form id="app">
<fieldset>
<legend>健康信息</legend>
身高:<input type="text" />
体重:<input type="text" />
</fieldset>
</form>
</body>
</html>
给需要垂直水平居中的元素给一个固定的高宽,然后将元素设置为绝对定位,再将绝对定位的四个属性都设置为0,再给加一个margin属性,值为auto,这样网页就会给元素自动增加一个margin值,这样就能实现了元素的垂直水平居中
2. 利用flex布局方式
#app{
height: 400px;/*需要给父元素固定的高度*/
display: flex;/*将父元素元素的类型设置为flex*/
justify-content: center;/*使元素水平居中*/
align-items: center;/*使元素垂直居中*/
}