PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 17, 2022

[FIXED] How do I vertically align my links for a navbar?

 November 17, 2022     css, html, vertical-alignment     No comments   

Issue

I am trying to make a navigation bar but I'm not sure how to line up the li and a tags vertically. How can I do this?

#navbar {
    font-weight: bold;
    display: block;
    background-color: #000;
}

li {
    float: left;
}

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #000;
}

li a {
    color: white;
    vertical-align: middle;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover {
    background-color: #111;
}
<div id="navbar">
        <ul>
            <li><img src="img/logo.png" width="75" height="75"></li>
            <li><a href="#">HOME</a></li>
            <li><a href="#">MY WORK</a></li>
            <li><a href="#">HIRE ME</a></li>
            <li><a href="#">PROJECTS</a></li>
        </ul>
    </div>


Solution

One of the common ways nowadays is to do it with the Flexbox:

#navbar {
  font-weight: bold;
  display: block;
  background-color: #000;
}

ul {
  display: flex; /* displays flex-items (children) inline */
  align-items: center; /* centers them vertically */
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #000;
}

li {
  float: left;
}

li a {
  color: white;
  vertical-align: middle;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover {
  background-color: #111;
}

/* addition */
img {display: block} /* or: "vertical-align: middle" // removes bottom margin/whitespace */
<div id="navbar">
  <ul>
    <li><img src="https://placehold.it/100x100" alt="" width="75" height="75"></li>
    <li><a href="#">HOME</a></li>
    <li><a href="#">MY WORK</a></li>
    <li><a href="#">HIRE ME</a></li>
    <li><a href="#">PROJECTS</a></li>
  </ul>
</div>



Answered By - VXp
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing