Home > Article > Web Front-end > How to Horizontally Center an Unordered List Within a Div?
Problem: Centering an unordered list (
Goal: The goal is to achieve proper horizontal alignment of the list within the div container.
Solution:
To center a
Example Code:
/* Solution 1: max-width and margin */ .container ul { max-width: 400px; margin: 0 auto; } /* Solution 2: display: inline-block and text-align */ .container-2 { text-align: center; } .container-2 ul { display: inline-block; } /* Solution 3: display: table and margin */ .container-3 ul { display: table; margin: 0 auto; } /* Solution 4: position: absolute, translateX, and left */ .container-4 ul { position: absolute; left: 50%; transform: translateX(-50%); } /* Solution 5: Flexbox Layout */ .container-5 { display: flex; justify-content: center; } .container-5 ul { list-style-position: inside; }
By applying one of these solutions to your CSS, you can effectively center the unordered list horizontally within the div container.
The above is the detailed content of How to Horizontally Center an Unordered List Within a Div?. For more information, please follow other related articles on the PHP Chinese website!