-1

I've been trying to make a nice drop-down menu, but the text is always on top! I want to align it in the centre, but "vertical-align" seems useless!

P.S. I haven't made any HTML/CSS codes for about a year or more and forgot almost everything about CSS. So some parts of CSS code are taken from the Internet. If there is something unnecessary, tell me please!

#navbar ul {
  display: none;
  position: absolute;
  top: 100%;
}

#navbar li:hover ul {
  display: block;
}

#navbar,
#navbar ul {
  margin: 0;
  padding: 0;
  list-style-type: none;
}

#navbar {
  height: 100px;
  width: 100%;
}

#navbar li {
  float: left;
  position: relative;
  height: 100%;
  width: 25%;
}

#navbar li a {
  display: block;
  height: 100px;
  text-align: center;
}

#navbar ul li {
  float: none;
}

#navbar li:hover {
  background-color: #6da3a3;
}

#navbar ul li:hover {
  background-color: #6da3a3;
}

#vnavbar li {
  width: 100%;
  height: 100px;
}

#vnavbar ul {
  width: 100%;
}

#vnavbar {
  width: 100%;
}
<ul id="navbar">
  <li><a href="#">MAIN</a></li>
  <li><a href="#">NEWS</a></li>
  <li><a href="#">CONTACT</a>
    <ul id="vnavbar">
      <li><a href="#">ADDRESS</a></li>
      <li><a href="#">PHONE NUMBER</a></li>
      <li><a href="#">EMAIL</a></li>
    </ul>
  </li>
  <li><a href="#">ABOUT US</a></li>
</ul>
Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35
Ingvar
  • 3
  • 2

3 Answers3

3

Go with flex implementation. This will help you get rid of position: relative;, relative width float: left; and a lot other things.

  • Add display: flex; to #navbar.
  • Add flex: 1; display: flex; align-items: center; justify-content: center; to #navbar li
  • Remove height from #navbar li a

#navbar ul {
    display: none;
    position: absolute;
    top: 100%;
}

#navbar li:hover ul {
    display: block;
}

#navbar,
#navbar ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
}

#navbar {
    height: 100px;
    width: 100%;

    display: flex;
}

#navbar li {
    /* float: left;
    position: relative;
    height: 100%;
    width: 25%; */
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

#navbar li a {
    display: block;
    /* height: 100px; */
    text-align: center;
}

#navbar ul li {
    float: none;
}

#navbar li:hover {
    background-color: #6da3a3;
}

#navbar ul li:hover {
    background-color: #6da3a3;
}

#vnavbar li {
    width: 100%;
    height: 100px;
}

#vnavbar ul {
    width: 100%;
}

#vnavbar {
    width: 100%;
}
<ul id="navbar">
    <li><a href="#">MAIN</a></li>
    <li><a href="#">NEWS</a></li>
    <li><a href="#">CONTACT</a>
        <ul id="vnavbar">
            <li><a href="#">ADDRESS</a></li>
            <li><a href="#">PHONE NUMBER</a></li>
            <li><a href="#">EMAIL</a></li>
        </ul>
    </li>
    <li><a href="#">ABOUT US</a></li>
</ul>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
0

With #navbar li a Instead of using display: block, you can use display:flex then align items like this:

#navbar li a {
    display: flex;
    height: 100px;
    text-align: center;
    align-items: center;
    justify-content: center;
}
-2

Use this code

yourIdOrClass {
text-align: center
}
Aaseer
  • 117
  • 10
  • 1
    Down-voted for following reasons: Code only ansers can be considered low-quality as they are hard to understand for the OP and therefor hard to reproduce. Also you are technical wrong. `text-align` has no influence on block-level elements such as `
      ` and `
    • ` in this case.
    – tacoshy Aug 15 '21 at 18:49