2016-07-16 7 views
-1

私は都市を最初に選択するページを設計しています。今、私はボタンとそれにドロップダウンアイテムを取得することができますが、それはページの上部にとどまっています。私はそれを中央に、あるいはおそらくページの左中央に移動したいと思っています。私はこの上の初心者だドロップダウンに沿ってこのボタンを移動する方法

<!DOCTYPE html> 
<html> 
<head> 
<style> 
.dropbtn { 
    background-color: #4CAF50; 
    color: white; 
    padding: 16px 40px; 
    font-size: 16px; 
    border: none; 
    cursor: pointer; 
} 

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

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

.dropdown-content { 
    display: none; 
    position: absolute; 
    background-color: #f9f9f9; 
    min-width: 160px; 
    overflow: auto; 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
} 

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

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

.show {display:block;} 
</style> 
</head> 
<body> 


<div class="dropdown"> 
<button onclick="myFunction()" class="dropbtn">Select City</button> 
<div id="myDropdown" class="dropdown-content"> 
<a href="#home">Mumbai</a> 

</div> 
</div> 

<script> 
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */ 
function myFunction() { 
document.getElementById("myDropdown").classList.toggle("show"); 
} 

// Close the dropdown if the user clicks outside of it 
window.onclick = function(event) { 
if (!event.target.matches('.dropbtn')) { 

var dropdowns = document.getElementsByClassName("dropdown-content"); 
var i; 
for (i = 0; i < dropdowns.length; i++) { 
    var openDropdown = dropdowns[i]; 
    if (openDropdown.classList.contains('show')) { 
    openDropdown.classList.remove('show'); 
    } 
} 
} 
} 

:ここに私のコードです。

答えて

0

私はわからないが、多分あなたはそれをしたい:このCSSに変更 : - あなたがtext-align: center;を使用することができ、ボタンの周りwidth: 100%;でコンテナを追加することにより

.dropdown { 
    position: relative; 
    display: block; 
    text-align: center; 
} 
.dropdown-content { 
    display: none; 
    position: relative; 
    background-color: #f9f9f9; 
    max-width: 160px; 
    overflow: auto; 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
    margin: auto; 
} 

/* When the user clicks on the button, 
 
toggle between hiding and showing the dropdown content */ 
 
function myFunction() { 
 
document.getElementById("myDropdown").classList.toggle("show"); 
 
} 
 

 
// Close the dropdown if the user clicks outside of it 
 
window.onclick = function(event) { 
 
if (!event.target.matches('.dropbtn')) { 
 

 
var dropdowns = document.getElementsByClassName("dropdown-content"); 
 
var i; 
 
for (i = 0; i < dropdowns.length; i++) { 
 
    var openDropdown = dropdowns[i]; 
 
    if (openDropdown.classList.contains('show')) { 
 
    openDropdown.classList.remove('show'); 
 
    } 
 
} 
 
} 
 
}
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px 40px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

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

 
.dropdown { 
 
    position: relative; 
 
    display: block; 
 
    text-align: center; 
 
} 
 

 
.dropdown-content { 
 
    display: none; 
 
    position: relative; 
 
    background-color: #f9f9f9; 
 
    max-width: 160px; 
 
    overflow: auto; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
    margin: auto; 
 
} 
 

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

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

 
.show {display:block;}
<div class="dropdown"> 
 
<button onclick="myFunction()" class="dropbtn">Select City</button> 
 
<div id="myDropdown" class="dropdown-content"> 
 
<a href="#home">Mumbai</a> 
 

 
</div> 
 
</div>

0

をボタンを水平に中央に置きます(左に水平に整列させる場合は削除できます)。top: 50%;を中心にposition: absolute;を使用します垂直;

さらに、ボタンの高さの半分を表す、margin-top: -38px;をコンテナに追加して、top: 50%;をボタンの上端に合わせて中央に配置しないように調整しました。

あなたは水平方向および垂直方向に整列するより多くの方法のために、この記事を見てみることができます:https://stackoverflow.com/a/19461564/5583283

/* When the user clicks on the button, 
 
toggle between hiding and showing the dropdown content */ 
 
function myFunction() { 
 
    document.getElementById("myDropdown").classList.toggle("show"); 
 
} 
 

 
// Close the dropdown if the user clicks outside of it 
 
window.onclick = function(event) { 
 
    if (!event.target.matches('.dropbtn')) { 
 

 
    var dropdowns = document.getElementsByClassName("dropdown-content"); 
 
    var i; 
 
    for (i = 0; i < dropdowns.length; i++) { 
 
     var openDropdown = dropdowns[i]; 
 
     if (openDropdown.classList.contains('show')) { 
 
     openDropdown.classList.remove('show'); 
 
     } 
 
    } 
 
    } 
 
}
.container { 
 
    position: absolute; 
 
    margin-top: -38px; /* this represent half the height of the button */ 
 
    top: 50%; 
 
    text-align: center; /* remove this to align on the left */ 
 
    width: 100%; 
 
} 
 

 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px 40px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

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

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

 
.dropdown-content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    overflow: auto; 
 
    box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); 
 
} 
 

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

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

 
.show { 
 
    display: block; 
 
}
<div class="container"> 
 
    <div class="dropdown"> 
 
    <button onclick="myFunction()" class="dropbtn">Select City</button> 
 
    <div id="myDropdown" class="dropdown-content"> 
 
     <a href="#home">Mumbai</a> 
 
    </div> 
 
    </div> 
 
</div>

関連する問題