This question already has answers here:
Center and right align flexbox elements
(6个答案)
去年关门了。
我试图把寄存器按钮从中间移到右边(如下面的照片所见),而不影响中间的3个项目。
这就是我所拥有的:
html - 将 flex 容器内的元素向右移动-LMLPHP
这就是我想要得到的:
html - 将 flex 容器内的元素向右移动-LMLPHP
这是我的代码:
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;,但它没有给我想要的。如果有人能帮忙,我将不胜感激!

最佳答案

您可以将按钮的位置设置为:

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

关于html - 将 flex 容器内的元素向右移动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52783457/

10-11 12:37