2014-10-28 42 views
0

我試圖讓兩個DIV並排放置在一個較小的DIV內:overflow; hidden; 我希望這兩個DIV是並排的,並且它們的一些內容由於其寬度較小的父DIV而導致隱藏。相反,最後一個DIV在其他DIV下移動,即使它們都浮動:left ;.兩個DIV並排在一個較小的DIV

HTML:

<div id="wrapper"> <!-- wrapper --> 
    <div id="nav"> <!-- nav --> 

    </div> <!-- /nav --> 
    <div id="container"> <!-- container --> 

    </div> <!-- /container --> 
</div> <!-- /wrapper --> 

CSS:

#wrapper { 
    float:left; 
    height:960px; 
    width:540px; 
    background-color:#eeeeee; 
    overflow:hidden; 
} 

#nav { 
    float:left; 
    width:460px; 
    height:960px; 
    background-color:red; 
} 

#container { 
    float:left; 
    width:540px; 
    height:960px; 
    background-color:green; 
} 

這是我想要的效果:

Mobile UI example

+0

是否#NAV-DIV總是具有相同的寬度? – RMo 2014-10-28 22:18:02

+0

不,它會和#wrapper具有相同的寬度-60px – 2014-10-28 22:19:21

+0

我發佈了一張圖片和我想要的結果。 – 2014-10-28 22:28:48

回答

1

如果你想在兩個元素並排的一面,我想你必須添加某種附加的「包裝」元素環繞你#nav#container元素。 (溢出設置爲auto,這樣你可以看到的元素是彼此相鄰。)

例子:

#wrapper { 
 
    float: left; 
 
    height: 960px; 
 
    width: 540px; 
 
    background-color: #eeeeee; 
 
    overflow: auto; 
 
} 
 
#wrap { 
 
    width: 1000px; 
 
} 
 
#nav { 
 
    float: left; 
 
    width: 460px; 
 
    height: 960px; 
 
    background-color: red; 
 
    clear: 
 
} 
 
#container { 
 
    float: left; 
 
    width: 540px; 
 
    height: 960px; 
 
    background-color: green; 
 
}
<div id="wrapper"> 
 
    <!-- wrapper --> 
 
    <div id="wrap"> 
 
    <!-- some new wrapping element --> 
 
    <div id="nav"> 
 
     <!-- nav --> 
 

 
    </div> 
 
    <!-- /nav --> 
 
    <div id="container"> 
 
     <!-- container --> 
 

 
    </div> 
 
    <!-- /container --> 
 
    </div> 
 
</div> 
 
<!-- /wrapper -->

+0

有點兒,現在#container一直移動到左側,在#nav後面..:/ – 2014-10-28 22:17:26

+0

我發佈了一張圖片,其結果是我想要的。 – 2014-10-28 22:30:09

+0

我更新了我的答案,基於我認爲你正在尋找的東西 - 希望這有助於! – wahwahwah 2014-10-28 23:00:47

1

我建議防止纏繞,對孩子使用display: inline-block<div>元素,並使用white-space: nowrap;在容器上:

#wrapper { 
 
    float: left; 
 
    height: 300px; 
 
    width: 300px; 
 
    background-color: #eeeeee; 
 
    overflow: hidden; 
 
    /* prevents content wrapping to a new line */ 
 
    white-space: nowrap; 
 
} 
 
#nav { 
 
    display: inline-block; 
 
    width: 400px; 
 
    height: 960px; 
 
    background-color: red; 
 
    /* purely to move the first element to the left, 
 
    to show that both elements are on the same line: */ 
 
    margin-left: -200px; 
 
} 
 
#container { 
 
    display: inline-block; 
 
    width: 400px; 
 
    height: 960px; 
 
    background-color: green; 
 
}
<div id="wrapper"> 
 
    <!-- wrapper --> 
 
    <div id="nav"> 
 
    <!-- nav --> 
 

 
    </div> 
 
    <!-- /nav --> 
 
    <div id="container"> 
 
    <!-- container --> 
 

 
    </div> 
 
    <!-- /container --> 
 
</div> 
 
<!-- /wrapper -->

+0

謝謝,但這似乎無法解決我的問題,我沒有提到的是我希望這可以在任何手機瀏覽器中工作,所以容器的寬度和高度是根據屏幕的大小調整的JavaScript和當顯示#nav時,它希望它將#container DIV推到一邊。 – 2014-10-28 22:05:26

+0

是;如果你把兩件東西放在一起,那會發生。那麼,只需使用'margin-left'將'#nav'元素向左移動即可? – 2014-10-28 22:07:06

+0

我已經發布了我想要的結果圖片。 – 2014-10-28 22:29:29