2016-11-09 93 views
2

我有兩個下拉列表,它們按預期工作(它們顯示懸停時的內容),但是,如果我點擊列表外部(左側)的區域,下拉列表仍會顯示。奇怪的下拉行爲css

我已經嘗試修復CSS,但我一直無法指出問題。

有沒有更好的樣式下拉式的方法,因爲我仍然是CSS的新手。

UPDATE:

測試這在Firefox和我無法重現該問題。我如何確保在所有瀏覽器中始終如一地運行?

.dropbtn { 
 
    color: white; 
 
    width: 200px; 
 
    margin-top: 160px; 
 
    margin-left: 276px; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
    text-align: left; 
 
    font-weight: 900; 
 
    background: rgba(0, 173, 239, 1); 
 
    border-radius: 0px; 
 
    -moz-border-radius: 0px; 
 
    -webkit-border-radius: 50px 50px 50px 50px; 
 
    position: relative; 
 
    z-index: 2; 
 
} 
 

 
.dropdown { 
 
    position: relative; 
 
    display: inline-block; 
 
    width: 480px; 
 
    height: 0px; 
 
    background: antiquewhite; 
 
} 
 

 
.dropdown-content { 
 
    display: none; 
 
    position: relative; 
 
    z-index: 1; 
 
    background-color: #f9f9f9; 
 
    /* min-width: 160px; */ 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
    right: 5px; 
 
    top: -20px; 
 
    margin-left: 283px; 
 
} 
 

 
.sphere { 
 
    height: 45px; 
 
    width: 45px; 
 
    border-radius: 50%; 
 
    vertical-align: top; 
 
    /* position: relative; */ 
 
    background: black; 
 
    display: inline-block; 
 
    position: relative; 
 
    margin-top: 165px; 
 
    margin-left: -55px; 
 
    z-index: 2; 
 
} 
 
.dropdown-content a { 
 
    table-layout: fixed; 
 
    color: black; 
 
    margin-left: -1px; 
 
    padding: 12px 16px 13px 30px; 
 
    text-decoration: none; 
 
    display: block; 
 
    border: 1px solid #000; 
 
    border-top: none; 
 
    font-size: 21px; 
 
} 
 
.dropdown-content a:first-child { 
 
    padding-top: 35px; 
 
    background: url("Images/sidearrow.png") no-repeat 7px 36px; 
 
} 
 

 
.dropdown a { 
 
\t background:url("Images/sidearrow.png") no-repeat 7px; 
 
} 
 

 
.dropdown-content a:first-child:hover { 
 
    padding-top: 35px; 
 
    background: url("Images/sidewhite.png") no-repeat 3px 36px; 
 
    color:black; 
 
    background-color: rgb(255,131,0); 
 
} 
 

 
.dropdown-content a:hover { 
 
\t background:url("Images/sidewhite.png") no-repeat 3px; 
 
\t background-color: rgb(255,131,0); 
 
\t color:white; 
 
} 
 

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

 
.dropdown:hover .dropbtn { 
 
    background-color: #3e8e41; 
 
} 
 

 
div#arrow { 
 
    position: absolute; 
 
    margin-left: 10px; 
 
    margin-top: 11px; 
 
} 
 

 

 
/* SENTINAL */ 
 

 

 
.dropbtn-sentinal { 
 
    color: white; 
 
    width: 200px; 
 
    margin-top: 160px; 
 
    margin-left: 276px; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
    text-align: left; 
 
    font-weight: 900; 
 
    background: rgba(0, 173, 239, 1); 
 
    border-radius: 0px; 
 
    -moz-border-radius: 0px; 
 
    -webkit-border-radius: 50px 50px 50px 50px; 
 
    position: relative; 
 
    z-index: 2; 
 
} 
 

 
.dropdown-sentinal{ 
 
    position: relative; 
 
    display: inline-block; 
 
    width: 480px; 
 
    height: 0px; 
 
    background: antiquewhite; 
 
} 
 

 
.dropdown-content-sentinal { 
 
    display: none; 
 
    position: relative; 
 
    z-index: 1; 
 
    background-color: #f9f9f9; 
 
    /* min-width: 160px; */ 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
    right: 5px; 
 
    top: -20px; 
 
    margin-left: 283px; 
 
} 
 

 
.sphere-sentinal { 
 
    height: 45px; 
 
    width: 45px; 
 
    border-radius: 50%; 
 
    vertical-align: top; 
 
    /* position: relative; */ 
 
    background: black; 
 
    display: inline-block; 
 
    position: relative; 
 
    margin-top: 165px; 
 
    margin-left: -55px; 
 
    z-index: 2; 
 
} 
 
.dropdown-content-sentinal a { 
 
    table-layout: fixed; 
 
    color: black; 
 
    margin-left: -1px; 
 
    padding: 12px 16px 13px 30px; 
 
    text-decoration: none; 
 
    display: block; 
 
    border: 1px solid #000; 
 
    border-top: none; 
 
    font-size: 21px; 
 
} 
 
