-1

I have a list and css.

However the ul goes to the left. I have tried many things but it either stays inline and floats to the left or it goes to the center as I want it to, but then it's not inline, they stack on one another..how can I have both an centered ordered list and inline?

.navbar {
  display: table;
  margin: 0 auto;
  width: 100%;
}

.navbar ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: white;
}

.navbar ul li {
  display: inline-block;
}

li a {
  display: block;
  color: black;
  text-align: center;
  padding: 12px 16px;
  font-size: 25px;
  text-decoration: none;
}
<div class="navbar">
  <ul>
    <li><a href="#">The Stock</a></li>
    <li>
      <a> <img style="height: 50px; width: 50px" src="img/logo.svg" alt="logotype" /> </a>
    </li>
    <li><a href="#">The Stock</a></li>
  </ul>
</div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
KRiPT
  • 37
  • 5

5 Answers5

2

To center the navbar ul add text-align: center;

.navbar {
  display: table;
  margin: 0 auto;
  width: 100%;
}

.navbar ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: white;
  text-align: center;
}

.navbar ul li {
  display: inline-block;
}

li a {
  display: block;
  color: black;
  text-align: center;
  padding: 12px 16px;
  font-size: 25px;
  text-decoration: none;
}
<div class="navbar">
  <ul>
    <li><a href="#">The Stock</a></li>
    <li>
      <a> <img style="height: 50px; width: 50px" src="img/logo.svg" alt="logotype" /> </a>
    </li>
    <li><a href="#">The Stock</a></li>
  </ul>
</div>
theWellHopeErr
  • 1,856
  • 7
  • 22
1

All you need is "text-align:center;" on your .navbar ul like below:

.navbar ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: white;
  text-align: center;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Just use display: flex; justify-content: center; for either <ul> tag or navbar div container.

.navbar {
  display: table;
  margin: 0 auto;
  width: 100%;
}

.navbar ul {
  display: flex;
  justify-content: center;
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: white;
}

.navbar ul li {
  display: inline-block;
}

li a {
  display: block;
  color: black;
  text-align: center;
  padding: 12px 16px;
  font-size: 25px;
  text-decoration: none;
}
<div class="navbar">
  <ul>
    <li><a href="#">The Stock</a></li>
    <li>
      <a> <img style="height: 50px; width: 50px" src="img/logo.svg" alt="logotype" /> </a>
    </li>
    <li><a href="#">The Stock</a></li>
  </ul>
</div>
wangdev87
  • 8,611
  • 3
  • 8
  • 31
0

<li> runs as a text element because of your css .navbar ul li { display: inline-block; }. therefor you can center it with text-align. In this case just add: .navbar { text-align: center; }

.navbar {
  margin: 0 auto;
  width: 100%;
  text-align: center;
}

.navbar ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: white;
}

.navbar ul li {
  display: inline-block;
}

li a {
  display: block;
  color: black;
  text-align: center;
  padding: 12px 16px;
  font-size: 25px;
  text-decoration: none;
}
<div class="navbar">
  <ul>
    <li><a href="#">The Stock</a></li>
    <li>
      <a> <img style="height: 50px; width: 50px" src="img/logo.svg" alt="logotype" /> </a>
    </li>
    <li><a href="#">The Stock</a></li>
  </ul>
</div>
tacoshy
  • 10,642
  • 5
  • 17
  • 34
-1

Just change your .navbar class like this

.navbar {
   display: flex;
   justify-content: center;
}
MaxiGui
  • 6,190
  • 4
  • 16
  • 33
user1592129
  • 461
  • 1
  • 5
  • 16