文本居中和按钮右对齐

泰勒

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

这是我所拥有的...

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: absoluteapply to 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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章