.dropdown-content-sentinal a:first-child { 
 
    padding-top: 35px; 
 
    background: url("Images/sidearrow.png") no-repeat 7px 36px; 
 
} 
 

 
.dropdown-sentinal a { 
 
\t background:url("Images/sidearrow.png") no-repeat 7px; 
 
} 
 

 
.dropdown-content-sentinal a:first-child:hover { 
 
    padding-top: 35px; 
 
    background: url("Images/sidewhite.png") no-repeat 3px 36px; 
 
    color:black; 
 
    background-color: rgb(255,131,0); 
 
} 
 

 
.dropdown-content-sentinal a:hover { 
 
\t background:url("Images/sidewhite.png") no-repeat 3px; 
 
\t background-color: rgb(255,131,0); 
 
\t color:white; 
 
} 
 

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

 
.dropdown-sentinal:hover .dropbtn-sentinal { 
 
    background-color: #3e8e41; 
 
} 
 

 
div#arrow-sentinal { 
 
    position: absolute; 
 
    margin-left: 10px; 
 
    margin-top: 11px; 
 
} 
 

 
div#dropdown-one { 
 
    width: 500px; 
 
    height: 198px; 
 
} 
 

 
div#dropdown-two { 
 
    margin-left: 700px; 
 
    margin-top: -198px; 
 
} 
 

 

 
div#dropdown-content a { 
 
    text-decoration: none; 
 
    border: solid black 1px; 
 
    display: table-caption; 
 
    background-color: rgb(237,237,238); 
 
    background: url(Images/sidearrow.png) no-repeat 7px 36px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>title</title> 
 
\t <link rel="stylesheet" type="text/css" href="dropdown.css"> 
 
<meta charset="utf-8"> 
 
</head> 
 
<body> 
 
<div class="averios-header"> 
 
\t <div id="averios-logo"> 
 
\t \t <img src="averioslogo.png" width="176" height="129"> 
 
\t </div> 
 
\t <div id="setting-dropdown"> 
 
\t \t <!-- SETTING DROPDOWN WILL GO HERE --> 
 
\t </div> 
 
\t <div id="header-hr"> 
 
\t </div> 
 
</div> 
 

 
<div id="log-in"> 
 
\t <div id="loginHeader"> 
 
\t \t <h1>Portal</h1> 
 
\t </div> 
 
\t <div id="welcome-text"> 
 
\t \t <p> Welcome name </p> 
 
\t \t <p> Your last login was time on date </p> 
 
\t \t <br> 
 
\t \t <p> Please select an application below to begin </p> 
 
\t </div> 
 
</div> 
 
<div id="dropdown-one"> 
 
\t <div class="dropdown"> 
 
\t <button class="dropbtn">SELECT</button> 
 
\t <div class="sphere"> 
 
\t \t <div id="arrow"> 
 
\t \t \t <img src="C:\Users\mseh\Desktop\Images\arrow.png" width="29" height="27" alt=""/> 
 
\t \t </div> 
 
\t </div> 
 
\t <div class="dropdown-content"> 
 
\t \t <a href="#">Link 1</a> 
 
\t \t <a href="#">Link 1</a> 
 
\t \t <a href="#">Link 1</a> 
 
\t \t <a href="#">Link 1</a> \t \t 
 
\t </div> 
 
\t </div> 
 
</div> 
 

 
<div id="dropdown-two"> 
 
\t <div class="dropdown-sentinal"> 
 
\t <button class="dropbtn-sentinal">SELECT</button> 
 
\t <div class="sphere-sentinal"> 
 
\t \t <div id="arrow-sentinal"> 
 
\t \t \t <img src="C:\Users\mseh\Desktop\Images\arrow.png" width="29" height="27" alt=""/> 
 
\t \t </div> 
 
\t </div> 
 
\t <div class="dropdown-content-sentinal"> 
 
\t \t <a href="#">Link 1</a> 
 
\t \t <a href="#">Link 1</a> 
 
\t \t <a href="#">Link 1</a> 
 
\t \t <a href="#">Link 1</a> \t 
 
\t </div> 
 
\t </div> 
 
</div> 
 
</body> 
 
</html>

Image

+0

您已經測試了自己的例子,對不對?如果我超出清單的範圍,清單就會關閉(應該如此)。所以或者這個例子不會給我們提供你遇到的問題,或者你正在經歷這個問題,這讓我質疑你正在使用的瀏覽器。因爲上面的例子按預期工作。 – junkfoodjunkie

+0

@junkfoodjunkie我剛剛在Firefox中測試,它沒有問題。但CSS不會在Firefox中加載。任何建議與我如何解決這個問題有關? – Maddy

+0

