2

I'm trying to center a ul for mobiles. This is my HTML code:

<nav>
    <ul class="nav-items">
        <li class="nav-active"><a href="/">Home</a></li>
        <li class="nav-item"><a href="#nav-active">Fruits</a></li>
        <li class="nav-item"><a href="#nav-inactive">Veggies</a></li>
    </ul>
</nav>

My CSS code:

.nav-items {
   align-items: center;
   display: flex;
   flex-wrap: wrap;
   font-weight: normal;
   justify-content: flex-end;
   margin-right: 30px;
   margin-top: 20px;
}

And I kept a media query for phones:

@media screen and (max-width: 500px) {
   .nav-items {
       margin: auto;
   }
}

How can I make the ul center (for phones)?

This is what I actually wanted:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • What do you want exactly do you want to display item in top of the `ul`, item in the middle , item at the end or you just wants all of `li` to be centered at the middle of `ul` or you want the `ul` as overlay add some cleatify to your question there's a lot of valid answers but you don't accept anyone because you are not clear enough – JS_INF Jul 17 '21 at 13:02

10 Answers10

2

Try this CSS code:

nav{
   text-align: center
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

File style.css

@media screen and (max-width: 500px) {
    .nav-items {
        margin: auto;
        display: flex;
        flex-direction: column;
        justify-content: center;
    }
}

.sub-items {
  display: flex;
  flex-direction: row;
  justify-content: center;
  text-align: center;
}

}

File index.html

<ul class="nav-items">
    <ul class="sub-items">
    <li class="nav-active"><a href="/">Home</a></li>
    <li class="nav-item"><a href="#nav-active">Fruits</a></li>
    </ul>
    <li class="nav-item"><a href="#nav-inactive">Veggies</a></li>
</ul>

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anonymous Coder
  • 556
  • 2
  • 16
1

There are multiple ways to do it:

Text align method (based on How can I center <ul> <li> into a div?):

@media screen and (max-width: 500px) {
    .nav-items {
        text-align: center;
    }
    .nav-items>li>ul {
        display: inline-table;
    }
    .nav-items>li {
        display: inline;
    }
}

Using Flexbox: Look at boris's answer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ragedcoder
  • 57
  • 11
1

Something of this must be working. Try it:

@media screen and (max-width: 500px) {
    nav{
        display: flex;
        justify-conten: center;
        align-content: center;
        align-items center;
        align-self: center;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Shouldn't there be a colon after "align-items"? Isn't "justify-conten" as misspelling? Both seem to be correct in the question. – Peter Mortensen Aug 11 '21 at 08:34
1

If you are not concerned about the bulletins then you can go with <center> tag.

<nav>
  <center>
    <ul class="nav-items">
        <li class="nav-active"><a href="/">Home</a></li>
        <li class="nav-item"><a href="#nav-active">Fruits</a></li>
        <li class="nav-item"><a href="#nav-inactive">Veggies</a></li>
    </ul>
  </center>
</nav>
Uttam
  • 718
  • 6
  • 19
1

All you need is replace margin: auto with justify-content: space-between;.

Here margin: auto haven't anything to do, because the element already takes the full width of the page. margin: auto is working when the element width is less than the page width while justify-content: space-between; will align the first element at first and the second element at the middle and third at the end.

Here's your code:

.nav-items {
   align-items: center;
   display: flex;
   flex-wrap: wrap;
   font-weight: normal;
   justify-content: flex-end;
   margin-right: 30px;
   margin-top: 20px;
}
@media screen and (max-width: 500px) {
   .nav-items {
       justify-content: space-between;
   }
}

Also, if you want just to make the elements at the middle, you can use justify-content: center;, align-items: center;. That will make them at the middle of the .nav-items.

Example

.nav-items {
   align-items: center;
   display: flex;
   flex-wrap: wrap;
   font-weight: normal;
   justify-content: flex-end;
   margin-right: 30px;
   margin-top: 20px;
}
@media screen and (max-width: 500px) {
   .nav-items {
       justify-content: center;
       align-items: center;
   }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JS_INF
  • 467
  • 3
  • 10
1

This may help. See the output below. I have given the output screenshot link below.

@media screen and (max-width: 500px) {
    .nav-items {
        justify-content: center;
    }

    .nav-items li:nth-of-type(3) {
        width: 100%;
        text-align: center;
    }
}

See the output

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Hello and welcome to stackoverflow, here is a suggestion to improve the post : you can add a language identifier to highlight the code and make it more readable. – I_love_vegetables Jul 18 '21 at 05:38
  • More concrete: *[What is syntax highlighting and how does it work?](https://meta.stackexchange.com/questions/184108/what-is-syntax-highlighting-and-how-does-it-work/184109#184109)* – Peter Mortensen Aug 11 '21 at 08:39
1

very simple and easy method because no forced width or parent div involved.

ul {
  display: table;
  margin: 0 auto;
}
<ul>
    <li>12345678910</li>
    <li>638023274</li>
    <li>12323145645</li>
  </ul>
Muhammad Ali
  • 956
  • 3
  • 15
  • 20
0
@media screen and (max-width: 500px) {
   .nav-items {
       margin: auto;
       flex-direction: column;
   }
}

If you want the last item to be on a new line:

@media screen and (max-width: 500px) {
    .nav-items {
        margin: auto;
        justify-content: center;
    }

    .nav-items li{
        text-align: center;
    }

   .nav-items li:last-of-type{
        width: 100%;
   }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-1
<nav style="text-align: center;" >
    <ul style="list-style-type: none;" >
        <li>google</li>
        <li>bing</li>
        <li>yahoo</li>
    </ul>

</nav>
  • An explanation would be in order. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/68420585/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 11 '21 at 08:23