html - 文本居中且按钮右对齐

标签 html css

如何让文本显示在中心,按钮显示在框的右侧,同时仍然能够很好地缩放屏幕?这意味着框和文本之间的空间会在屏幕缩小时变大,而在屏幕放大时空间会缩小。

这是我的...

https://imgur.com/a/MigatID

这是我想要得到的...

https://imgur.com/H2gCl0S

编辑:这是 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>

关于html - 文本居中且按钮右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57134769/

相关文章:

html - 在这种情况下如何制作 Logo 之类的文字?

android - 在 Android 中将本地 html 文件加载到 Intent 的正确路径是什么?

javascript - 链接淡入、淡出、动画、执行顺序错误

css - 定义相同类的两个 css 文件

css - 在 Less CSS 中向列表添加值

javascript - 交替重复倒计时 - html - javascript

css - 多个 div 的 100% 高度

jquery - 如何将 silverlight 对象的大小调整到可用空间

css - 如何使文本从 x 轴上的空间位置开始(不同的 div)

jquery - 单击时,向下滑动 div 并更改按钮图标