-1

I have a php web page with a header on it. Inside the header I have a logo, and a nav tag with links, and a login form in it. The only problem is that everything in the nav is aligned at the top of the header. Is there a way to center all of it inside the header? I have tried properties like vertical-align: middle and nothing has worked. I have looked at other questions and tried to implement it, but nothing has worked so far.

body {
  margin: 0;
  background: #555;
  font-family: 'Work Sans', sans-serif;
  font-weight: 300;
}

.container {
  width: 80%;
  margin: 0 auto;
}

header {
  background: #55d6aa;
}

header::after {
  content: '';
  display: table;
  clear: both;
}

.logo {
  float: left;
  height: 40px;
  width: 40px;
  padding: 0;
  margin: 0;
}

nav {
  float: right;
  display: flex;
}

nav div{
  display: flex;
  float: right;
}

nav a {
  color: #444;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 16px;
}
<header>

  <div class="container">

    <a href="#">
      <img src="./res/TempLogo.png" class="logo" alt="DailyMath logo">
    </a>
    <nav>
        <div><a href="index.php">Home</a></div>
        <div><a href="#">Question of the Day</a></div>
        <div><a href="#">About</a></div>
        <div><a href="#">Login</a></div>

        <div>
          <form action="includes/login.inc.php" method="post">
            <div><input type="text" name="mailuid" placeholder="Username/Email"></div>
            <div><input type="password" name="pwd" placeholder="Password"></div>
            <div><button type="submit" name="login-submit">Login</button></div>
          </form>
        </div>

        <div><a href="signup.php">Sign up</a></div>

        <div>
          <form action="includes/logout.inc.php" method="post">
            <div><button type="submit" name="logout-submit">Logout</button></div>
          </form>
        </div>

    </nav>
  </div>

</header>

What I want:

SpencerLS
  • 361
  • 5
  • 16

1 Answers1

0

I think your CSS is a little bloated right now (no doubt from trying to figure this out), but I've made some edits that I believe achieve your goal.

You need to have display:flex on the .container and then align-items:center to vertically center the child elements.

body {
  margin: 0;
  background: #555;
  font-family: 'Work Sans', sans-serif;
  font-weight: 300;
}

.container {
display:flex;
flex-direction:row;
flex-wrap:nowrap;
  width: 80%;
  margin: 0 auto;
  align-items:center;
}

header {
  background: #55d6aa;
}

header::after {
  content: '';
  display: table;
  clear: both;
}
.logo {
  flex:0 0 15%;
}
.logo img {
  height: 40px;
  width: 40px;
  padding: 0;
  margin: 0;
}

nav {
  float: right;
  display: flex;
}

nav div{
  display: flex;
  float: right;
}

nav a {
  color: #444;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 16px;
}
<header>

  <div class="container">

    <a class="logo" href="#">
      <img src="./res/TempLogo.png" alt="DailyMath logo">
    </a>
    <nav>
        <div><a href="index.php">Home</a></div>
        <div><a href="#">Question of the Day</a></div>
        <div><a href="#">About</a></div>
        <div><a href="#">Login</a></div>

        <div>
          <form action="includes/login.inc.php" method="post">
            <div><input type="text" name="mailuid" placeholder="Username/Email"></div>
            <div><input type="password" name="pwd" placeholder="Password"></div>
            <div><button type="submit" name="login-submit">Login</button></div>
          </form>
        </div>

        <div><a href="signup.php">Sign up</a></div>

        <div>
          <form action="includes/logout.inc.php" method="post">
            <div><button type="submit" name="logout-submit">Logout</button></div>
          </form>
        </div>

    </nav>
  </div>

</header>
jdfink
  • 339
  • 1
  • 6
  • If you run your snippet in full page mode, you can see that the text is higher up on the page than the buttons and input fields. This is about as good as I was getting on my own. – SpencerLS Feb 03 '20 at 23:57
  • That's caused by the styling on the form elements. Set `margin:0` on those and they'll line up better with the text. – jdfink Feb 04 '20 at 00:07
  • I tried that, and it doesn't do anything... – SpencerLS Feb 04 '20 at 00:45