代码之家  ›  专栏  ›  技术社区  ›  Nazim Kerimbekov Gusev Slava

将flex容器内的项目向右移动[复制]

  •  2
  • Nazim Kerimbekov Gusev Slava  · 技术社区  · 6 年前

    我试图把登记按钮从中间移到右边(如下面的照片所见),而不影响中间的3个项目。

    这就是我所拥有的: present: CSS Menu Bar

    这就是我想要得到的: Desired: CSS Menu Bar

    这是我的密码:

    body {
      background-color: #323642;
    }
    
    .menubar {
      justify-content: center;
      display: flex;
      flex-direction: row;
      background-color: #272a33;
      margin-top: -10px;
      margin-left: -10px;
      margin-right: -10px;
      align-items: center;
      height: 100%;
    }
    
    .menuitem {
      padding: 11px;
      padding-top: 17px;
      padding-left: 29px;
      font-size: 22px;
      font-family: "Ostrich Sans";
      color: #ee5f95;
      text-decoration: none;
    }
    
    #login_button {
      margin-top: 2px;
      border-radius: 5px;
      background-color: #ee5f95;
      width: 100px;
      height: 34px;
      text-align: center;
      border: none;
      font-family: "Ostrich Sans";
      font-size: 22px;
      color: white;
      line-height: 33px;
      transition: 0.7s;
      justify-content: flex-end;
    }
    
    #login_button:hover {
      width: 110px;
      background-color: #ae466d;
      transition: 0.7s;
    }
    <head lang="Eng">
      <meta charset="UTF-8">
      <title>test </title>
    </head>
    
    <body>
      <div class="menubar" id="hey">
        <a class="menuitem" id="firstmenuitem" href="./buy_sell.html">Buy & Sell</a>
        <a class="menuitem" href="./exchange.html">Exchange</a>
        <a class="menuitem" href="./events.html">Events</a>
        <div id="delimeter"></div>
        <button id="login_button">Register</button>
      </div>
    </body>

    margin-right:auto; 虽然它只是完全移动按钮的权利,并没有提出任何空间之间的窗口。我也尝试过添加 justify-content: space-between;

    4 回复  |  直到 5 年前
        1
  •  2
  •   Mustapha Larhrouch    6 年前

    你可以把按钮的位置 absolute 这样地:

    #login_button {
        position: absolute;
        right: 10px;
        margin-top: 2px;
        border-radius: 5px;
        background-color: #ee5f95;
        width: 100px;
        height: 34px;
        text-align: center;
        border: none;
        font-family: "Ostrich Sans";
        font-size: 22px;
        color: white;
        line-height: 33px;
        transition: 0.7s;
        justify-content: flex-end;
    }
    

    https://jsfiddle.net/fh3cj02d/

        2
  •  0
  •   doppler    6 年前

    实现这一点的一种方法是使第一个和最后一个项目的左边距 auto . 在本例中,您将对 .menuitem:last-of-type #login_button .

    body {
        background-color: #323642;
    }
    
    .menubar {
        justify-content: center;
        display: flex;
        flex-direction: row;
        background-color: #272a33;
        margin-top: -10px;
        margin-left: -10px;
        margin-right: -10px;
        align-items: center;
        height: 100%;
    }
    .menuitem {
        padding: 11px;
        padding-top: 17px;
        padding-left: 29px;
        font-size: 12px;
        font-family: "Ostrich Sans";
        color: #ee5f95;
        text-decoration: none;
    }
    .menuitem:first-of-type {
        margin-left: auto; /* NEW */
    }
    #login_button {
        margin-left: auto; /* NEW */
        margin-top: 2px;
        border-radius: 5px;
        background-color: #ee5f95;
        width: 100px;
        height: 34px;
        text-align: center;
        border: none;
        font-family: "Ostrich Sans";
        font-size: 12px;
        color: white;
        line-height: 33px;
        transition: 0.7s;
        justify-content: flex-end;
    }
    #login_button:hover {
        width: 110px;
        background-color: #ae466d;
        transition: 0.7s;
    }
    <body>
        <div class="menubar" id="hey">
            <a class="menuitem" id="firstmenuitem" href="./buy_sell.html">Buy & Sell</a>
            <a class="menuitem" href="./exchange.html">Exchange</a>
            <a class="menuitem" href="./events.html">Events</a>
            <div id="delimeter"></div>
            <button id="login_button">Register</button>
        </div>
    </body>
        3
  •  0
  •   Marco Dagrada    6 年前

    我想你可以这样编辑你的身体:

    <body>
    <div class="menubar" id="hey">
        <a class="menuitem" id="firstmenuitem" href="./buy_sell.html">Buy & Sell</a>
        <a class="menuitem" href="./exchange.html">Exchange</a>
        <a class="menuitem" href="./events.html">Events</a>
        <div id="delimeter"></div>
        <button class="festa" id="login_button">Register</button>
    </div>
    

    .festa {
      position: absolute;
      right: 0px;
    }
    

    这将使3个按钮保持在屏幕的完美中心,寄存器保持在右边,但它们会在小屏幕上重叠。

    这是小提琴: https://jsfiddle.net/5xbnhkcr/

    否则,您可以使用flexbox权重(这与您期望的结果类似)

    按以下方式修改正文:

    <body>
      <div class="menubar" id="hey">
      <div class="buttons">
        <a class="menuitem" id="firstmenuitem" href="./buy_sell.html">Buy & Sell</a>
        <a class="menuitem" href="./exchange.html">Exchange</a>
        <a class="menuitem" href="./events.html">Events</a>
      <div id="delimeter"></div>
      </div>
        <button class="festa" id="login_button">Register</button>
    </div>
    

    .buttons {
      flex: 9;
      justify-content: center;
      display: flex;
      flex-direction: row;
    }
    .festa {
      flex: 1;
    }
    

    弹性:9; 弹性:1; 包含3个按钮的部分将占据宽度的9/10,注册按钮占1/10。

    这是小提琴: https://jsfiddle.net/e5hp2v7L/

        4
  •  0
  •   axanpi    6 年前

    你可以在这样的flexbox中使用flexbox

      body {
        background-color: #323642;
      }
    
      .menubar {
        display: flex;
        flex-direction: row;    
        background-color: #272a33;
        margin-top: -10px;
        margin-left: -10px;
        margin-right: -10px;
        height: 100%;
      }
      
      .left {    
        flex: 1;    
      }
    
      .middle {    
        display: flex;        
      }
      
      .right {
        flex: 1;
        display: flex;
        flex-direction: row-reverse;        
        align-items: center;
      }
      
    
      .menuitem {    
        padding: 11px;
        padding-top: 17px;
        padding-left: 29px;
        font-size: 22px;
        font-family: "Ostrich Sans";
        color: #ee5f95;
        text-decoration: none;
      }
    
    
      #login_button {
        margin-top: 2px;
        margin-right: 10px;
        border-radius: 5px;
        background-color: #ee5f95;
        width: 100px;
        height: 34px;
        text-align: center;
        border: none;
        font-family: "Ostrich Sans";
        font-size: 22px;
        color: white;
        line-height: 33px;
        transition: 0.7s;    
      }
    
      #login_button:hover {
        width: 110px;
        background-color: #ae466d;
        transition: 0.7s;
      }
    <html>
    
    <head lang="Eng">
      <meta charset="UTF-8">
      <title>test </title>
    </head>
    
    <body>
      <div class="menubar" id="hey">
        <div class="left"></div>
        <div class="middle">
          <a class="menuitem" id="firstmenuitem" href="./buy_sell.html">Buy & Sell</a>
          <a class="menuitem" href="./exchange.html">Exchange</a>
          <a class="menuitem" href="./events.html">Events</a>
          <div id="delimeter"></div>
        </div>
        <div class="right">
          <button id="login_button">Register</button>
        </div>
      </div>
    </body>
    
    </html>

    我喜欢这个,因为它是纯flexbox,没有绝对定位或自动边距。还有 .middle div自然是这样居中的。