2017-03-26 31 views
1

所以我想了解爲什麼當我指定我的.thenav類的寬度時,它不會擴展到頁面的整個寬度。爲什麼我的導航類不能擴展到100%的寬度?

我理解它正在採用.container類的特性,但我不明白爲什麼和解決方案看到我指定了寬度?請幫忙!

這裏是我發生了什麼事的圖片(我連着發生了什麼事,因爲的jsfiddle使得DIV的圖像出現在100%,它不是):

http://imgur.com/a/zsBqC

這裏是我的jsfiddle:

https://jsfiddle.net/CheckLife/yox7Ln1b/3/

下面是引用代碼:

HTML:

<div class="header"> 
    <div class="container"> 
    <h1 id="box" onload="changed()"><image src="nbaone.png" width="40px" height="55px" class="nba">NBA Legends</h1> 
    <div class="thenav" onload="changed()"> 
     <ul> 
     <li><a href="http://www.nba.com"/>Home</a></li> 
     <li onclick="changeP()"><a href="#">About</a></li> 
     <li><a href="#">Players</a> 
      <ul> 
       <li onmouseover="slow()"><a href="#"/>T-Mac <img src="tmacw.png" width="25px" height="20px" class="tmacw"></a></li> 
       <li><a href="#kobesec"/>Kobe</a></li> 
       <li><a href="#"/>Kevin Durant</a></li> 
       <li><a href="#"/>The Goat</a></li> 
      </ul> 
     </li> 
     <li onclick="slow()"><a href="#">News</a></li> 
     </ul>   
    </div> 
    </div> 
</div> 

CSS:

* { 
    margin: 0; 
    padding: 0; 
    list-style-type: none; 
    text-decoration: none; 
} 

header, nav, section, aside, footer, article { 
    display: block; 
} 

body { 
background-image: url(backwood.png); 
width: 100%; 
margin: auto; 

} 

.container { 
    margin: 0px auto; 
    background-size: cover; 
    width: 1300px; 
    height: 100%; 
} 

