垂直居中line-height要设置和height一样高
代码:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>单行文字垂直居中</title><style>div {width: 200px;height: 40px;background-color: pink;/* 垂直居中line-height要设置和height一样高 */line-height: 40px;padding:0px;}</style></head><body><div>我要居中</div></body></html>