4

I'm trying to move the register button from the middle to the right (as see on the following photos) Without it affecting the 3 items in the middle.

So this is what I have: present: CSS Menu Bar

This is what I'm trying to get: Desired: CSS Menu Bar

Here is my code:

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>

I have tried adding margin-right:auto; though it just completly shifter the button to the right and didn't leve any space between it and the window. I have also tried adding justify-content: space-between; but it didn't give me what I wanted. if anyone could help that would be greatly appreciated!

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58

4 Answers4

3

you can put the position of the button absolute like this :

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

Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28
1

You could use a flexbox inside a flexbox like this

  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>

I like this because it's purely flexbox, no absolute positioning or margin auto. Also the .middle div is naturally centered this way.

axanpi
  • 731
  • 9
  • 16
0

One way to accomplish this is to make the left margin of the first and last item auto. In this example, you would apply a left margin to .menuitem:last-of-type and #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>
doppler
  • 795
  • 5
  • 9
  • As noted, I have tried doing this the problem is that it "glues" the button to the browser window. (I want to have some space between both the browser window and the button) – Nazim Kerimbekov Oct 12 '18 at 16:31
  • @Fozoro you can just add some `padding-right` to the container, but absolutely positioning you login button (from another answer here) is better because you're changing its width on hover. – Strelok Oct 12 '18 at 16:39
0

I think you can just edit your body this way:

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

and add this css class:

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

This will keep the 3 buttons in the perfect center of the screen and the register one to the right, but they will overlap onto small screens.

This is the fiddle: https://jsfiddle.net/5xbnhkcr/

Otherwise you can play with flexbox weights (this is similar to your desired result)

modify body this way:

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

and add those css classes:

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

Using flex: 9; and flex: 1; will divide the div into 10 parts. The part containing the 3 buttons will occupy 9/10 of the width and the register button 1/10.

here's the fiddle: https://jsfiddle.net/e5hp2v7L/