Html 使用Css将长方体居中

Html 使用Css将长方体居中,html,css,twitter-bootstrap,flexbox,centering,Html,Css,Twitter Bootstrap,Flexbox,Centering,我好像不能把这个条放在这页的中间 我的代码 #菜单标题{ 背景色:#009376; 垫面:3件; 右侧填充:3px; 垫底:2件; 左:3倍; 宽度:900px; 显示器:flex; 自对准:居中; }  鸡尾酒 请找到下面的代码,将其置于中间,您只需提供div的高度和宽度 #navbar-header { position: relative; } #menu-header{ background-color:#009376; padding-top:3px;

我好像不能把这个条放在这页的中间

我的代码

#菜单标题{
背景色:#009376;
垫面:3件;
右侧填充:3px;
垫底:2件;
左:3倍;
宽度:900px;
显示器:flex;
自对准:居中;
}



 鸡尾酒
请找到下面的代码,将其置于中间,您只需提供div的高度和宽度

#navbar-header {
    position: relative;
}
#menu-header{
  background-color:#009376;
  padding-top:3px;
  padding-right:3px;
  padding-bottom:2px;
  padding-left:3px;
  width: 900px;
  height: 400px;
  display: flex;
  align-self: center;
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  margin: auto;
}

我想你最好用-

。菜单标题父项{
显示器:flex;
证明内容:中心;
}
#菜单标题{
背景色:#009376;
垫面:3件;
右侧填充:3px;
垫底:2件;
左:3倍;
宽度:900px;
显示:块;
}



 鸡尾酒
如果要在屏幕上垂直居中显示div,可以使用定位或边距顶部

单位vh用于获取垂直屏幕高度

例如:

页边距顶部方法

#menu-header{
 background-color:#009376;
 padding-top:3px;
 padding-right:3px;
 padding-bottom:2px;
 padding-left:3px;
 width: 900px;
 display: flex;
 align-self: center;
 margin-top: calc( 50vh - 33px );
}
但是,在这种方法中,最终很可能会浪费顶部空间

定位

#menu-header{
 background-color:#009376;
 padding-top:3px;
 padding-right:3px;
 padding-bottom:2px;
 padding-left:3px;
 width: 900px;
 display: flex;
 align-self: center;
 position: absolute;
 top: calc( 50vh - 33px );
}
这里div被定位为绝对值,并给出一个最大值


在这两种解决方案中,我都是通过计算屏幕实际高度的一半,然后减去div高度的一半来计算top/margin top value

@Ibo,如果这是您要寻找的答案,请务必将其标记为答案。不幸的是,这不是我的朋友,但感谢您抽出时间来回答help@Ibo它会起作用的。。我时不时地用它。。总是。。你能给我一个你的Q的现场版本吗?你说的Q是什么意思?@Ibo Q=Question,我的意思是你的问题的现场样本。!它解决了你的问题吗?不,很遗憾,没有。谢谢。你能详细说明你的问题吗?我想把我创建的条居中,不幸的是,代码片段工具没有产生我这里的东西,我用其他Css工具(如填充)修复了这个问题
#menu-header{
 background-color:#009376;
 padding-top:3px;
 padding-right:3px;
 padding-bottom:2px;
 padding-left:3px;
 width: 900px;
 display: flex;
 align-self: center;
 position: absolute;
 top: calc( 50vh - 33px );
}