两个不同宽度的弹性盒子的免费填充 space
Filling free space of two flex boxes with different widths
所以我一直在为 select 不同的语言创建一个下拉菜单。代码如下:
//Show DropDown Menu
var button = document.getElementById('select-lang-btn');
var ul = document.getElementById('language_dropdown');
button.onclick = function() {
if (ul.style.display === 'block') {
ul.style.display = 'none';
} else {
ul.style.display = 'block';
}
};
//Remove DropDown Menu if clicked outside
window.addEventListener('mouseup', function(event) {
if (event.target != ul && event.target.parentNode != ul) {
ul.style.display = 'none';
}
});
//Select Language
function show(anything) {
document.getElementById("selected-lang").innerHTML = anything
document.getElementById('language_dropdown').style.display = 'none';
if (anything === 'English') {
document.getElementById("select-lang-img").src = "gb.svg";
} else if (anything === 'German') {
document.getElementById("select-lang-img").src = "de.svg";
} else if (anything === 'Spanish') {
document.getElementById("select-lang-img").src = "es.svg";
} else {
document.getElementById("select-lang-img").src = "global.svg";
}
}
body {
background-color: #339699;
}
.language_section {
width: 500px;
}
.reverse_language {
width: 200px;
height: 65px;
background-color: #afafaf;
}
.language_section>div {
display: flex;
}
#select-lang-btn {
display: flex;
padding: 20px 15px;
outline: none;
cursor: pointer;
background-color: white;
align-items: center;
}
#selected-lang {
font-family: "Montserrat", "Open Sans";
margin-left: 5px;
}
.language_flag {
width: 24px;
height: 24px;
}
.language_dropdown {
padding: 0;
display: none;
list-style: none;
cursor: pointer;
}
.language_dropdown>li>span:hover {
background-color: #d7d7d7;
}
.language_dropdown>li>span {
background-color: white;
display: flex;
align-items: center;
padding: 10px;
}
.language_text {
color: black;
text-decoration: none;
padding-left: 10px;
font-family: "Montserrat", "Open Sans";
}
.language_text {
line-height: 2;
}
.icon-angle-down {
margin-top: -30px;
margin-left: 15px;
width: 5px;
height: 5px;
border-right: 2px solid #444444;
border-top: 2px solid #444444;
transform: rotate(135deg);
}
<div style="display:flex; justify-content: center;">
<div class="language_section">
<div><span id="select-lang-btn"><img id="select-lang-img" class="language_flag" src="img/flag/gb.svg"><a id="selected-lang">English</a><div class="icon-angle-down"></div></span></div>
<ul class="language_dropdown" id="language_dropdown">
<li onclick="show('English')"><span><img class="language_flag" src="img/flag/gb.svg"><a href="#" class="language_text">English</a></span></li>
<li onclick="show('German')"><span><img class="language_flag" src="img/flag/de.svg"><a href="#" class="language_text">German</a></span></li>
<li onclick="show('Spanish')"><span><img class="language_flag" src="img/flag/es.svg"><a href="#" class="language_text">Spanish</a></span></li>
</ul>
</div>
<div class="reverse_language"></div>
</div>
使用这段代码我得到以下结果:
但是有免费的 space,我想避免。我尝试了许多不同的方法来摆脱这个问题。我想出的最好办法是删除 .language_section
的 witdth。这是结果:
但是正如您在我的图片中看到的那样,下拉菜单也发生了变化,我也不想这样。有谁知道这个问题的解决方案吗?
您可以将 position: relative
添加到 .language_section
.language_section {
position: relative;
}
然后将 position: absolute
添加到 .language_dropdown
以从 language_section
width
中分离语言列表
.language_dropdown {
padding: 0;
display: none;
list-style: none;
cursor: pointer;
position: absolute; /*Detach the current element to not depend on the parent width*/
width: 250px; /*Width for your language list*/
}
最终结果
注意语言列表我设置了250px
,你可以根据自己的需要设置
完整代码
//Show DropDown Menu
var button = document.getElementById('select-lang-btn');
var ul = document.getElementById('language_dropdown');
button.onclick = function() {
if (ul.style.display === 'block') {
ul.style.display = 'none';
} else {
ul.style.display = 'block';
}
};
//Remove DropDown Menu if clicked outside
window.addEventListener('mouseup', function(event) {
if (event.target != ul && event.target.parentNode != ul) {
ul.style.display = 'none';
}
});
//Select Language
function show(anything) {
document.getElementById("selected-lang").innerHTML = anything
document.getElementById('language_dropdown').style.display = 'none';
if (anything === 'English') {
document.getElementById("select-lang-img").src = "gb.svg";
} else if (anything === 'German') {
document.getElementById("select-lang-img").src = "de.svg";
} else if (anything === 'Spanish') {
document.getElementById("select-lang-img").src = "es.svg";
} else {
document.getElementById("select-lang-img").src = "global.svg";
}
}
body {
background-color: #339699;
}
.language_section {
position: relative;
}
.reverse_language {
width: 200px;
height: 65px;
background-color: #afafaf;
}
.language_section>div {
display: flex;
}
#select-lang-btn {
display: flex;
padding: 20px 15px;
outline: none;
cursor: pointer;
background-color: white;
align-items: center;
}
#selected-lang {
font-family: "Montserrat", "Open Sans";
margin-left: 5px;
}
.language_flag {
width: 24px;
height: 24px;
}
.language_dropdown {
padding: 0;
display: none;
list-style: none;
cursor: pointer;
position: absolute;
/*Detach the current element to not depend on the parent width*/
width: 250px;
/*Width for your language list*/
}
.language_dropdown>li>span:hover {
background-color: #d7d7d7;
}
.language_dropdown>li>span {
background-color: white;
display: flex;
align-items: center;
padding: 10px;
}
.language_text {
color: black;
text-decoration: none;
padding-left: 10px;
font-family: "Montserrat", "Open Sans";
}
.language_text {
line-height: 2;
}
.icon-angle-down {
margin-top: -30px;
margin-left: 15px;
width: 5px;
height: 5px;
border-right: 2px solid #444444;
border-top: 2px solid #444444;
transform: rotate(135deg);
}
<div style="display:flex; justify-content: center;">
<div class="language_section">
<div><span id="select-lang-btn"><img id="select-lang-img" class="language_flag" src="img/flag/gb.svg"><a id="selected-lang">English</a>
<div class="icon-angle-down"></div>
</span></div>
<ul class="language_dropdown" id="language_dropdown">
<li onclick="show('English')"><span><img class="language_flag" src="img/flag/gb.svg"><a href="#" class="language_text">English</a></span></li>
<li onclick="show('German')"><span><img class="language_flag" src="img/flag/de.svg"><a href="#" class="language_text">German</a></span></li>
<li onclick="show('Spanish')"><span><img class="language_flag" src="img/flag/es.svg"><a href="#" class="language_text">Spanish</a></span></li>
</ul>
</div>
<div class="reverse_language"></div>
</div>
所以我一直在为 select 不同的语言创建一个下拉菜单。代码如下:
//Show DropDown Menu
var button = document.getElementById('select-lang-btn');
var ul = document.getElementById('language_dropdown');
button.onclick = function() {
if (ul.style.display === 'block') {
ul.style.display = 'none';
} else {
ul.style.display = 'block';
}
};
//Remove DropDown Menu if clicked outside
window.addEventListener('mouseup', function(event) {
if (event.target != ul && event.target.parentNode != ul) {
ul.style.display = 'none';
}
});
//Select Language
function show(anything) {
document.getElementById("selected-lang").innerHTML = anything
document.getElementById('language_dropdown').style.display = 'none';
if (anything === 'English') {
document.getElementById("select-lang-img").src = "gb.svg";
} else if (anything === 'German') {
document.getElementById("select-lang-img").src = "de.svg";
} else if (anything === 'Spanish') {
document.getElementById("select-lang-img").src = "es.svg";
} else {
document.getElementById("select-lang-img").src = "global.svg";
}
}
body {
background-color: #339699;
}
.language_section {
width: 500px;
}
.reverse_language {
width: 200px;
height: 65px;
background-color: #afafaf;
}
.language_section>div {
display: flex;
}
#select-lang-btn {
display: flex;
padding: 20px 15px;
outline: none;
cursor: pointer;
background-color: white;
align-items: center;
}
#selected-lang {
font-family: "Montserrat", "Open Sans";
margin-left: 5px;
}
.language_flag {
width: 24px;
height: 24px;
}
.language_dropdown {
padding: 0;
display: none;
list-style: none;
cursor: pointer;
}
.language_dropdown>li>span:hover {
background-color: #d7d7d7;
}
.language_dropdown>li>span {
background-color: white;
display: flex;
align-items: center;
padding: 10px;
}
.language_text {
color: black;
text-decoration: none;
padding-left: 10px;
font-family: "Montserrat", "Open Sans";
}
.language_text {
line-height: 2;
}
.icon-angle-down {
margin-top: -30px;
margin-left: 15px;
width: 5px;
height: 5px;
border-right: 2px solid #444444;
border-top: 2px solid #444444;
transform: rotate(135deg);
}
<div style="display:flex; justify-content: center;">
<div class="language_section">
<div><span id="select-lang-btn"><img id="select-lang-img" class="language_flag" src="img/flag/gb.svg"><a id="selected-lang">English</a><div class="icon-angle-down"></div></span></div>
<ul class="language_dropdown" id="language_dropdown">
<li onclick="show('English')"><span><img class="language_flag" src="img/flag/gb.svg"><a href="#" class="language_text">English</a></span></li>
<li onclick="show('German')"><span><img class="language_flag" src="img/flag/de.svg"><a href="#" class="language_text">German</a></span></li>
<li onclick="show('Spanish')"><span><img class="language_flag" src="img/flag/es.svg"><a href="#" class="language_text">Spanish</a></span></li>
</ul>
</div>
<div class="reverse_language"></div>
</div>
使用这段代码我得到以下结果:
但是有免费的 space,我想避免。我尝试了许多不同的方法来摆脱这个问题。我想出的最好办法是删除 .language_section
的 witdth。这是结果:
但是正如您在我的图片中看到的那样,下拉菜单也发生了变化,我也不想这样。有谁知道这个问题的解决方案吗?
您可以将 position: relative
添加到 .language_section
.language_section {
position: relative;
}
然后将 position: absolute
添加到 .language_dropdown
以从 language_section
width
.language_dropdown {
padding: 0;
display: none;
list-style: none;
cursor: pointer;
position: absolute; /*Detach the current element to not depend on the parent width*/
width: 250px; /*Width for your language list*/
}
最终结果
注意语言列表我设置了250px
,你可以根据自己的需要设置
完整代码
//Show DropDown Menu
var button = document.getElementById('select-lang-btn');
var ul = document.getElementById('language_dropdown');
button.onclick = function() {
if (ul.style.display === 'block') {
ul.style.display = 'none';
} else {
ul.style.display = 'block';
}
};
//Remove DropDown Menu if clicked outside
window.addEventListener('mouseup', function(event) {
if (event.target != ul && event.target.parentNode != ul) {
ul.style.display = 'none';
}
});
//Select Language
function show(anything) {
document.getElementById("selected-lang").innerHTML = anything
document.getElementById('language_dropdown').style.display = 'none';
if (anything === 'English') {
document.getElementById("select-lang-img").src = "gb.svg";
} else if (anything === 'German') {
document.getElementById("select-lang-img").src = "de.svg";
} else if (anything === 'Spanish') {
document.getElementById("select-lang-img").src = "es.svg";
} else {
document.getElementById("select-lang-img").src = "global.svg";
}
}
body {
background-color: #339699;
}
.language_section {
position: relative;
}
.reverse_language {
width: 200px;
height: 65px;
background-color: #afafaf;
}
.language_section>div {
display: flex;
}
#select-lang-btn {
display: flex;
padding: 20px 15px;
outline: none;
cursor: pointer;
background-color: white;
align-items: center;
}
#selected-lang {
font-family: "Montserrat", "Open Sans";
margin-left: 5px;
}
.language_flag {
width: 24px;
height: 24px;
}
.language_dropdown {
padding: 0;
display: none;
list-style: none;
cursor: pointer;
position: absolute;
/*Detach the current element to not depend on the parent width*/
width: 250px;
/*Width for your language list*/
}
.language_dropdown>li>span:hover {
background-color: #d7d7d7;
}
.language_dropdown>li>span {
background-color: white;
display: flex;
align-items: center;
padding: 10px;
}
.language_text {
color: black;
text-decoration: none;
padding-left: 10px;
font-family: "Montserrat", "Open Sans";
}
.language_text {
line-height: 2;
}
.icon-angle-down {
margin-top: -30px;
margin-left: 15px;
width: 5px;
height: 5px;
border-right: 2px solid #444444;
border-top: 2px solid #444444;
transform: rotate(135deg);
}
<div style="display:flex; justify-content: center;">
<div class="language_section">
<div><span id="select-lang-btn"><img id="select-lang-img" class="language_flag" src="img/flag/gb.svg"><a id="selected-lang">English</a>
<div class="icon-angle-down"></div>
</span></div>
<ul class="language_dropdown" id="language_dropdown">
<li onclick="show('English')"><span><img class="language_flag" src="img/flag/gb.svg"><a href="#" class="language_text">English</a></span></li>
<li onclick="show('German')"><span><img class="language_flag" src="img/flag/de.svg"><a href="#" class="language_text">German</a></span></li>
<li onclick="show('Spanish')"><span><img class="language_flag" src="img/flag/es.svg"><a href="#" class="language_text">Spanish</a></span></li>
</ul>
</div>
<div class="reverse_language"></div>
</div>