将弹性容器内的项目向右移动

时间:2018-10-12 16:18:37

标签: html css flexbox

我试图将注册按钮从中间向右移动(如下图所示),而不会影响中间的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 个答案:

答案 0 :(得分:1)

您可以像这样放置按钮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/

答案 1 :(得分:0)

一种实现此目的的方法是使第一个和最后一个项目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>

答案 2 :(得分:0)

我认为您可以这样修改身体:

<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>

并添加此CSS类:

.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>

并添加这些CSS类:

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

使用 flex:9; flex:1; 将div分为10个部分。 包含3个按钮的部分将占宽度的9/10,并且注册按钮将占1/10。

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

答案 3 :(得分:0)

您可以在这样的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自然也居中居中。