It is often desirable to center a list of items within a fixed-width div. This can be achieved through a combination of HTML and CSS techniques.
Solution:
To center the list and its child elements, we employ the following steps:
Code:
<div class="wrapper"> <ul> <li>List Item 1</li> <li>List Item 2</li> <li>List Item 3</li> </ul> </div>
.wrapper { text-align: center; } .wrapper ul { display: inline-block; margin: 0; padding: 0; } .wrapper li { float: left; padding: 2px 5px; border: 1px solid black; }
Explanation:
By following these steps, you can effectively center a list within a div, ensuring a balanced and visually appealing layout.
The above is the detailed content of How to Center a List Horizontally Within a Div?. For more information, please follow other related articles on the PHP Chinese website!