咦?我正在查看你的代碼 - 它顯示了一個藍色的矩形,每邊都有半個圓圈,當我將它懸停在它上面時,它變成綠色,並且下拉選擇菜單。這看起來是一樣的,並且在Safari,Firefox,Chrome和Opera中也是一樣的。你使用的是什麼瀏覽器? – junkfoodjunkie

回答

1

這裏 - 我縮短了例如降一點(我也不認爲你需要一個以上的按鈕,所有獨立的CSS,你應該能夠使用這些類,只是通過使用#dropdown_two .classname來改變您可能想要改變的東西(如位置)的具體情況。

Chrome中的問題我認爲是容器的寬度比按鈕本身更寬,這使得它變得不合適。它可能不是100%,但它應該讓你開始。我將某些值更改爲動態(em而不是px),因此可能會出現一些問題。

#dropdown-one { 
 
    margin-left: 20em; 
 
    } 
 
.dropbtn { 
 
    color: white; 
 
    width: 100%; 
 
    height: 3.2em; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
    text-align: left; 
 
    font-weight: 900; 
 
    background: rgba(0, 173, 239, 1); 
 
    border-radius: 0px; 
 
    -moz-border-radius: 0px; 
 
    -webkit-border-radius: 50px 50px 50px 50px; 
 
    position: relative; 
 
    z-index: 2; 
 
} 
 

 
.dropdown { 
 
    position: relative; 
 
    display: block; 
 
    width: 12.5em; 
 
    background:antiquewhite; 
 
} 
 

 
.dropdown-content { 
 
    display: none; 
 
    position: relative; 
 
    z-index: 1; 
 
    background-color: #f9f9f9; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
    top: -20px; 
 
} 
 

 
.sphere { 
 
    height: 2.9em; 
 
    width: 2.9em; 
 
    border-radius: 50%; 
 
    border: 2px solid rgba(0, 173, 239, 1); 
 
    /* position: relative; */ 
 
    background: black; 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    right: .1em; 
 
    z-index: 2; 
 
} 
 
.dropdown-content a { 
 
    table-layout: fixed; 
 
    color: black; 
 
    padding: .75em; 
 
    text-decoration: none; 
 
    display: block; 
 
    border: 1px solid #000; 
 
    border-top: none; 
 
    font-size: 21px; 
 
    box-sizing: border-box; 
 
} 
 
.dropdown-content a:first-child { 
 
    padding-top: 35px; 
 
    background: url("Images/sidearrow.png") no-repeat 7px 36px; 
 
} 
 

 
.dropdown a { 
 
\t background:url("Images/sidearrow.png") no-repeat 7px; 
 
} 
 

 
.dropdown-content a:first-child:hover { 
 
    background: url("Images/sidewhite.png") no-repeat 3px 36px; 
 
    color:black; 
 
    background-color: rgb(255,131,0); 
 
} 
 

 
.dropdown-content a:hover { 
 
\t background:url("Images/sidewhite.png") no-repeat 3px; 
 
\t background-color: rgb(255,131,0); 
 
\t color:white; 
 
} 
 

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

 
.dropdown:hover .dropbtn { 
 
    background-color: #3e8e41; 
 
} 
 

 
div#arrow { 
 
    position: absolute; 
 
    margin-left: 10px; 
 
    margin-top: 11px; 
 
}
<div class="averios-header"> 
 
\t <div id="setting-dropdown"> 
 
\t \t <!-- SETTING DROPDOWN WILL GO HERE --> 
 
\t </div> 
 
</div> 
 

 
<div id="log-in"> 
 
\t <div id="loginHeader"> 
 
\t \t <h1>Portal</h1> 
 
\t </div> 
 
\t <div id="welcome-text"> 
 
\t \t <p> Welcome name </p> 
 
\t \t <p> Your last login was time on date </p> 
 
\t \t <p> Please select an application below to begin </p> 
 
\t </div> 
 
</div> 
 
<div id="dropdown-one"> 
 
\t <div class="dropdown"> 
 
\t <button class="dropbtn">SELECT</button> 
 
\t <div class="sphere"> 
 
\t \t <div id="arrow"> 
 
\t \t \t <img src="C:\Users\mseh\Desktop\Images\arrow.png" width="29" height="27" alt=""/> 
 
\t \t </div> 
 
\t </div> 
 
\t <div class="dropdown-content"> 
 
\t \t <a href="#">Link 1</a> 
 
\t \t <a href="#">Link 1</a> 
 
\t \t <a href="#">Link 1</a> 
 
\t \t <a href="#">Link 1</a> \t \t 
 
\t </div> 
 
\t </div> 
 
</div>

+0

@如果你可以幫助我一件事,如何將選擇按鈕向右移動,如果我將按鈕向右移動,它具有相同的行爲。 – Maddy

+0

停止移動按鈕,移動按鈕的容器。我更新了答案。 – junkfoodjunkie

+0

謝謝你的幫助 – Maddy