CSS:无法将div居中

CSS:无法将div居中,css,Css,“我的标记”页面包含以下2个元素: html { width: 100%; height: 100%; } Body { height: 100%; width: 100%; } div#bg { height:320px; width:850px; margin: auto; background-color: blue; } #black { position: absolute; background-colo

“我的标记”页面包含以下2个元素:

html {
    width: 100%;
    height: 100%;
}
Body {
    height: 100%;
    width: 100%;
}
div#bg {
    height:320px;
    width:850px;
    margin: auto;
    background-color: blue;
}
#black {
    position: absolute;
    background-color: black;
    width:33%;
    height:13%;
    margin:90px auto 0;
}

...

<ul id="black"></ul>
<div id="bg"></div>
html{
宽度:100%;
身高:100%;
}
身体{
身高:100%;
宽度:100%;
}
部门背景{
高度:320px;
宽度:850px;
保证金:自动;
背景颜色:蓝色;
}
#黑色的{
位置:绝对位置;
背景色:黑色;
宽度:33%;
身高:13%;
利润率:90像素自动0;
}
...

    我想把
    black
    放在
    bg
    的内部,中间距顶部边缘13%。不幸的是,我无法将
    黑色
    居中!我得到的是:

    有谁能告诉我如何将
    black
    置于
    bg
    的中心位置?
    谢谢

    更改
    边距:90px自动0
    保证金:90px 33.5%0


    当您使用
    position:absolute
    时,
    margin:auto
    不再满足您的需求。我将容器的宽度除以2(33%/2),然后从现在的相对左侧位置(50%)中减去它,即50%-16.5%=33.5%。

    当您尝试将绝对定位元素居中时,此技巧就可以实现

    left: 50%;
    top: 50%
    margin-top:-(half of the height);
    margin-left: -(half of the width);
    
    CSS


    我会将UL放在DIV中,然后像这样计算:


    在UL上将左右值设置为%,而不是设置宽度。

    快速提问。如果您希望无序列表显示在div中,为什么不将其实际放置在div中,如下所示:
      ?由于绝对定位,边距的行为与您预期的不一样。你的小提琴成绩不在中间,那是黑色的谢谢你,萨钦。“黑色”不是垂直居中的,当我擦除“高度”时,它就消失了。顺便说一句,在背景DIV之外也是一样的。你的解决方案没有我希望的那么复杂,使用“自动”来实现居中,但这是唯一有效的解决方案。。。
      #black 
      {
       position: absolute;
       background-color: black;
       width:33%;
       height:13%;
       margin-top:-7%;
       margin-left:-17%;
       left:50%;
       top:50%;
      }