Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 另一个(100%宽度)分区内的中心分区_Css_Xhtml - Fatal编程技术网

Css 另一个(100%宽度)分区内的中心分区

Css 另一个(100%宽度)分区内的中心分区,css,xhtml,Css,Xhtml,这是一个相当“简单”的问题,不知道为什么会如此复杂 有一个100%(宽度)大小的div 在div(大小940px宽)的中间有另一个div 有什么想法吗?:) 下面的内部div样式将使其居中 margin: 0 auto; 只需添加边距:0自动到内部分区。键是边距:0自动在内部分区上。概念验证示例: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD

这是一个相当“简单”的问题,不知道为什么会如此复杂

  • 有一个100%(宽度)大小的div
  • 在div(大小940px宽)的中间有另一个div

  • 有什么想法吗?:)

    下面的内部div样式将使其居中

    margin: 0 auto;
    

    只需添加
    边距:0自动到内部分区。

    键是边距:0自动在内部分区上。概念验证示例:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <body>
        <div style="background-color: blue; width: 100%;">
            <div style="background-color: yellow; width: 940px; margin: 0 auto;">
                Test
            </div>
        </div>
    </body>
    </html>
    
    
    试验
    
    有关详细信息,假设下面的代码将使div对齐中心:

    margin-left: auto;
    margin-right: auto;
    
    或者简单地使用:

    margin: 0 auto;
    
    但是请记住,上面的CSS代码只有在html元素上指定固定宽度(不是100%)时才有效。因此,您的问题的完整解决方案是:

    .your-inner-div {
          margin: 0 auto;
          width: 900px;
    }
    

    在internet explorer 7中不工作。。。但是谁在乎呢?

    我没有对齐文本,好像我有边距,没有工作。接受,干杯。文本对齐:居中无助于使内部div居中。但将内容(文本)居中,从而产生相同的预期结果(如果您只有文本)。要使自动边距发挥作用,还需要在内部分区上设置一个宽度。请阅读有关使用
    文本对齐将一个分区与另一个分区居中的内容。根据这一解释,
    文本对齐
    不应应用于div,但它确实适用于IE5,例如,在IE5中,
    边距
    不能正常工作。所以这是一个解决办法;作为一个副作用,它还将文本置于内部div的中心。我不知道为什么我没有注意到这一点。只需指定宽度。这是一个很好的答案,因为如果您使用边距:0自动技术,则正确的文档类型至关重要。这是一个很好的示例,因为您正确地明确指出需要在内部div上指定宽度。可能重复
    .your-inner-div {
          margin: 0 auto;
          width: 900px;
    }
    
    .outerdiv {
        margin-left: auto;
        margin-right: auto;
        display: table;
    }