.header { 
    background:linear-gradient(to right, #5092f4, #f29641); 
    margin-top: 0px; 
    width: 100%; 
} 

.header h1{ 
    text-align: center; 
    width: 100%; 
    padding-bottom: 15px; 
    font-family: Arial, Helvetica, sans-serif; 
    color: #f4ca1f; 
} 
.tmacw { 
    display:inline; 
    position: relative; 
    padding: 0px; 
    top: 5px; 
} 

.nba { 
    margin-right: 10px; 
} 

.thenav { 
    background-color: #7886a3; 
    width: 100%; 
    height: 85px; 
    position: relative; 
    z-index: 1; 
} 

       /* Style for the Nav Bar */ 


.thenav ul { 
    padding: 0; 
    margin: 0; 

} 

.thenav ul li { 
    float: left; 
    width: 90px; 

    text-align: center; 
    border-right: 1px groove #141e38; 
    position: relative; 
} 

.thenav ul li a { 
    display: block; 
    color: white; 
    font-weight: bold; 
    padding: 33px 10px; 
} 

.thenav ul li a:hover { 
    background-color: #47e586; 
    transition: all 0.90s; 
} 

           /*Dropdown Nav */ 

.thenav li ul li{ 
    background-color: #7886a3; 
    border: 2px groove grey; 
    border-radius: 4px; 
    position: relative; 
} 

.thenav li ul li a { 
padding: 8px; 
text-align:left; 
} 

.thenav li ul li:nth-child(1) a:hover { 
    background-color: #F47575; 

} 

.thenav li ul li:nth-child(2) a:hover { 
    background-color: #f7d759 ; 
} 
.thenav li ul li a:hover{ 
    background-color: red; 
} 




.thenav ul li ul { 
    display: none; 
} 
.thenav li:hover ul{ 
    position:absolute; 

} 
.thenav li:hover ul{ 
    display: block; 
} 

        /* End of Dropdown */ 

.userlogin { 
    font-size: 12px; 
    top:2px; 
    color: white; 
} 

input[type=text], input[type=password] { 
    font-weight: bold; 
    margin: 0; 
    font-size: 8px; 
    height: 10px; 
    padding: 3px 5px 3px 5px; 
    color: 162354; 
    } 

         /* Stats Button */ 
.stat input[type=button] { 
    background-color: #6cd171; 
    color: blue; 
    border-radius: 6px; 
    font-weight: bold; 
    border: none; 
    float: left; 
    margin-top: 20px; 
    margin-left: 20px; 
    padding: 2px 2px; 
    font-family: Verdana, Geneva, sans-serif; 
} 


.log[type=button] { 
    background-color: white; 
    color: #008cff; 
    border-radius: 4px; 
    font-weight: bold; 
    border: none; 
    padding: 1px 2px 2px 2px; 
    position: relative; 
    left: 5px; 
    top: 3px; 

} 
+0

值得在這裏上傳您的代碼片段,以及提供的jsfiddle鏈接。 – Pineda

+0

@pineda是啊我知道它只是每當我做錯了!哈哈我知道即時通訊這樣的新手,我一直這樣做了3周,現在我不能把它放下,這就是爲什麼我不斷問問題!感謝兄弟! – CheckLife

回答

0

一個孩子的div不具有絕對定位,並且具有100%的寬度(不必要如果它的顯示是block缺省值)將被設置爲它的容器寬度。您的div.containerwidth設置爲1300px,並且它是div.thenav的父元素,因此div.thenav的寬度也將爲1300px。

.container { 
    margin: 0px auto; 
    background-size: cover; 
    /*width: 1300px; remove this */ 
    height: 100%; 
} 

或:

移動div.thenav以外的div.container如以下代碼: (https://jsfiddle.net/nod19rze/

<div class="header"> 
    <h1 id="box" onload="changed()"><image src="nbaone.png" width="40px" height="55px" class="nba">NBA Legends</h1> 
    <div class="thenav" onload="changed()"> 
    <!-- contents of thenav here --> 
    </div> 
    <div class="container"> 

    </div> 
</div> 

+0

但我這樣做,所以我可以將其應用到我的內容部分....我想如果我指定一個寬度,它將使用指定的屬性。 – CheckLife

+0

我明白了。事情是,你的'.thenav' div是_inside_'div.container',所以它會尊重它父母的寬度。 – Pineda

+0

是的,我明白,但爲什麼如果我設置不同的寬度?我知道該怎麼做,我只是不明白,如果我可以將規則應用於其他容器中的孩子元素,並且他們不尊重父母?所以唯一的辦法是,如果我刪除它? – CheckLife

0

您可以在容器上除去寬度無論是con tainer應該是第一個包裝,然後是標題,這可以解決問題。我不確定這是否是你需要的。請檢查此琴: https://jsfiddle.net/estgLn1q/1/

<div class="container"> 
    <div class="header"> 
    </div> 
</div> 

或者,如果你想保持相同的HTML結構,然後從「.container」這將導致集裝箱採取相同的寬度爲其父刪除width:1300px

0

我只想將#box.thenav.container中移出,並在那些元素之後開始該類。

* { 
 
    margin: 0; 
 
    padding: 0; 
 
    list-style-type: none; 
 
    text-decoration: none; 
 
} 
 

 
header, 
 
nav, 
 
section, 
 
aside, 
 
footer, 
 
article { 
 
    display: block; 
 
} 
 

 
body { 
 
    background-image: url(backwood.png); 
 
    width: 100%; 
 
    margin: auto; 
 
} 
 

 
.container { 
 
    margin: 0px auto; 
 
    background-size: cover; 
 
    width: 1300px; 
 
    height: 100%; 
 
} 
 

 
.header { 
 
    background: linear-gradient(to right, #5092f4, #f29641); 
 
    margin-top: 0px; 
 
    width: 100%; 
 
} 
 

 
.header h1 { 
 
    text-align: center; 
 
    width: 100%; 
 
    padding-bottom: 15px; 
 
    font-family: Arial, Helvetica, sans-serif; 
 
    color: #f4ca1f; 
 
} 
 

 
.tmacw { 
 
    display: inline; 
 
    position: relative; 
 
    padding: 0px; 
 
    top: 5px; 
 
} 
 

 
.nba { 
 
    margin-right: 10px; 
 
} 
 

 
.thenav { 
 
    background-color: #7886a3; 
 
    width: 100%; 
 
    height: 85px; 
 
    position: relative; 
 
    z-index: 1; 
 
} 
 

 

 
/* Style for the Nav Bar */ 
 

 
.thenav ul { 
 
    padding: 0; 
 
    margin: 0; 
 
} 
 

 
.thenav ul li { 
 
    float: left; 
 
    width: 90px; 
 
    text-align: center; 
 
    border-right: 1px groove #141e38; 
 
    position: relative; 
 
} 
 

 
.thenav ul li a { 
 
    display: block; 
 
    color: white; 
 
    font-weight: bold; 
 
    padding: 33px 10px; 
 
} 
 

 
.thenav ul li a:hover { 
 
    background-color: #47e586; 
 
    transition: all 0.90s; 
 
} 
 

 

 
/*Dropdown Nav */ 
 

 
.thenav li ul li { 
 
    background-color: #7886a3; 
 
    border: 2px groove grey; 
 
    border-radius: 4px; 
 
    position: relative; 
 
} 
 

 
.thenav li ul li a { 
 
    padding: 8px; 
 
    text-align: left; 
 
} 
 

 
.thenav li ul li:nth-child(1) a:hover { 
 
    background-color: #F47575; 
 
} 
 

 
.thenav li ul li:nth-child(2) a:hover { 
 
    background-color: #f7d759; 
 
} 
 

 
.thenav li ul li a:hover { 
 
    background-color: red; 
 
} 
 

 
.thenav ul li ul { 
 
    display: none; 
 
} 
 

 
.thenav li:hover ul { 
 
    position: absolute; 
 
} 
 

 
.thenav li:hover ul { 
 
    display: block; 
 
} 
 

 

 
/* End of Dropdown */ 
 

 
.userlogin { 
 
    font-size: 12px; 
 
    top: 2px; 
 
    color: white; 
 
} 
 

 
input[type=text], 
 
input[type=password] { 
 
    font-weight: bold; 
 
    margin: 0; 
 
    font-size: 8px; 
 
    height: 10px; 
 
    padding: 3px 5px 3px 5px; 
 
    color: 162354; 
 
} 
 

 

 
/* Stats Button */ 
 

 
.stat input[type=button] { 
 
    background-color: #6cd171; 
 
    color: blue; 
 
    border-radius: 6px; 
 
    font-weight: bold; 
 
    border: none; 
 
    float: left; 
 
    margin-top: 20px; 
 
    margin-left: 20px; 
 
    padding: 2px 2px; 
 
    font-family: Verdana, Geneva, sans-serif; 
 
} 
 

 
.log[type=button] { 
 
    background-color: white; 
 
    color: #008cff; 
 
    border-radius: 4px; 
 
    font-weight: bold; 
 
    border: none; 
 
    padding: 1px 2px 2px 2px; 
 
    position: relative; 
 
    left: 5px; 
 
    top: 3px;
<div class="header"> 
 

 
    <h1 id="box" onload="changed()"><image src="nbaone.png" width="40px" height="55px" class="nba">NBA Legends</h1> 
 
    <div class="thenav" onload="changed()"> 
 

 
    <ul> 
 
     <li><a href="http://www.nba.com" />Home</a> 
 
     </li> 
 
     <li onclick="changeP()"><a href="#">About</a></li> 
 
     <li><a href="#">Players</a> 
 
     <ul> 
 
      <li onmouseover="slow()"><a href="#" />T-Mac <img src="tmacw.png" width="25px" height="20px" class="tmacw"></a> 
 
      </li> 
 
      <li><a href="#kobesec" />Kobe</a> 
 
      </li> 
 
      <li><a href="#" />Kevin Durant</a> 
 
      </li> 
 
      <li><a href="#" />The Goat</a> 
 
      </li> 
 
     </ul> 
 
     </li> 
 
     <li onclick="slow()"><a href="#">News</a></li> 
 
    </ul> 
 

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

+0

Ohhhhhhhhhh我明白了,但是如果看到div仍然在標題內,它是否會尊重屬性並且仍然將其位置固定在頂部? @ michael – CheckLife

+0

男人感謝兄弟你總是幫助!!!!! – CheckLife

相關問題