如何使文本显示在中间,按钮显示在框的右侧,同时仍然能够很好地缩放屏幕?这意味着当屏幕缩小时,框和文本之间的空间将增大,而当屏幕放大时,空间将缩小。
这就是我所拥有的...
这就是我想要得到的...
编辑:这是JSFiddle ...
https://jsfiddle.net/d697spr8/1/
<div id="outer">
<div class="topStuff">
<p>Games</p>
<div class="dropdownListPg">
<button class="dropbtn" style="height: 50px; width: 120px">Sort By
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdownListPg-content" style="color: black">
<a href="index.html"><button style="height: 50px; width: 120px">Alphabetical</button></a>
<a href="indexDate.html"><button style="height: 50px; width: 120px">Date</button></a>
<a href="indexUserScore.html"><button style="height: 50px; width: 120px">User Score</button></a>
</div>
</div>
</div>
</div>
#outer
{
min-width: 1200px;
}
.topStuff
{
display: flex;
justify-content: center;
margin: 0;
margin-top: 20px;
margin-left: 150px;
margin-right: 150px;
padding: 0;
background-color: #999999;
}
.dropdownListPg
{
display: inline;
}
.dropdownListPg .dropbtn
{
}
.dropdownListPg-content
{
display: none;
position: absolute;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
width: fit-content;
height: fit-content;
overflow-x: hidden;
margin: 0;
padding: 0;
}
.dropdownListPg-content a
{
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
color: black !important;
margin: 0;
padding: 0;
}
.dropdownListPg-content a:hover
{
background-color: wheat !important;
}
.dropdownListPg:hover .dropdownListPg-content
{
display: block;
}
答案 0 :(得分:1)
您可以使用flex
定位。另外我还注意到您将<button></button>
嵌套在<a></a>
内,将交互式元素嵌套到另一个交互式元素中是一种不好的做法。
还存在另一种将position: absolute
应用于dropdown
的变体,但在这种情况下,flex
更好。
#outer {
min-width: 1200px;
}
.topStuff {
display: flex;
justify-content: center;
margin-top: 20px;
margin-left: 150px;
margin-right: 150px;
background-color: #999999;
}
.holder {
flex: 1 0 auto;
}
.holder--align--right {
display: flex;
justify-content: flex-end;
}
.dropdownListPg {
position: relative;
}
.dropdownListPg-content {
position: absolute;
top: 100%;
z-index: 1;
display: none;
width: 100%;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdownListPg-content a {
display: block;
color: black;
padding: 12px 16px;
text-decoration: none;
text-align: left;
}
.dropdownListPg-content a:hover {
background-color: wheat;
}
.dropdownListPg:hover .dropdownListPg-content {
display: block;
}
<div id="outer">
<div class="topStuff">
<div class="holder"></div>
<p>Games</p>
<div class="holder holder--align--right">
<div class="dropdownListPg">
<button class="dropbtn" style="height: 50px; width: 120px">Sort By
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdownListPg-content">
<a href="index.html">Alphabetical</a>
<a href="indexDate.html">Date</a>
<a href="indexUserScore.html">User Score</a>
</div>
</div>
</div>
</div>
</div>