【问题标题】:How to horizontally align items using flexbox?如何使用 flexbox 水平对齐项目?
【发布时间】:2020-05-16 18:15:43
【问题描述】:

这里是初学者。出于某种原因,在我正在编码的网站中,准按钮不会在导航栏上水平对齐。我正在使用 flexbox,但即便如此,它也不会像我想要的那样对齐它们。代码似乎也没有任何问题。

.topPart {
  position: fixed;
  overflow: hidden;
  top: 0;
  margin: 0px 15px 30px 15px;
  width: 1790
  padding: 30px;
  border: 2px solid #3cc851;
  background-color: #1f1f1f;
}


.topButtons {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-around;
  align-items: flex-center;
  height: 15px;
  width: auto;
  padding: 15px;
  background-color: #fff;
  color: #1f1f1f;
}


.topButtons .rightButtons {
  text-align: right;
  margin: auto 2px auto 1500px;
}


.topButtons .leftButtons {
  text-align: left;
  margin: auto 2% auto 2%;
}
   <div class="topPart">
     <h2> Website name I don't want to share </h2> <p> Website slogan I don't want to share </p>
     <!-- later, make it so that the boxes of text are lined up like how it looks in the code using css -->
   <div class="topButtons">
     <div class="leftButtons">
       <a href="index.html" class="home">home<a>
       <a href="" class="mssg">mssg board<a>
       <a href="database.html" class="data">database<a>
       <a href="roleplay.html" class="rp">rp<a>
     </div>
     <div class="rightButtons">
       <a href="" class="dms">dms</a>
       <a class="out">logout</a>
       <a class="acc">acc</a>
     </div>
     </div>
    </div>

右侧按钮似乎比左侧按钮低约 5 像素。我在这里完全不知所措,我的代码中似乎没有任何错误导致这种情况。应该修复它的 Flexbox 标签也不做任何事情,例如 flex-direction: row; 和 justify-content。

What it looks like.

【问题讨论】:

  • 能否请您附上相关的HTML部分
  • 尝试删除此行flex-flow: row wrap;。除此之外,按照这家伙说的做^^^
  • 这能回答你的问题吗? Flexbox align items horizontally
  • 不,justify-content 没有任何作用。
  • 你为什么不把width给父div topButtons而不是给右边的margin

标签: html css flexbox


【解决方案1】:

div 是一个块元素,因此保持 auto 的宽度基本上是推动第二个 div 向下的可用空间的 100%。

尝试 inline-flex 或将宽度设置为固定值而不是自动

【讨论】:

    【解决方案2】:

    这是由于“.leftButtons”和“.rightButtons”类的“text-align”和“margin”的CSS属性而发生的。您不需要使用这些属性,因为您在“.topButtons”类上使用“display:flex”。子类的水平和垂直对齐将由“justify-content: space-between;”管理和“对齐项目:中心;” “.topButtons”。

    我已删除“.leftButtons”和“.rightButtons”类的 CSS 属性。更新了“justify-content: space-between;”在“.topButtons”上,以便子类通过在元素之间留下相等的空间来水平对齐。

    我还对您发现的标记进行了一些更正,如下所述: (1) 缺少子“.leftButtons”的锚 (/a) 闭包标签。 (2) 在“.topPart”中“;”在宽度属性声明后丢失。还添加了宽度值为“1790”的“px”。

    请检查添加的代码。

                .topPart {
                  position: fixed;
                  overflow: hidden;
                  top: 0;
                  margin: 0px 15px 30px 15px;
                  width: 1790px;
                  padding: 30px;
                  border: 2px solid #3cc851;
                  background-color: #1f1f1f;
                }
    
                .topButtons {
                  display: flex;
                  flex-flow: row wrap;
                  /* justify-content: space-around; [old code] */ 
                  justify-content: space-between;
                  align-items: center;
                  height: 15px;
                  width: auto;
                  padding: 15px;
                  background-color: #fff;
                  color: #1f1f1f;
                }
    
                /*---- [old code]----*/
                .topButtons .rightButtons {
                  /*text-align: right;
                  margin: auto 2px auto 1500px;*/
                }
                
                .topButtons .leftButtons {
                  /*text-align: left;
                  margin: auto 2% auto 2%;*/
                }
    <div class="topPart">
             <h2> The Blood Tundra Kingdom </h2>
             <p> - never talk about the blood tundra kingdom </p>
             <!-- later, make it so that the boxes of text are lined up like how it looks in the code using css -->
               <div class="topButtons">
                     <div class="leftButtons">
                       <a href="index.html" class="home">home</a>
                       <a href="" class="mssg">mssg board</a>
                       <a href="database.html" class="data">database</a>
                       <a href="roleplay.html" class="rp">rp</a>
                     </div>
                     <div class="rightButtons">
                       <a href="" class="dms">dms</a>
                       <a class="out">logout</a>
                       <a class="acc">acc</a>
                     </div>
                </div>
            </div>

    【讨论】:

    • 我有左右类,因为它们有自己的一组按钮。
    • 好的。没关系。我现在在这里更新了我的代码。您仍然可以将左右类用于任何其他属性。因为上层阶级已经处理了对齐属性。我认为这是让您的代码更加优化和有用的最佳方式。
    【解决方案3】:

    您可以在 topPart 类中定义display: flex;,justify-content: space-between,align-items: center;,并通过此flex: 1 1 auto; css 属性等分div 内部的宽度。
    我希望在下面sn-p 会帮到你很多。

    *,*:before,*:after{box-sizing: border-box;}
    body{margin: 0; padding: 0}
    .topPart {
      position: fixed;
      overflow: hidden;
      top: 0;
      padding: 30px 20px;
      border-bottom: 2px solid #3cc851;
      background-color: #fff;
      width: 100%;
      z-index: 100;
      display: flex;
      flex-flow: row wrap;
      justify-content: space-between;
      align-items: center;
    }
    .leftButtons {
      flex: 1 1 auto;
      text-align: left;
    }
    .centerButtons {
      text-align: center;
      flex: 1 1 auto;
    }
    .rightButtons {
      text-align: right;
      flex: 1 1 auto;
    }
    .centerButtons a, .rightButtons a{
      display: inline-flex;
      background-color: #ccc;
      padding: 4px 5px;
      text-decoration: none;
      color: #444;
      text-transform: capitalize;
    }
    <div class="topPart">
      <div class="leftButtons">
        <div>Website Name<br>Website slogan</div>
      </div>
      <div class="centerButtons">
        <a href="index.html" class="home">home</a>
        <a href="#" class="mssg">mssg board</a>
        <a href="database.html" class="data">database</a>
        <a href="roleplay.html" class="rp">rp</a>
      </div>
      <div class="rightButtons">
        <a href="" class="dms">dms</a>
        <a class="out">logout</a>
        <a class="acc">acc</a>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 2022-01-26
      • 2019-12-13
      • 2017-12-02
      • 2021-03-18
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 2018-02-06
      相关资源
      最近更新 更多