2013-03-08 295 views
2

真正在如何實現這一點掙扎。HTML/CSS - 響應式3列導航欄

我想製作一個導航欄,960px居中在頁面上。在此導航欄中有3個不同的部分或列。最左邊的是至少200px。中間的一直總是以20px爲中心。最右邊的一個至少爲300像素,並與右側對齊。在滿960像素,它會是這樣的: enter image description here

在740px,它會是這樣的: enter image description here

在620px,它會是這樣的: enter image description here

在520px,它看起來像這樣: enter image description here

而在下面,它會進入一些替代設計,所以不用擔心。 這樣做最好的方法是什麼?

編輯:這是我現在所擁有的。它的工作原理是960像素,但由於填充的原因,當您調整它的大小時,中間的div不會停留在中間位置。當它變得太小時,它也會崩潰,進入2條線。

.left { 
    float: left; 
    width: 300px; 
} 
.right { 
    float: right; 
    width: 400px; 
} 
.middle { 
    width: 100%; 
    padding: 0 100px; 
    height: 39px; 
} 
+0

在我看來,你也需要一個最大寬度,否則在左右兩側和中間的列之間可能永遠不會有空白空間。 – 2013-03-08 00:59:36

+0

你是什麼意思集中?在960像素和740像素的中間div居中,但在520px視圖中,它偏離了左邊的中心位置。 – JoeyP 2013-03-08 01:05:56

+0

在我看來,你希望左右列的寬度隨着瀏覽器寬度的減小而增大,對吧?如果是這樣,我不認爲這可以用CSS來完成。您可能需要JavaScript。 – 2013-03-08 01:09:07

回答

1

這是一個棘手的,因爲它確實需要像素完美的媒體查詢決絕:

http://codepen.io/cimmanon/pen/cmJhx

<nav> 
    <div class="a">a</div> 
    <div class="b">b</div><!-- 
    --><div class="c">c</div> 
</nav> 

注意註釋掉空白^^

body { 
    margin: 0; 
    padding: 0; 
} 

@media (min-width: 520px) and (max-width: 620px) { 
    nav { 
    text-align: right; 
    } 

    nav div { 
    text-align: left; 
    display: inline-block; 
    } 

    .a { 
    float: left; 
    } 
} 

@media (min-width: 620px) { 
    nav { 
    position: relative; 
    overflow: hidden; 
    } 

    .a { 
    float: left; 
    min-width: 200px; 
    } 

    .b { 
    position: absolute; 
    top: 0; 
    left: 50%; 
    margin-left: -10px; 
    } 

    .c { 
    float: right; 
    min-width: 300px; 
    } 
} 

/* colors! */ 

.a { 
    background: green; 
    min-width: 200px; 
} 

.b { 
    background: yellow; 
    width: 20px; 
} 

.c { 
    background: pink; 
    min-width: 300px; 
} 

的問題,當然,如果是你的第一個/最後一個元素是沒有大到足以容納他們的孩子。因爲中間元素絕對位於620px斷點處的頁面中間,所以不會有任何錯誤空間。

+0

非常好的工作!也有非常好的和簡單的只有3個div,並在正確的順序。非常感謝! – morgoe 2013-03-08 02:49:24

1

HTML

<div class="navWrap"> 
    <div class="nav"> 
     <div class="navLeft"></div> 
     <div class="navCenterWrap"> 
      <div class="navCenter"></div> 
     </div> 
     <div class="navRight"></div> 
    </div> 
</div>  

CSS

.navWrap {margin:auto; width:960px;} 
.nav { 
    float:left; 
    width:100%; 
} 
.navLeft { 
    width:20%; 
    float:left; 
    padding:10px 0; 
    background-color:green; 
} 
.navCenterWrap { 
    margin:auto; 
    width:5%; 
} 
.navCenter { 
    float:left; 
    width:100%; 
    padding:10px 0; 
    background-color:yellow; 
} 
.navRight { 
    width:30%; 
    float:right; 
    padding:10px 0; 
    background-color:blue; 
} 

@media screen and (max-width: 740px){ 
    .navWrap { 
     width:100%; 
    } 
} 

@media screen and (max-width: 520px){ 
    .navWrap { 
     width:100%; 
    } 
    .navLeft { 
     width:40%; 
     margin:0; 
    } 
    .navCenterWrap { 
     width:5%; 
     float:left; 
    } 
    .navRight { 
     width:55%; 
     margin:0; 
    } 
} 

注:滾動小提琴看到效果
工作demo

+0

這就是我想要的,但我需要它是流暢的,而不僅僅是在某些閾值上改變。 – morgoe 2013-03-08 01:41:10

+0

你說你想'960像素包裝',但如果你想它更流暢的佈局只是消除'960px'到'100%' – jhunlio 2013-03-08 01:44:23

2

你應該只需要一箇中介在這裏查詢。在下面的例子中,當窗口縮小時,黃色div將居中顯示,直到620px時,它將隨着粉紅色div一起滑動,直到520px滾動。不知道你是否在尋找這個或@ jhunlio的解決方案。

HTML

<div class="wrapper"> 
    <div class="left-col"></div> 
    <div class="inner-wrapper"> 
     <div class="middle-col"></div> 
     <div class="right-col"></div> 
     <br class="clean" /> 
    </div> 
</div> 

CSS

body{ 
    margin: 0px; 
} 
.clean{ 
    clear: both; 
} 
.wrapper{ 
    height: 100px; 
    position: relative; 
    min-width: 520px; 
} 
.inner-wrapper{ 
    position: absolute; 
    top: 0px; 
    right: 0px; 
    width: calc(50% + 10px); 
    height: 100%; 
} 
.left-col{ 
    background: green; 
    width: 200px; 
    height: 100%; 
} 
.middle-col{ 
    width: 20px; 
    height: 100%; 
    float: left; 
    background-color: yellow; 
} 
.right-col{ 
    float: right; 
    width: 300px; 
    background: pink; 
    height: 100%; 
} 

@media screen and (max-width: 620px){ 
    .inner-wrapper{ 
    width: auto; 
    } 
} 

小提琴http://jsfiddle.net/BkFup/2/

+0

這將正是我想要的..但​​這只是黃色的div堅持粉紅色div整個方式? – morgoe 2013-03-08 02:00:22