How I can center a menu bar with ul

后端 未结 3 1320
孤街浪徒
孤街浪徒 2020-12-12 04:20

I have a simple menu bar inside a div and I can center the div but I want to center the menu bar inside the div.

This is the code:

CSS:

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 05:01

    First of all, please note that you should put the elements inside the tag.

    Actually, The ul element is aligned at the center (See demo here). But the point is that the list items are floated to the left.

    There are tow options:

    1. Using a proper fixed width (or max-width as you did) for the

        element. Working Demo

      • Or use display: inline-block instead of float: left for the list items, and use text-align: center for the ul to align the inline elements horizontally. Working Demo.

    But note that there is a white space between inline(-block) elements, which absolutely the normal behavior of inline elements in inline flow. You can refer my answer here to fix that.

提交回复
热议问题