2013-02-20 53 views
0

我有2個div應用最大寬度代理他們,因爲我想。一個最大寬度爲900px,而另一個爲820px,如果窗口尺寸變小,它們會縮小。中心格與最大寬度

<div id="componentWrapper"> 
    <div class="thumbContainer"></div> 
</div> 

#componentWrapper{ 
    position:absolute; 
    height:190px; 
    width:100%; 
    max-width:500px; 
    top:50%; 
    background:red; 
    margin-top:-85px; 
} 

#componentWrapper .thumbContainer{ 
    top:0px; 
    left:0px; 
    height:180px; 
    max-width:420px; 
    margin-left:40px; 
    margin-right:40px; 
    background:green; 
    overflow:hidden; 
} 

http://jsfiddle.net/DzgQ3/1/

我想現在純CSS如果可能的話這個中心裏面的窗口。即使窗口的大小小於外部元素的當前寬度,它也需要保持居中。

+0

似乎按照您爲我描述的方式工作,或者我錯過了什麼? – byronyasgur 2013-02-20 20:04:50

+0

在FF中似乎沒問題。 – 2013-02-20 20:05:15

回答

1

margin:auto;通常適用於這樣的事情。

+0

佈局的工作原理,但我的問題是如何將窗戶外的div集中在一起。只有當窗口大小大於外部div時,才能使用邊距,一旦窗口大小小於外部div的原始大小,邊距不會再使它居中。 – Toniq 2013-02-21 00:49:50

+0

我想我現在看到你在嘗試什麼,我不認爲在使用position:absolute時有什麼辦法。 這是你所需要的? #組件包裝器{float:top; margin:auto; height:190px; 寬度:100%; max-width:500px; 背景:紅色; margin-top:30%; } – 2013-02-23 08:47:37