2017-04-22 64 views
0

我試圖實現這種看中標誌居中且菜單按鈕位於右側的樣子。響應式中心徽標右邊的菜單按鈕

enter image description here

這是我都試過了。我要麼居中或兩者都沒有。我能夠做的另一件事是讓圖像左對齊,菜單按鈕右對齊。

我們的目標是讓圖像中心和菜單按鈕右對齊

HTML

.topnav { 
 
    background-repeat:no-repeat; 
 
    background-size:cover; 
 
    overflow:visible; 
 
    width: 100%; 
 
    padding-bottom: 0px; 
 
} 
 

 
.topnav nav{ 
 
    padding-top: 5px; 
 
    padding-bottom: 8px; 
 
    background: -webkit-linear-gradient(left, transparent, black, black, transparent); /* For Safari 5.1 to 6.0 */ 
 
    background: -o-linear-gradient(left, transparent, black,black, transparent); /* For Opera 11.1 to 12.0 */ 
 
    background: -moz-linear-gradient(left, transparent, black,black, transparent); /* For Fx 3.6 to 15 */ 
 
    background: linear-gradient(to right, transparent, black, black, transparent); /* Standard syntax (must be last) */ 
 
    width:100%; 
 
} 
 

 
.topnav nav div{ 
 
    width: 85%; 
 
    margin-left: 7.5%; 
 
    margin-right: 7.5% ; 
 
    display:flex; 
 
    align-items: center; 
 
    justify-content: center; 
 
} 
 

 
.link { 
 
    color: white; 
 
    text-decoration: none; 
 
    font-size: 11px; 
 
    font-weight: 700; /* this is futura heavy*/ 
 
    width:11.25%; 
 
    letter-spacing: 2px; 
 
    word-wrap:true; 
 
    display:inline-block; 
 
    vertical-align: top; 
 
    text-align: center; 
 
    /*line-height: 5em;*/ 
 
} 
 

 
.notlink { 
 
    width:30.4%; 
 
    margin-left: 1%; 
 
    margin-right: 1%; 
 
    display: inline-block; 
 
} 
 

 
.notlink img { 
 
    width: 100%; 
 
} 
 

 
.topnav nav .link:hover { 
 
    color: #008ed2; 
 
} 
 

 
.icon { 
 
    display: none; 
 
    font-size: 32px; 
 
    color: white; 
 
    text-decoration: none; 
 
    text-align: center; 
 
    line-height: 40px; 
 
    min-width: 40px; 
 
    min-height: 40px; 
 
} 
 

 

 
/* When the screen is less than 600 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */ 
 
@media only screen and (max-width: 767px) { 
 
    .myTopnav a.link {display: none;} 
 
    
 
    .topnav nav div{ 
 
     width: 100%; 
 
     margin-left: 0; 
 
     margin-right: 0; 
 
     position: relative; 
 
     top: 0; 
 
     left: 0; 
 
    } 
 
    
 
    .icon { 
 
     position: relative; 
 
     float: right; 
 
     display: inline-block; 
 
    } 
 
    
 
    .notlink { 
 
     width: 70%; 
 
     float: left; 
 
    } 
 
    .notlink img { 
 
     display: inline-block; 
 
     max-width:100%; 
 
     max-height:75px; 
 
     width: auto; 
 
     height: auto; 
 
    } 
 
} 
 

 
@media screen and (max-width: 767px) { 
 
    .myTopnav.responsive {position: relative;} 
 
    .myTopnav.responsive .icon { 
 
     position: absolute; 
 
     right: 0; 
 
     top: 0; 
 
    } 
 
    .myTopnav.responsive a { 
 
     float: none; 
 
     display: block; 
 
     text-align: left; 
 
    } 
 

 
}
<div class="topnav"> 
 
       <nav> 
 
        <div class="myTopnav" id="myTopnav"> 
 
         <a class="link" href="index.html">HOME</a> 
 
         <a class="link" id ="selected" href="about.html">ABOUT</a> 
 
         <a class="link" href="expertise.html">EXPERTISE</a> 
 
         <a class="notlink" href="index.html"><img src="https://www.google.com/logos/doodles/2017/earth-day-2017-5650394117046272-s.png"></a> 
 
         <a class="link" href="industries.html">INDUSTRIES</a> 
 
         <a class="link" href="friends.html">FRIENDS OF MARY</a> 
 
         <a class="link" href="contact.html">CONTACT</a> 
 
         <a href="javascript:void(0);" class="icon" onclick="topNav()">&#9776;</a> 
 
        </div> 
 
       </nav> 
 
      </div>

這裏是FIDDILE

回答

1

使用Flexbox的:

.icon { 
    display :inline-block; /*or inline works as well*/ 
    position : absolute; 
    right : 0; 
} 

