0

I would want to centre the li element in ul vertically. I've tried using text-align:center but it didn't work. I've been searching for a solution but it doesn't work. I'd also want an explanation to the solution because I'm relatively new to css.

/*Default styling*/

 * {
     margin: 0;
     padding: 0;
 }



/*Links section*/
.links {
    background-color: coral;
    width:100%;
    height:40px;

}

.links ul {
}

.links li {
    margin: auto;
    display:inline-block;
    text-align:center;
    height:100%;
    padding: 0 20px;
     font-family:Arial, Helvetica, sans-serif;


}
.links li:hover {
    background-color: red;
    width:auto;
}
<!DOCTYPE html>
<html>
    <head>
        <link href="./style.css" type="text/css" rel="stylesheet">
        <title>Beans testing AREA</title>
    </head>
    <body>
            <ul class="links">
                <li>
                    Home
                </li>
                <li>
                    About
                </li>
                <li>
                    Life
                </li>
            </ul>
    </body>
</html>
Breaz Freind
  • 43
  • 1
  • 6
  • have you tried: vertical-align: middle; https://www.w3schools.com/cssref/pr_pos_vertical-align.asp – developer Mar 02 '18 at 12:20
  • 1
    text-align - used only for aligning text horizontally. u can make it with flex property: display: flex; justify-content: center; flex-direction: column; https://jsfiddle.net/rj5zp3r0/11/ – Vasyl Gutnyk Mar 02 '18 at 12:34

1 Answers1

2

Add a line-height and vertical-align to the list items.

/*Default styling*/
* {
  margin: 0;
  padding: 0;
}


/*Links section*/
.links {
  background-color: coral;
  width: 100%;
  height: 40px;
}
.links li {
  margin: auto;
  display: inline-block;
  text-align: center;
  padding: 0 20px;
  font-family: Arial, Helvetica, sans-serif;
  height: 100%;
  line-height: 40px;
  vertical-align: center;
}

.links li:hover {
  background-color: red;
  width: auto;
}
<ul class="links">
  <li>Home</li>
  <li>About</li>
  <li>Life</li>
</ul>
GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59