简体   繁体   English

Div不能与margin:auto对齐

[英]Div doesn't align to center with margin:auto

I'm trying to center a div between two others following the second answer to this question . 这个问题的第二个答案之后,我试图将div居中。 However, it doesn't work and, inspecting my page's elements, I found out the center div's padding is taking up the whole screen's width, as if it were containing the two other divs. 但是,它不起作用,并且在检查页面元素时,我发现中心div的填充占据了整个屏幕的宽度,就好像它包含了其他两个div一样。 I solved that by setting display:inline-block. 我通过设置display:inline-block解决了这一问题。 The padding now is correct, but the div is still not centered. 现在填充是正确的,但div仍未居中。 Did I forget something? 我忘记了什么吗? Is the inline-block option incorrect, since it wasn't present in the answer to which I linked? 内联阻止选项是否不正确,因为我所链接的答案中没有该选项? Code follows. 代码如下。

HTML: HTML:

<div class="menu" id="left-menu">
<div class="menu" id="right-menu">
<div class="main-col">
    <canvas id="my-canvas" width="800" height="600"></canvas>
/div>

CSS: CSS:

#left-menu
{
float:left;
margin-right:15px; /*won't be needed if the center div is well positioned*/
}
#right-menu
{
float:right;
margin-left:15px; /*won't be needed if the center div is well positioned*/
}
#main-col
{
margin-left: auto;
margin-right: auto;
    display:inline-block;
}

display:inline-block更改为display:block或将text-align:center添加到#main-col容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM