CSS - 中心大div里面较小的一个+隐藏溢出

时间:2012-06-02 10:46:36

标签: html css overflow positioning

http://jsfiddle.net/vT65S/

html:

<div class="container">
    <div class="wide1">test</div>
</div>

<div class="container">
    <div class="wide2">test</div>
</div>

<div class="container">
    <div class="wide3">test</div>
</div> 

的CSS:

.container { width: 100%; text-align: center; margin: 20px 0; overflow: hidden; }
.container div { background: red; margin: 0 auto; }

.wide1 { width: 100px; }
.wide2 { width: 600px; } 
.wide3 { width: 1100px; } 

我想在一条垂直线(居中)中进行所有“测试”。我需要在我不知道内部div的宽度的情况下使用它。

3 个答案:

答案 0 :(得分:2)

以什么为中心?外容器div?您必须使用max-width: 100%之类的内容覆盖内部div的宽度。

示例:http://jsfiddle.net/vT65S/1/

.container div { background: red; margin: 0 auto; max-width: 100% }

答案 1 :(得分:1)

知道了!但是使用JavaScript ... http://jsfiddle.net/vT65S/10/

jQuery(function($){
    $(".container > *").each(function(){
       var m = ($(this).parent().innerWidth() - $(this).outerWidth())/2;
       $(this).css("margin-left", m);
    });
});

也许有一个纯CSS(?)

的解决方案

答案 2 :(得分:1)

遇到同样的问题 - 用CSS3 translateX

解决了
.container div{
    position: relative;
    left: 50%;
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
}
相关问题