.notlink { 
    width: 70%; 
    display : flex; 
    align-items: center; 
    justify-content : center; 
} 
.notlink img { 
    /*removed display inline-block*/ 
    max-width:100%; 
    max-height:75px; 
    width: auto; 
    height: auto; 
} 

fiddle

+0

這使徽標居中,但菜單按鈕未對齊。 – codeNinja

+0

好吧,得到的圖像是對齊的,但問題與實際顯示菜單現在.... https://jsfiddle.net/gyexzhao/8/ – codeNinja

+0

好吧,但這是其他問題作爲您的初始職位 –

1

有幾個選項,

text-align爲標誌和設置在absolute漢堡可以很容易。

這裏是一個例子。在媒體查詢

/* show me center */ 
 

 
body { 
 
    background:linear-gradient(to left, white 50%,gray 50%); 
 
} 
 

 
/* layout */ 
 
.topnav { 
 
    background-repeat:no-repeat; 
 
    background-size:cover; 
 
    overflow:visible; 
 
    width: 100%; 
 
    padding-bottom: 0px; 
 
} 
 

 
.topnav nav{ 
 
    padding-top: 5px; 
 
    padding-bottom: 8px; 
 
    background: -webkit-linear-gradient(left, transparent, black, black, transparent); /* For Safari 5.1 to 6.0 */ 
 
    background: -o-linear-gradient(left, transparent, black,black, transparent); /* For Opera 11.1 to 12.0 */ 
 
    background: -moz-linear-gradient(left, transparent, black,black, transparent); /* For Fx 3.6 to 15 */ 
 
    background: linear-gradient(to right, transparent, black, black, transparent); /* Standard syntax (must be last) */ 
 
    width:100%; 
 
} 
 

 
.topnav nav div{ 
 
    width: 85%; 
 
    margin-left: 7.5%; 
 
    margin-right: 7.5% ; 
 
    display:flex; 
 
    align-items: center; 
 
    justify-content: center; 
 
} 
 

 
.link { 
 
    color: white; 
 
    text-decoration: none; 
 
    font-size: 11px; 
 
    font-weight: 700; /* this is futura heavy*/ 
 
    width:11.25%; 
 
    letter-spacing: 2px; 
 
    word-wrap:true; 
 
    display:inline-block; 
 
    vertical-align: top; 
 
    text-align: center; 
 
    /*line-height: 5em;*/ 
 
} 
 

 
.notlink { 
 
    width:30.4%; 
 
    margin-left: 1%; 
 
    margin-right: 1%; 
 
    display: inline-block; 
 
} 
 

 
.notlink img { 
 
    width: 100%; 
 
} 
 

 
.topnav nav .link:hover { 
 
    color: #008ed2; 
 
} 
 

 
.icon { 
 
    display: none; 
 
    font-size: 32px; 
 
    color: white; 
 
    text-decoration: none; 
 
    text-align: center; 
 
    line-height: 40px; 
 
    min-width: 40px; 
 
    min-height: 40px; 
 
} 
 

 

 
/* When the screen is less than 600 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */ 
 
@media only screen and (max-width: 767px) { 
 
    .myTopnav a.link {display: none;} 
 
/* update */ 
 
    .topnav nav div{ 
 
     width: 100%; 
 
     margin-left: 0; 
 
     margin-right: 0; 
 
     position: relative; 
 
     top: 0; 
 
     left: 0; 
 
     text-align:center; 
 
    } 
 
    
 
    .icon { 
 
     position: absolute; 
 
     z-index:1; 
 
     left:auto; 
 
     left:100%; 
 
     top:50%; 
 
     transform:translate(-200%,-50%); 
 
     display: inline-block; 
 
    } 
 
    /* end update */ 
 
    .notlink { 
 
    } 
 
    .notlink img { 
 
     display: inline-block; 
 
     max-width:100%; 
 
     max-height:75px; 
 
     width: auto; 
 
     height: auto; 
 
    } 
 
}
<div class="topnav"> 
 
       <nav> 
 
        <div class="myTopnav" id="myTopnav"> 
 
         <a class="link" href="index.html">HOME</a> 
 
         <a class="link" id ="selected" href="about.html">ABOUT</a> 
 
         <a class="link" href="expertise.html">EXPERTISE</a> 
 
         <a class="notlink" href="index.html"><img src="https://www.google.com/logos/doodles/2017/earth-day-2017-5650394117046272-s.png"></a> 
 
         <a class="link" href="industries.html">INDUSTRIES</a> 
 
         <a class="link" href="friends.html">FRIENDS OF MARY</a> 
 
         <a class="link" href="contact.html">CONTACT</a> 
 
         <a href="javascript:void(0);" class="icon" onclick="topNav()">&#9776;</a> 
 
        </div> 
 
       </nav> 
 
      </div>

snippet turned into a fiddle

+0

菜單按鈕不會顯示爲右對齊 – codeNinja

+0

@codeNinja以及更新協調來設置它,無論你需要它:),答案是關於一個方法來設置標誌在中心。 –