垂直对齐固定的div

mau*_*e77 2 html css vertical-alignment

我有以下css:

#leftmenu{
top: 400px;
left: 4%;
position: fixed;
width: 150px;
height: 200px;
background: transparent;
}

html,body{
height:100%;
margin:0;
}
Run Code Online (Sandbox Code Playgroud)

"Leftmenu"在屏幕左侧有一个固定的位置,但现在top在px(400)中定义,我想用%定义它,而div"leftmenu"必须在中心垂直对齐.高度必须相同.

谢谢

Mor*_*eus 6

将您的CSS更新为:

#leftmenu{
  top: 50%;
  margin-top: -100px; // half height of this container
}
Run Code Online (Sandbox Code Playgroud)