html - 文本居中和按钮右对齐
问题描述
如何使文本显示在中心并且按钮显示在框的右侧,同时仍然能够很好地缩放屏幕?这意味着当屏幕缩小时,框和文本之间的空间会增大,而当屏幕放大时,空间会缩小。
这是我所拥有的...
这是我想要得到的...
编辑:这是 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;
}
解决方案
您可以使用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>
推荐阅读
- reactjs - 65 个漏洞需要人工审查。有关详细信息,请参阅完整报告
- java - 计算 SHA-256 哈希时的前导零
- networking - Wireshark过滤所有当前数据包?
- jquery - jQuery append() 每个循环
- javascript - 我需要从数组内的对象中获取值
- jquery - 如何使用 jQuery 动态重新评估函数?
- javascript - 函数未在 setInterval 上运行
- python - aiohttp 的 pip 安装失败,出现奇怪的 AttributeError _dep_map
- ruby-on-rails - Rails 控制台中的 Unicode 问题,但在 pry 或 irb 中没有
- c# - 无法使用最新的 Windows 10 IoT Core 在 RPi3 上播放声音文件 (WAV)