2016-07-28 17 views
1

Ich habe eine obere Leiste erstellt, die auf der rechten Seite die Dropdown-Taste MENU enthält. Aber dieser Drop-Down-Inhalt hat genau die gleiche Größe (Breite) wie meine MENU-Taste. Schließlich - mein Ziel ist es, diesen Drop-Down-Inhalt so breit wie die obere Leiste zu machen.CSS: mache das Dropdown-Menü so breit wie die obere Leiste

Mein HTML-Code sieht wie folgt aus:

<div id="top-bar"> 
    <div class="dropdown"> 
    <button class="dropbtn">MENU</button> 
    <div class="dropdown-content"> 
     <a href="#">Link</a> 
     <a href="#">Link</a> 
     <a href="#">Link</a> 
    </div> 
    </div> 
</div> 

Und wichtiger Teil - CSS:

#top-bar{ 
    left: 0; 
    top: 0; 
    float: left; 
    width:100%; 
    height:40px; 
    background-color: black; 
} 
.dropbtn { 
    background-color: blue; 
    color: white; 
    height: 40px; 
    font-size: 12px; 
    border: none; 
    cursor: pointer; 
} 
.dropdown { 
    float: right; 
    position: relative; 
    display: inline-block; 
} 
.dropdown-content { 
    display: none; 
    float: left; 
    position: absolute; 
    background-color: #f9f9f9; 
    width: 100%; 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
} 
.dropdown-content a { 
    color: black; 
    padding: 10px 14px; 
    text-decoration: none; 
    display: block; 
} 
.dropdown-content a:hover {background-color: #f1f1f1} 
.dropdown:hover .dropdown-content { 
    display: block; 
    width: 100%; 
    float: left; 
    left: 0; 
} 
.dropdown:hover .dropbtn { 
    background-color: blue; 
} 

Wenn Sie sehen wollen, wie es wie HERE sieht ist jsFiddle Link.
Haben Sie keine Ahnung, wie Sie mein Problem lösen können?

Antwort

1

Bewegen Sie einfach position: relative von .dropdown zu #top-bar.

Dadurch berechnet .dropdown-contentwidth nach dem nächsten Element mit position: relative, d. H. #top-bar.

#top-bar{ 
 
    position: relative; 
 
    height:40px; 
 
    float: left; 
 
    width: 100%; 
 
    background-color: black; 
 
} 
 

 
.dropbtn { 
 
    background-color: blue; 
 
    color: white; 
 
    height: 40px; 
 
    font-size: 12px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
.dropdown { 
 
    float: right; 
 
} 
 

 
.dropdown-content { 
 
    display: none; 
 
    left: 0; 
 
    top: 100%; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    width: 100%; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
.dropdown-content a { 
 
    color: black; 
 
    padding: 10px 14px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
.dropdown-content a:hover {background-color: #f1f1f1} 
 

 
.dropdown:hover .dropdown-content { 
 
    display: block; 
 
} 
 

 
.dropdown:hover .dropbtn { 
 
    background-color: blue; 
 
}
<div id="top-bar"> 
 
    <div class="dropdown"> 
 
    <button class="dropbtn">MENU</button> 
 
    <div class="dropdown-content"> 
 
     <a href="#">Link</a> 
 
     <a href="#">Link</a> 
 
     <a href="#">Link</a> 
 
    </div> 
 
    </div> 
 
</div>

+0

so einfach wollen, Sie sind erstaunlich, danke! –

+0

@ PetrBečka Sie sind willkommen) –

+0

@ PetrBečka Ich habe CSS aktualisiert und unnötige Eigenschaften entfernt. –

1

Ich denke, das ist, was Sie

#top-bar{ 
 
    left: 0; 
 
    top: 0; 
 
    float: left; 
 
    width:100%; 
 
    height:40px; 
 
    background-color: black; 
 
} 
 

 
.dropbtn { 
 
    display: block; 
 
    float: right; 
 
    background-color: blue; 
 
    color: white; 
 
    height: 40px; 
 
    font-size: 12px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
.dropdown { 
 
    width: 100%; 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 

 
.dropdown-content { 
 
    display: none; 
 
    float: left; 
 
    background-color: #f9f9f9; 
 
    width: 100%; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
.dropdown-content a { 
 
    color: black; 
 
    padding: 10px 14px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
.dropdown-content a:hover {background-color: #f1f1f1} 
 

 
.dropdown:hover .dropdown-content { 
 
    display: block; 
 
    width: 100%; 
 
    float: left; 
 
    left: 0; 
 
} 
 

 
.dropdown:hover .dropbtn { 
 
    background-color: blue; 
 
}
<div id="top-bar"> 
 
    <div class="dropdown"> 
 
    <button class="dropbtn">MENU</button> 
 
    <div class="dropdown-content"> 
 
     <a href="#">Link</a> 
 
     <a href="#">Link</a> 
 
     <a href="#">Link</a> 
 
    </div> 
 
    </div> 
 
</div>

https://jsfiddle.net/69uts0dr/3/